利用python的词云第三方库Wordcloud,结合jieba的分词功能构建《三国演义》的词云效果
时间: 2024-05-13 21:15:36 浏览: 95
python 基于 wordcloud + jieba + matplotlib 生成词云
5星 · 资源好评率100%
好的,下面是实现步骤:
1. 安装Wordcloud和jieba库
```python
!pip install wordcloud
!pip install jieba
```
2. 读取《三国演义》文本
```python
with open('sanguo.txt', 'r', encoding='utf-8') as f:
text = f.read()
```
3. 对文本进行分词
```python
import jieba
words = jieba.cut(text)
words = ' '.join(words)
```
4. 生成词云图
```python
from wordcloud import WordCloud
wc = WordCloud(font_path='msyh.ttc', background_color='white', width=800, height=600, max_words=2000)
wc.generate(words)
import matplotlib.pyplot as plt
plt.imshow(wc)
plt.axis('off')
plt.show()
```
完整代码如下:
```python
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 读取文本
with open('sanguo.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 分词
words = jieba.cut(text)
words = ' '.join(words)
# 生成词云
wc = WordCloud(font_path='msyh.ttc', background_color='white', width=800, height=600, max_words=2000)
wc.generate(words)
# 显示词云
plt.imshow(wc)
plt.axis('off')
plt.show()
```
注意,需要将文本保存为UTF-8编码的文件,否则会出现乱码。此外,需要下载中文字体文件msyh.ttc,可以从Windows系统的C:\Windows\Fonts目录下复制到当前目录下。
阅读全文