【问题标题】:adding words to stopwords in wordcloud does not work将单词添加到 wordcloud 中的停用词不起作用
【发布时间】:2020-03-03 05:40:09
【问题描述】:

我想向 wordcloud 添加一些停用词,但生成的图像显示了我添加到停用词中的确切单词。我做错了什么?如何将单词添加到停用词中

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)

【问题讨论】:

  • 首先你可以查看print(stopwords)
  • this question 对于同一个问题我觉得有更好的答案,基本上用collocations=False

标签: python numpy word-cloud


【解决方案1】:

你可以做这样的事情。

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

如果您想了解更多关于插值的信息,这里是一个有用的link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 2021-08-07
    • 2016-10-17
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多