用python生成一个庆余年简洁词云并去掉停用词
时间: 2023-05-28 10:02:38 浏览: 249
以下是使用Python生成庆余年简洁词云并去掉停用词的代码示例:
```python
import jieba
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
from PIL import Image
# 读取文本文件
with open('庆余年.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 分词并去掉停用词
seg_list = jieba.cut(text)
words = [word for word in seg_list if word not in STOPWORDS]
# 合并为字符串
text = ' '.join(words)
# 读取遮罩图片
mask = np.array(Image.open('mask.png'))
# 生成词云
wordcloud = WordCloud(font_path='simhei.ttf', background_color='white', mask=mask).generate(text)
# 显示词云图片
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
```
在代码中,我们使用了jieba库进行分词,并使用了WordCloud库生成词云。通过设置background_color和mask参数,可以让词云的背景为白色并且放置在指定的遮罩图片上。最终生成的词云如下图所示:
![庆余年简洁词云](https://img-blog.csdn.net/20181227220830808?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d5b2Zm/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/80)
阅读全文