Every line of 'most frequent word in an array of strings' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
37 def topKFrequent(self, words: List[str], k: int) -> List[str]: 38 counts = collections.Counter(words) 39 40 h = [] 41 heapq.heapify(h) 42 for word, count in counts.items(): 43 heapq.heappush(h, Element(word, count)) 44 if len(h) > k: 45 heapq.heappop(h) 46 47 ans = [] 48 while h: 49 ans.append(heapq.heappop(h).word) 50 return ans[::-1]
43 def topKFrequent1(self, words, k): 44 # beats 77.28% 45 d = {} 46 for word in words: 47 d[word] = d.get(word, 0) + 1 48 49 ret = sorted(d, key=lambda word: (-d[word], word)) 50 51 return ret[:k]
6 def get_word_frequencies(text, words_n=10, lang='german'): 7 default_stopwords = set(nltk.corpus.stopwords.words(lang)) 8 words = nltk.tokenize.word_tokenize(text) 9 words = [word for word in words if len(word) > 1] 10 words = [word for word in words if not word.isnumeric()] 11 words = [word.lower() for word in words] 12 words = [word for word in words if word not in default_stopwords] 13 14 fdist = nltk.FreqDist(words) 15 return fdist.most_common(words_n)