You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
from collections import Counter
|
|
|
|
from cppy.cp_util import *
|
|
|
|
|
|
|
|
|
|
|
|
class DataStorageManager:
|
|
|
|
""" 数据模型 """
|
|
|
|
def __init__(self, path_to_file):
|
|
|
|
self._data = re_split( read_file(path_to_file) )
|
|
|
|
|
|
|
|
def words(self):
|
|
|
|
return self._data
|
|
|
|
|
|
|
|
|
|
|
|
class StopWordManager:
|
|
|
|
""" 停用词模型 """
|
|
|
|
def __init__(self):
|
|
|
|
self._stop_words = get_stopwords()
|
|
|
|
|
|
|
|
def is_stop_word(self, word):
|
|
|
|
return word in self._stop_words
|
|
|
|
|
|
|
|
|
|
|
|
class WordFrequencyManager:
|
|
|
|
""" 词频模型 """
|
|
|
|
def __init__(self):
|
|
|
|
self._word_freqs = Counter()
|
|
|
|
|
|
|
|
def increment_count(self, word):
|
|
|
|
self._word_freqs[word] += 1
|
|
|
|
|
|
|
|
def sorted(self):
|
|
|
|
return self._word_freqs.most_common()
|
|
|
|
|
|
|
|
|
|
|
|
class WordFrequencyController:
|
|
|
|
def __init__(self, path_to_file):
|
|
|
|
self._storage_manager = DataStorageManager(path_to_file)
|
|
|
|
self._stop_word_manager = StopWordManager()
|
|
|
|
self._word_freq_manager = WordFrequencyManager()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
for w in self._storage_manager.words():
|
|
|
|
if not self._stop_word_manager.is_stop_word(w):
|
|
|
|
self._word_freq_manager.increment_count(w)
|
|
|
|
|
|
|
|
word_freqs = self._word_freq_manager.sorted()
|
|
|
|
print_word_freqs(word_freqs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
WordFrequencyController(testfilepath).run()
|