from collections import Counter
import jieba
读取文本文件
with open(‘example.txt’, ‘r’, encoding=‘utf-8’) as f:
text = f.read()
使用jieba分词
words = jieba.cut(text)
统计词频并排序
word_counts = Counter(words)
top_counts = word_counts.most_common(10)
输出前10个热词和它们的词频
print(“前10个热词:”)
for word, count in top_counts:
print(f"{word}: {count}")