安装jieba
库
pip install jieba
文件"demo.txt"是《沉默的羔羊》中文版内容,请读入文件内容,分词后输出长度大于2且最多的单词。
如果存在多个单词出现频率一致,请输出按照Unicode排序后最大的单词。
注意:要读入的文件已放入当前目录下,源文件中直接读取此文件即可。
import jieba
from collections import Counter
# 读取文件内容
file_path = "demo.txt"
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
# 分词
words = list(jieba.cut(content))
# 过滤长度大于2的单词
filtered_words = [word for word in words if len(word) > 2]
# 计算词频
word_frequency = Counter(filtered_words)
# 找到最多的单词
most_common_words = word_frequency.most_common()
# 如果存在多个单词出现频率一致,请按照Unicode排序
most_common_words.sort(key=lambda x: (x[1], x[0]), reverse=True)
# 输出结果
if most_common_words:
max_word, max_count = most_common_words[0]
print(f"{max_word}")