我的词云缺少我字符串中四个单词中的三个。如何添加缺少的单词?

问题描述 投票:1回答:2

我正在尝试从句子“嗨,你好吗”中创建一个词云。但是我只有第一个字。为什么?

代码:

#@title Bar plot of most frequent words.
from wordcloud import WordCloud,STOPWORDS
stopwords = set(STOPWORDS)
wordcloud = WordCloud(
    width=800,height=800,
    stopwords = stopwords,
    min_font_size = 10,
    background_color='white'
).generate("hi how are you") 
# plot the WordCloud image                        
plt.figure(figsize = (8, 8), facecolor = None) 
plt.imshow(wordcloud,interpolation="bilinear") 
plt.axis("off") 
plt.tight_layout(pad = 0) 

plt.show()

输出:

enter image description here

python matplotlib word-cloud
2个回答
1
投票

在上面的OP代码中,stopwords参数设置为模块STOPWORDS列表。在该列表中,都包括howareyou。这限制了这些单词无法显示在wordcloud中。

注意,如果未提供此参数,则它也将默认为该列表,因此,如果要包含所有单词,则需要加载一个空列表。


0
投票

hiareyou包含在STOPWORDS中。

[如果要保留这些特定的单词,则需要从STOPWORDS中过滤掉它们

赞:

from wordcloud import WordCloud, STOPWORDS

stopwords = {word for word in STOPWORDS if word not in {'how', 'are', 'you'}}
wordcloud = WordCloud(
    width=800,height=800,
    stopwords = stopwords,
    min_font_size = 10,
    background_color='white'
).generate("hi how are you")

# plot the WordCloud image           
plt.figure(figsize = (8, 8), facecolor=None)
plt.imshow(wordcloud,interpolation="bilinear")
plt.axis("off")
plt.tight_layout(pad = 0)

plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.