import jieba import wordcloud import imageio mask = imageio.imread('C:/Users/Febird/Desktop/1.png') #设定一个词云背景 with open('C:/Users/Febird/Desktop/傲世.txt', encoding='utf-8') as f: t = f.read() # 打开需要制作词云图的文件 ls = jieba.lcut(t) txt = " ".join(ls) # 将文章中的词组提出来 stopwords_file = open('C:/Users/Febird/Desktop/stopWord.txt', 'r', encoding='utf-8') stopwords = [(words.strip()) for words in stopwords_file.readlines()] w = wordcloud.WordCloud(width=4000, height=4000, stopwords=stopwords, font_path="msyh.ttc", colormap='hot', background_color='pink', mask=mask) # 定义一个词云 w.generate(txt) #生成词云 w.to_file(r'C:/Users/Febird/Desktop/test_3.png') #将词组变量txt导入词云对象w中并保存 添加代码 完成排名前五十个词的词云
时间: 2024-01-26 15:01:46 浏览: 324
Python错题本:from scipy.misc import imread 报错cannot import name imread 的解决方案
在代码中添加以下部分可以完成排名前五十个词的词云:
```
from collections import Counter
# 统计词频
words_count = Counter(ls)
# 获取词频前50的词及其频次
top50_words = words_count.most_common(50)
# 将排名前50的词转换为一个字符串
top50_str = ""
for word, count in top50_words:
top50_str += (word + " ") * count
# 生成词云
w.generate(top50_str)
```
完整代码如下:
```
import jieba
import wordcloud
import imageio
from collections import Counter
mask = imageio.imread('C:/Users/Febird/Desktop/1.png')
with open('C:/Users/Febird/Desktop/傲世.txt', encoding='utf-8') as f:
t = f.read()
ls = jieba.lcut(t)
txt = " ".join(ls)
stopwords_file = open('C:/Users/Febird/Desktop/stopWord.txt', 'r', encoding='utf-8')
stopwords = [(words.strip()) for words in stopwords_file.readlines()]
w = wordcloud.WordCloud(width=4000, height=4000,
stopwords=stopwords,
font_path="msyh.ttc",
colormap='hot',
background_color='pink',
mask=mask)
# 统计词频
words_count = Counter(ls)
# 获取词频前50的词及其频次
top50_words = words_count.most_common(50)
# 将排名前50的词转换为一个字符串
top50_str = ""
for word, count in top50_words:
top50_str += (word + " ") * count
# 生成词云
w.generate(top50_str)
w.to_file(r'C:/Users/Febird/Desktop/test_3.png')
```
阅读全文