WordCloud is not removing custom stopwords

Viewed 656

I am trying to add stopwords to be removed from my word clouds. It seems that all of a sudden, my additional stopwords are not being added. It was working before.

I have boiled down the problem to what is shown here along with the first word cloud from the loop. You can see at the top that the word "product" is still present even though I added it to the list of stopwords. The other two stopwords were properly removed. I have collocations set to False.

I tried with version 1.5.0 and 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()

enter image description here

2 Answers

You are creating a new instance on each loop, plus you are replacing instead of adding additional stopwords. Try create wc and adding stopwords to the known STOPWORDS outside your loop

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()

Try this function: (Reference)

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 ...")
Related