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.
92 lines
2.3 KiB
92 lines
2.3 KiB
9 months ago
|
import abc, re
|
||
|
from cppy.cp_util import *
|
||
|
|
||
|
#
|
||
|
# 接口
|
||
|
#
|
||
|
class IDataStorage (metaclass=abc.ABCMeta):
|
||
|
@abc.abstractmethod
|
||
|
def words(self):
|
||
|
pass
|
||
|
|
||
|
class IStopWordFilter (metaclass=abc.ABCMeta):
|
||
|
@abc.abstractmethod
|
||
|
def is_stop_word(self, word):
|
||
|
pass
|
||
|
|
||
|
class IWordFrequencyCounter(metaclass=abc.ABCMeta):
|
||
|
@abc.abstractmethod
|
||
|
def increment_count(self, word):
|
||
|
pass
|
||
|
|
||
|
@abc.abstractmethod
|
||
|
def sorted(self):
|
||
|
pass
|
||
|
|
||
|
#
|
||
|
# 类实现
|
||
|
#
|
||
|
class DataStorageManager1:
|
||
|
def __init__(self, path_to_file):
|
||
|
self._data = read_file(path_to_file)
|
||
|
self._data = re_split(self._data)
|
||
|
|
||
|
def words(self): return self._data
|
||
|
|
||
|
|
||
|
class DataStorageManager2:
|
||
|
def __init__(self, path_to_file):
|
||
|
self._data = read_file(path_to_file)
|
||
|
self._data = re.findall('[a-z]{2,}', self._data)
|
||
|
|
||
|
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 = {}
|
||
|
|
||
|
def increment_count(self, word):
|
||
|
self._word_freqs[word] = self._word_freqs.get(word, 0) + 1
|
||
|
|
||
|
def sorted(self):
|
||
|
return sort_dict( self._word_freqs )
|
||
|
|
||
|
|
||
|
#
|
||
|
# 注册到抽象接口:并非必要
|
||
|
#
|
||
|
# IDataStorage.register(subclass=DataStorageManager1)
|
||
|
IDataStorage.register(subclass=DataStorageManager2)
|
||
|
IStopWordFilter.register(subclass=StopWordManager)
|
||
|
IWordFrequencyCounter.register(subclass=WordFrequencyManager)
|
||
|
|
||
|
|
||
|
#
|
||
|
# 应用类
|
||
|
#
|
||
|
class WordFrequencyController:
|
||
|
def __init__(self, path_to_file):
|
||
|
# self._storage = DataStorageManager1(path_to_file)
|
||
|
self.storage = DataStorageManager2(path_to_file)
|
||
|
self.stop_word_manager = StopWordManager()
|
||
|
self.word_freq_counter = WordFrequencyManager()
|
||
|
|
||
|
def run(self): # 可以看做面向协议编程
|
||
|
for word in self.storage.words():
|
||
|
if not self.stop_word_manager.is_stop_word(word):
|
||
|
self.word_freq_counter.increment_count(word)
|
||
|
|
||
|
print_word_freqs( self.word_freq_counter.sorted() )
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
WordFrequencyController(testfilepath).run()
|