【问题标题】:WordCloud is not removing custom stopwordsWordCloud 不会删除自定义停用词
【发布时间】:2021-08-07 07:26:29
【问题描述】:

我正在尝试添加要从我的词云中删除的停用词。似乎突然之间,我的其他停用词没有被添加。它以前工作过。

我已将问题归结为此处显示的内容以及循环中的第一个词云。您可以在顶部看到“产品”一词仍然存在,即使我将它添加到停用词列表中。其他两个停用词已正确删除。我将搭配设置为 False。

我尝试使用版本 1.5.0 和 1.6.0

from wordcloud import WordCloud, STOPWORDS
import pandas as pd
import collections
import matplotlib.pyplot as plt

for i in range(20):
  print(i)
  wordcloud = WordCloud(stopwords=["product", "and", "the"], background_color='white', collocations=False).generate(clusterStrings[i])
  # Display the generated image:
  plt.imshow(wordcloud, interpolation='bilinear')
  plt.axis("off")
  plt.show()

【问题讨论】:

    标签: python word-cloud stop-words


    【解决方案1】:

    您正在每个循环上创建一个新实例,并且您正在替换而不是添加其他停用词。尝试创建 wc 并将停用词添加到循环外的已知 STOPWORDS

    from wordcloud import WordCloud, STOPWORDS
    import pandas as pd
    import collections
    import matplotlib.pyplot as plt
    
    ...
    # create the instance only once and add stopwords
    stopwords = set(STOPWORDS)
    stopwords.add(["product", "and", "the"])
    
    wordcloud = WordCloud(stopwords=stopwords, background_color='white', collocations=False)
    
    for i in range(20):
      print(i)
      wordcloud.generate(clusterStrings[i])
      # Display the generated image:
      plt.imshow(wordcloud, interpolation='bilinear')
      plt.axis("off")
      plt.show()
    

    【讨论】:

    • 我最初是在添加原始停用词。我只是将代码更改为此,以显示三个显式停用词之一仍在显示。我尝试了您的代码,但仅在集合中添加了“产品”。但“产品”仍然出现。
    • 他们很奇怪。尝试在 clusterString 中搜索“产品”并检查是否有隐藏空间,例如“产品 ”。另一个,用产品获取clusterString并检查"product" in clusterString[x]
    【解决方案2】:

    试试这个功能: (参考)

    from wordcloud import WordCloud, STOPWORDS
    import matplotlib.pyplot as plt
    
    def plot_wordcloud(text, mask=None, max_words=200, max_font_size=100, figure_size=(24.0,16.0), 
                       title = None, title_size=40, image_color=False):
        stopwords = set(STOPWORDS)
        more_stopwords = {'one', 'br', 'Po', 'th', 'sayi', 'fo', 'Unknown'}
        stopwords = stopwords.union(more_stopwords)
    
        wordcloud = WordCloud(background_color='black',
                        stopwords = stopwords,
                        max_words = max_words,
                        max_font_size = max_font_size, 
                        random_state = 42,
                        width=800, 
                        height=400,
                        mask = mask)
        wordcloud.generate(str(text))
        
        plt.figure(figsize=figure_size)
        if image_color:
            image_colors = ImageColorGenerator(mask);
            plt.imshow(wordcloud.recolor(color_func=image_colors), interpolation="bilinear");
            plt.title(title, fontdict={'size': title_size,  
                                      'verticalalignment': 'bottom'})
        else:
            plt.imshow(wordcloud);
            plt.title(title, fontdict={'size': title_size, 'color': 'black', 
                                      'verticalalignment': 'bottom'})
        plt.axis('off');
        plt.tight_layout()  
        
    plot_wordcloud(train_df["Col_name"], title="Word Cloud of ...")
    

    【讨论】:

    • 尝试了这段代码,但在将其添加到停用词后仍会显示“产品”一词。
    • 另外,请确保对文件进行标记化,以便逐字分解。
    • 使用split()' '.join() 这样它会删除中间的任何不必要的空格
    猜你喜欢
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2015-05-30
    • 2018-10-13
    • 1970-01-01
    相关资源
    最近更新 更多