forked from p46318075/CodePattern
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.
58 lines
1.5 KiB
58 lines
1.5 KiB
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()
|
|
|
|
|
|
'''
|
|
函数输入参数调用后,你的马上接住返回值
|
|
类输入参数后实例化后,你可以需要的时候去访问你需要的数据(实例属性)
|
|
''' |