用Python写一个腾讯游戏评价的词云
时间: 2023-11-29 18:06:21 浏览: 43
好的,下面是一个简单的 Python 代码,用于生成腾讯游戏评价的词云:
首先,需要安装 `jieba`、`wordcloud` 和 `matplotlib` 库。可以使用以下命令来安装:
```
pip install jieba wordcloud matplotlib
```
然后,准备好需要分析的文本,可以使用腾讯游戏的评价文本作为例子。将文本保存在 `comments.txt` 文件中。
接下来是 Python 代码:
```python
import jieba
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
# 读取文本内容
with open('comments.txt', 'r', encoding='utf-8') as f:
comments = f.read()
# 使用 jieba 分词
words = jieba.cut(comments)
# 过滤停用词
stopwords = set(STOPWORDS)
stopwords.add('腾讯')
stopwords.add('游戏')
filtered_words = []
for word in words:
if word not in stopwords:
filtered_words.append(word)
# 生成词云
wc = WordCloud(
font_path='msyh.ttc', # 设置字体
background_color='white', # 设置背景色
max_words=200, # 设置最大显示的词数
stopwords=stopwords, # 设置停用词
)
wc.generate(' '.join(filtered_words))
# 显示词云
plt.imshow(wc)
plt.axis('off')
plt.show()
# 保存词云图片
wc.to_file('wordcloud.png')
```
这段代码首先读取 `comments.txt` 文件中的文本内容,然后使用 `jieba` 库对文本进行分词,并过滤掉一些常见的停用词。接着,使用 `wordcloud` 库生成词云,并使用 `matplotlib` 库显示和保存词云图片。
代码中可以自定义一些参数,例如设置字体、背景色、最大显示的词数等等。如果需要更多的自定义,可以查看 `wordcloud` 和 `matplotlib` 库的文档。
希望这个例子能够帮助你生成自己想要的腾讯游戏评价词云!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)