|
| 1 | +""" |
| 2 | +Finds the top K most frequent words from the provided word list. |
| 3 | +
|
| 4 | +This implementation aims to show how to solve the problem using the Heap class |
| 5 | +already present in this repository. |
| 6 | +Computing order statistics is, in fact, a typical usage of heaps. |
| 7 | +
|
| 8 | +This is mostly shown for educational purposes, since the problem can be solved |
| 9 | +in a few lines using collections.Counter from the Python standard library: |
| 10 | +
|
| 11 | +from collections import Counter |
| 12 | +def top_k_frequent_words(words, k_value): |
| 13 | + return [x[0] for x in Counter(words).most_common(k_value)] |
| 14 | +""" |
| 15 | + |
| 16 | + |
| 17 | +from collections import Counter |
| 18 | +from functools import total_ordering |
| 19 | + |
| 20 | +from data_structures.heap.heap import Heap |
| 21 | + |
| 22 | + |
| 23 | +@total_ordering |
| 24 | +class WordCount: |
| 25 | + def __init__(self, word: str, count: int) -> None: |
| 26 | + self.word = word |
| 27 | + self.count = count |
| 28 | + |
| 29 | + def __eq__(self, other: object) -> bool: |
| 30 | + """ |
| 31 | + >>> WordCount('a', 1).__eq__(WordCount('b', 1)) |
| 32 | + True |
| 33 | + >>> WordCount('a', 1).__eq__(WordCount('a', 1)) |
| 34 | + True |
| 35 | + >>> WordCount('a', 1).__eq__(WordCount('a', 2)) |
| 36 | + False |
| 37 | + >>> WordCount('a', 1).__eq__(WordCount('b', 2)) |
| 38 | + False |
| 39 | + >>> WordCount('a', 1).__eq__(1) |
| 40 | + NotImplemented |
| 41 | + """ |
| 42 | + if not isinstance(other, WordCount): |
| 43 | + return NotImplemented |
| 44 | + return self.count == other.count |
| 45 | + |
| 46 | + def __lt__(self, other: object) -> bool: |
| 47 | + """ |
| 48 | + >>> WordCount('a', 1).__lt__(WordCount('b', 1)) |
| 49 | + False |
| 50 | + >>> WordCount('a', 1).__lt__(WordCount('a', 1)) |
| 51 | + False |
| 52 | + >>> WordCount('a', 1).__lt__(WordCount('a', 2)) |
| 53 | + True |
| 54 | + >>> WordCount('a', 1).__lt__(WordCount('b', 2)) |
| 55 | + True |
| 56 | + >>> WordCount('a', 2).__lt__(WordCount('a', 1)) |
| 57 | + False |
| 58 | + >>> WordCount('a', 2).__lt__(WordCount('b', 1)) |
| 59 | + False |
| 60 | + >>> WordCount('a', 1).__lt__(1) |
| 61 | + NotImplemented |
| 62 | + """ |
| 63 | + if not isinstance(other, WordCount): |
| 64 | + return NotImplemented |
| 65 | + return self.count < other.count |
| 66 | + |
| 67 | + |
| 68 | +def top_k_frequent_words(words: list[str], k_value: int) -> list[str]: |
| 69 | + """ |
| 70 | + Returns the `k_value` most frequently occurring words, |
| 71 | + in non-increasing order of occurrence. |
| 72 | + In this context, a word is defined as an element in the provided list. |
| 73 | +
|
| 74 | + In case `k_value` is greater than the number of distinct words, a value of k equal |
| 75 | + to the number of distinct words will be considered, instead. |
| 76 | +
|
| 77 | + >>> top_k_frequent_words(['a', 'b', 'c', 'a', 'c', 'c'], 3) |
| 78 | + ['c', 'a', 'b'] |
| 79 | + >>> top_k_frequent_words(['a', 'b', 'c', 'a', 'c', 'c'], 2) |
| 80 | + ['c', 'a'] |
| 81 | + >>> top_k_frequent_words(['a', 'b', 'c', 'a', 'c', 'c'], 1) |
| 82 | + ['c'] |
| 83 | + >>> top_k_frequent_words(['a', 'b', 'c', 'a', 'c', 'c'], 0) |
| 84 | + [] |
| 85 | + >>> top_k_frequent_words([], 1) |
| 86 | + [] |
| 87 | + >>> top_k_frequent_words(['a', 'a'], 2) |
| 88 | + ['a'] |
| 89 | + """ |
| 90 | + heap: Heap[WordCount] = Heap() |
| 91 | + count_by_word = Counter(words) |
| 92 | + heap.build_max_heap( |
| 93 | + [WordCount(word, count) for word, count in count_by_word.items()] |
| 94 | + ) |
| 95 | + return [heap.extract_max().word for _ in range(min(k_value, len(count_by_word)))] |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + import doctest |
| 100 | + |
| 101 | + doctest.testmod() |
0 commit comments