您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用Python的wordcloud库时,为什么不将停用词排除在词云之外?

使用Python的wordcloud库时,为什么不将停用词排除在词云之外?

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我得到:

在此处输入图片说明

随着collocations=False我得到:

在此处输入图片说明

码:

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()
python 2022/1/1 18:31:21 有259人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶