在Python中进行中文词频统计,通常需要使用`jieba`库进行分词,然后使用`Counter`或字典来统计词频。以下是一个简单的步骤说明:
1. 安装`jieba`库(如果尚未安装):
pip install jieba
2. 读取中文文本文件:
with open('text.txt', 'r', encoding='utf-8') as file:text = file.read()
3. 使用`jieba`进行分词:
words = jieba.lcut(text)
4. 排除长度小于2的词,并统计词频:
word_count = {}for word in words:if len(word) > 1:word_count[word] = word_count.get(word, 0) + 1

5. 找出出现频率最高的前N个词:
top_n = 10top_words = sorted(word_count.items(), key=lambda x: x, reverse=True)[:top_n]
6. 打印结果:
for word, count in top_words:print(f'{word}: {count}')
7. (可选)可视化词频统计结果:
import matplotlib.pyplot as pltx = [word for word in top_words]y = [word for word in top_words]plt.bar(x, y)plt.show()
以上步骤展示了如何使用Python进行中文文本的词频统计,并可选地通过图表形式直观展示结果。
