Wordcloud的默认设置是collocations=True
,因此两个相邻单词的常用短语都包含在云中- 对于您的问题,重要的是,搭配使用时,停用词的去除方式有所不同,例如“谢谢”是有效的搭配,并且可能即使默认单词中的“ you”也会出现在生成的云中。仅包含停用词的 并置会被 删除。
听起来不合理的理由是,如果在构建搭配列表之前删除停用词,则例如“非常感谢”将提供“非常感谢”作为搭配,我当然不希望这样。
因此,为了使您的停用词按预期运行,即云中完全没有停用词出现,可以这样使用collocations=False
:
my_wordcloud = WordCloud(
stopwords=my_stopwords,
background_color='white',
collocations=False,
max_words=10).generate(all_tweets_as_one_string)
更新:
这在源代码中都是可见的:-)
例如,使用默认值,collocations=True
我得到:
码:
from wordcloud import WordCloud
from matplotlib import pyplot as plt
text = "The bear sat with the cat. They were good friends. " + \
"My friend is a bit bear like. He's lovely. The bear, the cat, the dog and me were all sat " + \
"there enjoying the view. You should have seen it. The view was absolutely lovely. " + \
"It was such a lovely day. The bear was loving it too."
cloud = WordCloud(collocations=False,
background_color='white',
max_words=10).generate(text)
plt.imshow(cloud, interpolation='bilinear')
plt.axis('off')
plt.show()