使用Python的词云

时间:2018-10-09 06:30:11

标签: python-3.x word-cloud

我想用变量'word'创建一个词云,该词云将向我显示所有'NN'和'NNP'的词云

import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
# Using Textblob
for word,noun in blob.tags:
    if noun in ['NN','NNP']:
    print(f'{word} ==> {noun}')

我将在哪里添加以下代码:

  • 创建并生成文字云图像:

    wordcloud = WordCloud().generate(word)
    # Display the generated image:
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis("off")
    plt.show()
    

1 个答案:

答案 0 :(得分:0)

首先,您需要简短列出单词并生成一个列表对象,如下所示:

words = []
for word,noun in blob.tags:
    if noun in ['NN','NNP']:
       print(f'{word} ==> {noun}')
       words.append(word)

然后,您可以将上述单词列表输入到单词云生成器中,如下所示,可以选择列出停用词列表:

wordcloud = WordCloud(stopwords=STOPWORDS).generate(' '.join(words))
# Display the generated image:
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()