为预定义的权重生成词云

时间:2019-04-18 03:57:33

标签: python-3.x word-cloud

我想从这个预先定义的权重和单词列表生成单词云:

[(-0.5299421108191749, 'disappointed'), (-0.39819759256183956, 'worst'), 
(-0.3602799803363835, 'horrible'), (-0.35797658997659154, 'ok'), 
(-0.35156345503427994, 'disappointing'), (-0.35107689857480584, 
'unfortunately'), (-0.3455404207434287, 'stick'), (-0.3322087737538663, 
'terrible'), (-0.3269409178623135, 'threw'), (-0.3245551871786074, 
'away')]

如何从中生成词云?

1 个答案:

答案 0 :(得分:0)

首先将此列表转换为字典:

values=list()
words=list()
for i in listWords:
    values.append(i[0])
    words.append(i[1])
temp = zip(words, values)
dictWords = dict(temp)

listWords包含以上列表。 现在使用 generate_from_frequencies 从字典创建词云,如下所示:

from wordcloud import WordCloud
wordcloud = WordCloud(width=1600,height=800).generate_from_frequencies(dictWords)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()