Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. import collections
  2. import string
  3.  
  4. def retrieveMostFrequentlyUseWords(helpText, wordsToExclude):
  5. wordsToExclude = set([item.lower() for item in wordsToExclude])
  6.  
  7. helpText = helpText.lower()
  8. exclude = set(string.punctuation)
  9. helpText = ''.join(ch if ch not in exclude else " " for ch in helpText).split()
  10.  
  11. c = collections.Counter()
  12. for word in helpText:
  13. c[word] += 1
  14.  
  15. for sym in wordsToExclude:
  16. if sym in c:
  17. c.pop(sym)
  18.  
  19. most_common = c.most_common()
  20. most_frequent = []
  21. if not len(most_common):
  22. return []
  23. max_n = most_common[0][1]
  24. most_frequent.append(most_common[0][0])
  25. for i in range(1, len(most_common)):
  26. if most_common[i][1] < max_n:
  27. return most_frequent
  28. most_frequent.append(most_common[i][0])
  29. return most_frequent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement