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.

35 lines
1.1 KiB

import cppy.cp_util as util
##########################################
# 工具类
##########################################
class wordsTaskHandler:
def handle(self,path_to_file):
return util.extract_file_words(path_to_file)
class frequenciesTaskHandler:
def handle(self,word_list):
return util.get_frequencies(word_list)
class sortTaskHandler:
def handle(self,word_freq):
return util.sort_dict(word_freq)
##########################################
# 应用框架
##########################################
def handle_task(task_type,*args):
handler_class_name = f"{task_type}TaskHandler" # 构建处理器类名
handler_class = globals().get(handler_class_name)
if handler_class:
handler = handler_class() # 实例化处理器类
return handler.handle(*args) # 调用处理方法
else:
print(f"No found for task type: {task_type}")
word_list = handle_task("words",util.testfilepath)
word_freq = handle_task("frequencies",word_list)
word_sort = handle_task("sort",word_freq)
util.print_word_freqs(word_sort)