import jieba import numpy import numpy as np from PIL import Image from wordcloud import WordCloud, STOPWORDS import matplotlib.pyplot as plt with open("comments.txt","r",encoding="utf-8") as f: comments = f.read() 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) mask = np.array(Image.open("mask.png")) cloud = WordCloud( font_path="C:\Windows\Fonts\simsun.ttc", background_color="white", mask=mask ).generate(" ".join(filtered_words)) cloud.to_file("词云.png") print("词云绘制成功")这个代码怎么运行错误
时间: 2024-02-14 11:06:44 浏览: 165
这段代码的错误可能是缩进不正确。正确的代码应该是这样的:
```
import jieba
import numpy
import numpy as np
from PIL import Image
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
with open("comments.txt","r",encoding="utf-8") as f:
comments = f.read()
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)
mask = np.array(Image.open("mask.png"))
cloud = WordCloud(
font_path="C:\Windows\Fonts\simsun.ttc",
background_color="white",
mask=mask
).generate(" ".join(filtered_words))
cloud.to_file("词云.png")
print("词云绘制成功")
```
请检查一下你的代码是否与这段代码一致。
阅读全文