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.
34 lines
968 B
34 lines
968 B
from collections import Counter
|
|
import cppy.cp_util as util
|
|
|
|
def word_frequency( top_n=10 ):
|
|
def decorator(func):
|
|
def wrapper(*args, **kwargs):
|
|
# 执行被装饰的函数
|
|
result = func(*args, **kwargs)
|
|
|
|
# 初始化词频计数器
|
|
word_counts = Counter()
|
|
|
|
# 分词并计数
|
|
for word in util.extract_str_words(result):
|
|
word_counts[word] += 1
|
|
|
|
# 输出所有词的频率最高的n个词
|
|
most_common = word_counts.most_common(top_n)
|
|
util.print_word_freqs( most_common )
|
|
|
|
return result
|
|
|
|
return wrapper
|
|
return decorator
|
|
|
|
|
|
# 使用装饰器
|
|
@word_frequency( top_n=10 )
|
|
def read_file(file_path):
|
|
with open(file_path, 'r', encoding='utf-8') as file:
|
|
return file.read()
|
|
|
|
|
|
read_file( util.testfilepath ) |