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.
45 lines
1.2 KiB
45 lines
1.2 KiB
import sys, collections
|
|
from cppy.cp_util import *
|
|
|
|
class WordFrequenciesModel:
|
|
""" 模型:数据 """
|
|
def __init__(self, path_to_file):
|
|
self.update(path_to_file)
|
|
|
|
def update(self, path_to_file):
|
|
try:
|
|
self.freqs = collections.Counter( extract_file_words(path_to_file) )
|
|
except IOError:
|
|
print("File not found")
|
|
self.freqs = {}
|
|
|
|
|
|
class WordFrequenciesView:
|
|
""" 视图:数据展现 """
|
|
def __init__(self, model):
|
|
self._model = model
|
|
|
|
def render(self):
|
|
sorted_freqs = sort_dict(self._model.freqs)
|
|
print_word_freqs(sorted_freqs)
|
|
|
|
|
|
class WordFrequencyController:
|
|
""" 控制:操作逻辑 """
|
|
def __init__(self, model, view):
|
|
self._model, self._view = model, view
|
|
view.render()
|
|
|
|
def run(self):
|
|
while True:
|
|
print("Enter the file path (or 'q' to quit): ", file=sys.stderr, flush=True)
|
|
filename = sys.stdin.readline().strip()
|
|
if filename.lower() == 'q': break
|
|
self._model.update(filename)
|
|
self._view.render()
|
|
|
|
|
|
m = WordFrequenciesModel( testfilepath )
|
|
v = WordFrequenciesView(m)
|
|
c = WordFrequencyController(m, v)
|
|
c.run() |