【问题标题】:How to add extra stop words in addition to default stopwords in wordcloud?除了 wordcloud 中的默认停用词之外,如何添加额外的停用词?
【发布时间】:2019-05-28 13:49:27
【问题描述】:

我想将某些单词添加到 wordcloud 中使用的默认停用词列表中。当前代码:

all_text = " ".join(rev for rev in twitter_clean.text)
stop_words = ["https", "co", "RT"]
wordcloud = WordCloud(stopwords = stop_words, background_color="white").generate(all_text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

当我使用自定义 stop_words 变量时,诸如“is”、“was”和“the”之类的词都被解释并显示为高频词。但是,当我使用默认停用词列表(没有停用词参数)时,还有许多其他词显示为非常频繁。如何将我的自定义 stop_words 变量与默认停用词列表一起添加到我的 wordcloud?

【问题讨论】:

    标签: python matplotlib data-analysis stop-words word-cloud


    【解决方案1】:

    只需使用from wordcloud import STOPWORDS 获取原始停用词列表,然后附加您的列表。赞这个[STOPWORDS.add(n) for n in custon_stop_words]

    【讨论】:

      【解决方案2】:

      只需将您的列表附加到内置的 STOPWORDS 列表中:

      来自 wordcloud 文档:

      停用词:一组字符串或无。会被淘汰的话。 如果为 None,将使用内置的 STOPWORDS 列表。

      因此,您可以简单地将 STOPWORDS 附加到您的自定义列表中并使用它

      all_text = " ".join(rev for rev in twitter_clean.text)
      stop_words = ["https", "co", "RT"] + list(STOPWORDS)
      wordcloud = WordCloud(stopwords = stop_words, background_color="white").generate(all_text)
      plt.imshow(wordcloud, interpolation='bilinear')
      plt.axis("off")
      plt.show()
      

      【讨论】:

      • 将其转换为列表或将自定义列表转换​​为集合。修改了代码。
      【解决方案3】:
      stopwords.update(["https", "co"])
      

      【讨论】:

      • 仅代码答案不是很有用,因为它们没有解释为什么代码解决了问题。请为您的答案添加解释。
      【解决方案4】:

      通过将您的自定义停用词列表添加到 wordcloud.STOPWORDS 集

      wordcloud 内置的 STOPWORDS 是一个 python 集。

      from wordcloud import STOPWORDS
      
      print(type(STOPWORDS))
      

      输出

      <class 'set'>
      

      我们可以使用set.update() 添加到这个集合中,如图所示:

      stop_words = STOPWORDS.update(["https", "co", "RT"])
      

      现在更新 wordcloud 中的停用词

      wordcloud = WordCloud(stopwords = stop_words, background_color="white").generate(all_text)
      

      【讨论】:

        猜你喜欢
        • 2014-02-03
        • 1970-01-01
        • 1970-01-01
        • 2021-11-13
        • 2018-05-26
        • 1970-01-01
        • 1970-01-01
        • 2013-07-28
        • 2021-08-07
        相关资源
        最近更新 更多