在WordCloud中将单词聚在一起

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

[我正在使用wordcloud库处理python中的词云。

作为示例,我想从以下列表中创建wordcloud:

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")
python python-3.x data-visualization information-retrieval word-cloud
1个回答
0
投票

应该起作用

from wordcloud import WordCloud
from collections import Counter

word_ls = ['orchards growers northern', 'apple orchards growers', 'threatening apple orchards']
word_text = ";".join(word_ls)
word_could_dict=Counter(word_ls)
wordcloud = WordCloud(regexp=r"\w[\w' ]+").generate_from_frequencies(word_could_dict)
wordcloud.to_file("word_test.png")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.