adding words to stopwords in wordcloud does not work

痴心易碎 提交于 2020-08-09 01:44:06

问题


I want to add some stopwords to wordcloud but the resulting image shows the exact words I add to the stopwords. I am doing something wrong? How can I add the word to the stopwords

from wordcloud import STOPWORDS as EN_STOPWORDS
from wordcloud import ImageColorGenerator
from stopword_persian import stopword_persian as STOPWORDS
from wordcloud_fa import WordCloudFa


# Add another stopword
STOPWORDS.add('ميساخته')
stopwords = STOPWORDS.union(EN_STOPWORDS)


# Generate a word cloud image

wordcloud = WordCloudFa(
    persian_normalize=True,
    include_numbers=True,
    max_words=300,
    stopwords=stopwords,
    margin=0,
    width=3000,
    height=3000,
    min_font_size=1,
    max_font_size=500,
    random_state=True,
    background_color="black",
    mask=twitter_mask
).generate(text)


回答1:


You can do something like this.

import matplotlib.pyplot as plt
import nltk # Natural Language ToolKit
nltk.download('stopwords')
from nltk.corpus import stopwords # to get rid of StopWords 

from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator # to create a Word Cloud
from PIL import Image # Pillow with WordCloud to image manipulation

text = 'New stop words are bad for this text.'

# Adding the stopwords
stop_words = stopwords.words('en') 
new_stopwors = ['new', 'stop', 'words']
stop_words.extend(new_stopwords)
stop_words = set(stop_words)

# Getting rid of the stopwords
clean_text = [word for word in text.split() if word not in stop_words]

# Converting the list to string
text = ' '.join([str(elem) for elem in clean_text])

# Generating a wordcloud
wordcloud = WordCloud(background_color = "black").generate(text)

# Display the generated image:
plt.figure(figsize = (15, 10))
plt.imshow(wordcloud, interpolation = 'bilinear')
plt.axis("off")
plt.show()

Here is a helpful link If you want to explore more about the interpolations.



来源:https://stackoverflow.com/questions/60494293/adding-words-to-stopwords-in-wordcloud-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!