在WordCloud中保持单词聚合

2024-10-03 11:24:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在python中使用wordclouds库处理wordclouds

例如,我想从以下列表中创建一个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")

Tags: 字符串textapple列表属性方式lsword
1条回答
网友
1楼 · 发布于 2024-10-03 11:24:24

这应该管用

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

相关问题 更多 >