在WordCloud中将单词保持在一起

时间:2019-12-02 23:39:49

标签: python python-3.x data-visualization information-retrieval word-cloud

我正在使用wordcloud库在python中使用词云。

作为一个例子,我想从以下列表中做一个词云:

word_ls = ['orchards growers northern', 'apple orchards growers', 'threatening apple orchards']

我面临的问题是,当我生成云时,我不能让它单独考虑每个字符串,而不是逐字地考虑

我尝试使用regexp属性以不同的方式进行令牌分离,尽管未成功(使用r"\w[\w ']+"获得KeyError)

有什么见解吗?

示例wordcloud生成代码段:

word_text = ";".join(word_ls)
wordcloud = WordCloud().generate(word_text)
wordcloud.to_file("word_test.png")

1 个答案:

答案 0 :(得分:2)

应该可以

from wordcloud import WordCloud
from collections import Counter

word_ls = ['orchards growers northern', 'apple orchards growers', 'threatening apple orchards']
word_could_dict = Counter(word_ls)
wordcloud = WordCloud().generate_from_frequencies(word_could_dict)
wordcloud.to_file("word_test.png")

enter image description here