jieba中文词频统计
时间: 2024-06-08 13:03:05 浏览: 6
jieba是一款中文分词工具,可以将中文文本按照词语进行划分。通过使用jieba的分词功能,我们可以实现中文词频统计。具体步骤如下:
1.安装jieba库:在终端中输入pip install jieba进行安装
2.导入jieba库:在Python文件中输入import jieba
3.读取中文文本:使用Python的文件读取功能,将需要统计词频的中文文本读入程序中。
4.分词:使用jieba库的cut函数对中文文本进行分词处理,将每个单词存入一个列表中。
5.统计词频:对于每个单词,使用Python的字典数据类型统计其出现次数。
6.输出结果:将统计结果按照词频从大到小排序,并输出到文件或者屏幕上。
下面是一段实现中文词频统计的示例代码:
```
import jieba
# 读取中文文本
with open('text.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 分词
words = jieba.cut(text)
# 统计词频
word_counts = {}
for word in words:
if len(word) == 1: # 排除单个汉字
continue
else:
word_counts[word] = word_counts.get(word, 0) + 1
# 输出结果
sorted_word_counts = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)
for item in sorted_word_counts:
print(item, item)
```
相关推荐
![blend](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![blend](https://img-home.csdnimg.cn/images/20210720083646.png)
![blend](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)