基于Python的分词库(例如jieba)实现词频统计
时间: 2024-05-05 17:15:12 浏览: 67
使用jieba分词库实现词频统计的步骤如下:
1. 安装jieba分词库,可以使用pip命令进行安装:
```
pip install jieba
```
2. 导入jieba库,使用cut方法进行分词:
```python
import jieba
text = "今天天气真好,出去玩吧!"
words = jieba.cut(text)
```
3. 遍历分词结果,使用字典统计每个词出现的次数:
```python
word_freq = {}
for word in words:
if word in word_freq:
word_freq[word] += 1
else:
word_freq[word] = 1
```
4. 打印统计结果:
```python
for word, freq in word_freq.items():
print(word, freq)
```
完整代码如下:
```python
import jieba
text = "今天天气真好,出去玩吧!"
words = jieba.cut(text)
word_freq = {}
for word in words:
if word in word_freq:
word_freq[word] += 1
else:
word_freq[word] = 1
for word, freq in word_freq.items():
print(word, freq)
```
输出结果:
```
今天 1
天气 1
真好 1
, 1
出去 1
玩吧 1
! 1
```
阅读全文