对中文text去除停用词后进行词云可视化
时间: 2023-08-12 16:06:35 浏览: 42
好的,您可以使用Python中的jieba库对中文文本进行分词,并使用stopwords库去除停用词。然后,您可以使用wordcloud库生成词云图像。下面是一个简单的示例代码:
```python
import jieba
from wordcloud import WordCloud
from collections import Counter
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from os import path
from imageio import imread
from stopwords import stopwords
# 读取文本文件
with open('text.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 分词
words = jieba.cut(text)
# 去除停用词
words = [word for word in words if word not in stopwords]
# 统计词频
word_counts = Counter(words)
# 生成词云
mask = np.array(Image.open('mask.png'))
wc = WordCloud(background_color='white', mask=mask, font_path='font.ttf')
wc.generate_from_frequencies(word_counts)
# 显示词云
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.show()
```
在代码中,您需要将`text.txt`替换为您的文本文件路径,将`mask.png`替换为您的词云形状图片路径,将`font.ttf`替换为您的字体文件路径。您还可以根据需要调整其他参数,例如词云颜色、字体大小等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)
![](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)