import jieba
# 读取文本文件
path = "红楼梦.txt"
file = open(path, "r", encoding="GB2312",errors="ignore")
text = file.read()
file.close()
# 使用jieba分词
words = jieba.lcut(text)
# 统计词频
counts = {}
for word in words:
# 过滤掉长度为1的词语
if len(word) == 1:
continue
# 更新字典中的词频
counts[word] = counts.get(word, 0) + 1
# 对字典中的键值对进行排序
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
# 输出前20个高频词语
for i in range(20):
word, count = items[i]
print(f"{word:<10}{count:>5}")
标签:jieba,word,items,file,counts,分词 From: https://www.cnblogs.com/antea/p/17920181.html