jieba提取高频词,生成文件是空的

这个代码哪里有错误吗,生成的count文件为什么是空的,是原文件有错误吗
绘制词云图的时候怎么改错误呀

img

img

img

img

img

img

  1. 错误原因

    img

  2. 没数据的原因,你看看分词那个地方是否正确有结果
words=jieba.lcut(s)
print(words)

词云那里报错是因为你变量写错了。txt应该是text。w.generate(text)

至于保存的文件是空的,建议你打印一下变量words_list,看是否有值。再看哪里出错了。

有两个情况存在,
1.txt变量没定义,
2.txt定义了,但没有权限写

img

img

这是我做的词云《三国演义》的.
from collections import Counter

import jieba
import imageio
import wordcloud as wc
import matplotlib.pyplot as plt

with open("三国演义gbk.txt", encoding='gbk') as f:
    content = f.read()
with open("hit_stopwords.txt",encoding= 'utf-8') as f:
    stopword = [i.strip() for i in f.readlines()]
res = jieba.lcut(content)
res = Counter(res)


result = {k: v for k,v in res.items() if k not in stopword and len(k) > 1 and v > 1}
res = sorted(result.items(), key=lambda x: x[1], reverse=True)
result = [i[0] for i in res][:200]
print(result)
mask =imageio.v2.imread("fivestar.png")

word = wc.WordCloud(background_color='white', 
    width=800,
    height=800,
    font_path='msyh.ttc',# simhei.ttf   c:\windows\fonts\msyhl.ttc
    mask=mask, 
    #stopwords=wc.STOPWORDS
    )
word.generate(' '.join(result))
word.to_file("exap.png") #生成的png图

plt.subplots(figsize=(12, 8))
plt.imshow(word) #将生成的图显示出来
plt.show()


img

img

img

是text,不是txt,报错都给你提示了,说这个变量没定义,你明显写错了呀