词云图python去除停用词
时间: 2023-11-22 09:48:25 浏览: 276
在Python中,我们可以使用第三方库nltk来去除停用词并生成词云图。具体步骤如下:
1.安装nltk库
```python
!pip install nltk
```
2.导入nltk库并下载停用词
```python
import nltk
nltk.download('stopwords')
```
3.导入需要使用的库
```python
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from nltk.corpus import stopwords
```
4.读取文本文件并去除停用词
```python
with open('text.txt', 'r', encoding='utf-8') as f:
text = f.read()
stop_words = set(stopwords.words('english')) # 设置停用词
words = text.split() # 分词
words = [word for word in words if word.lower() not in stop_words] # 去除停用词
```
5.生成词云图
```python
wordcloud = WordCloud(width=800, height=800, background_color='white').generate(' '.join(words))
plt.figure(figsize=(8, 8), facecolor=None)
plt.imshow(wordcloud)
plt.axis('off')
plt.tight_layout(pad=0)
plt.show()
```
阅读全文