Skip to content

Instantly share code, notes, and snippets.

@miodeqqq
Created September 10, 2019 09:54
Show Gist options
  • Save miodeqqq/80d48d892ac755a423e95049e20f9693 to your computer and use it in GitHub Desktop.
Save miodeqqq/80d48d892ac755a423e95049e20f9693 to your computer and use it in GitHub Desktop.
from collections import Counter
class ArticleHashtags:
def __init__(self, text):
self.text = text
def get_most_common_words(self, n=5, text_min_lenth=5):
"""
Returns 5 most common words in string.
"""
words = list(
filter(
None,
[x.strip().lower().strip(',"') for x in self.text.split() if len(x) >= text_min_lenth if x]
)
)
most_common_words = dict(Counter(words).most_common(n))
return list(map(lambda x: '#' + x, most_common_words))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment