为 python 中列表中的项目生成词云

generating word cloud for items in a list in python

 my_list=["one", "one two", "three"]

我正在使用

为此列表生成词云
 wordcloud = WordCloud(width = 1000, height = 500).generate(" ".join(my_list))

当我将所有项目转换为字符串时,它正在为

生成词云
   "one","two","three"

 But I want to generate word cloud for the values, "one","one two","three"

帮助我为列表中的项目生成词云

WordCloud以正则表达式作为参数。使用这个我们可以使拆分字符成为 + 而不是 space.

regexp=r"\w[\w' ]+"

然后需要在 + 上加入单词列表,每个单词现在都用于拆分单词。产生以下代码:

wordcloud = WordCloud(width=1000, height=500, regexp=r"\w[\w' ]+").generate("+".join(my_list))

一种做法,

import matplotlib.pyplot as plt
from wordcloud import WordCloud

#convert list to string and generate
unique_string=(" ").join(my_list)
wordcloud = WordCloud(width = 1000, height = 500).generate(unique_string)
plt.figure(figsize=(15,8))
plt.imshow(wordcloud)
plt.axis("off")
plt.savefig("your_file_name"+".png", bbox_inches='tight')
plt.show()
plt.close()

创建计数器字典的另一种方法,

#convert it to dictionary with values and its occurences
from collections import Counter
word_could_dict=Counter(my_list)
wordcloud = WordCloud(width = 1000, height = 500).generate_from_frequencies(word_could_dict)

plt.figure(figsize=(15,8))
plt.imshow(wordcloud)
plt.axis("off")
#plt.show()
plt.savefig('yourfile.png', bbox_inches='tight')
plt.close()