Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. from collections import Counter
  2.  
  3. class ArticleHashtags:
  4. def __init__(self, text):
  5. self.text = text
  6.  
  7. def get_most_common_words(self, n=5, text_min_lenth=5):
  8. """
  9. Returns 5 most common words in string.
  10. """
  11.  
  12. words = list(
  13. filter(
  14. None,
  15. [x.strip().lower().strip(',"') for x in self.text.split() if len(x) >= text_min_lenth if x]
  16. )
  17. )
  18.  
  19. most_common_words = dict(Counter(words).most_common(n))
  20.  
  21. return list(map(lambda x: '#' + x, most_common_words))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement