Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove words from my wordcloud? (Python 3)

import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import wordcloud
from wordcloud import WordCloud,STOPWORDS

# Read the whole text.
remarks = open(r'C:\Users\marmar\Documents\Remarks.txt').read()

#Create words over an image
mask = np.array(Image.open(r'C:\users\marmar\Documents\cloud.png'))

#set the stopwords list
stopwords= set(STOPWORDS)

#append new words to the stopwords list
new_words =open(r'C:\Users\marmar\comments.txt').read()
new_stopwords=stopwords.union(new_words)

#generate the word cloud with parameters
wc = WordCloud(background_color="white", 
               max_words=2000, 
               mask=mask,
               min_font_size =12, 
               max_font_size=20, 
               relative_scaling = 0.5, 
               stopwords=new_stopwords,
               normalize_plurals= True)
wc.generate(remarks)
plt.figure(figsize=(25,25))
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")

#Show the wordcloud
plt.show()

Basically, I'm using Python 3 ( Jupyter Notebook) to create a wordcloud with an actual cloud picture. WordCloud packages actually has its own stopwords function. However, i want to include some words in the stopwords list that I don't want to see in my cloud. I tried to include some words in that text file, but I can see the words in my cloud. For example the text file looks like : customer, CSR Customer, satisfied, Item Completed

How do I go about adding more words to the list. I tried add, append, both of these functions and they won't work.

Thank you in advance.

like image 541
marmar Avatar asked Nov 07 '22 13:11

marmar


1 Answers

Ah hah! It's because I had commas separating my words in my text file.

For those building a wordcloud, just write the type the words seperating it with just a space. No need for punctuation. @RagingRoosevelt was correct on using the "split" function.

like image 197
marmar Avatar answered Nov 14 '22 20:11

marmar