我尝试在单独的文本文件中使用停用词清理文本文件后在python中创建 wordcloud
我得到了所需的结果,即主要用于文本文件但无法理解 wordcloud
的单词我的代码:
import collections
from wordcloud import WordCloud
import matplotlib.pyplot as plt
file = open('example.txt', encoding = 'utf8' )
stopwords = set(line.strip() for line in open('stopwords'))
wordcount = {}
for word in file.read().split():
word = word.lower()
word = word.replace(".","")
word = word.replace(",","")
word = word.replace("\"","")
word = word.replace("“","")
if word not in stopwords:
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
d = collections.Counter(wordcount)
for word, count in d.most_common(10):
print(word , ":", count)
wordcloud = WordCloud(background_color='white',
width=1200,
height=1000
).generate((d.most_common(10)))
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
我正在TypeError: expected string or buffer
但是当我使用.generate(str(d.most_common(10)))
形成的wordcloud在几个单词后显示撇号(')符号
使用Python3,Jupyter-Notebook