Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating word cloud for items in a list in python

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

and I am generating a word cloud for this list by using

 wordcloud = WordCloud(width = 1000, height = 500).generate(" ".join(my_list))

As I am converting all the items into string it is generating word cloud for

   "one","two","three"

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

help me for generating word cloud for items in a list

like image 416
pyd Avatar asked Aug 09 '17 10:08

pyd


People also ask

How do I make a word cloud with multiple words?

How do I keep multiple words together in the cloud (e.g. 'New York')? Use a tilde character ~ between words you want to keep together. To do this, run a find & replace on the original text file and insert a ~ (tilde character) between the words you want to group.


2 Answers

one way of doing,

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()

Another way by creating Counter Dictionary,

#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()
like image 179
pyd Avatar answered Sep 29 '22 09:09

pyd


The WordCloud takes regular expression as argument. Using this we can make the splitting character a + instead of a space.

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

The list of words then needs to be joined on a + as well as each this is now used to split words. Resulting in the following code:

wordcloud = WordCloud(width=1000, height=500, regexp=r"\w[\w' ]+").generate("+".join(my_list))
like image 41
ikkuh Avatar answered Sep 29 '22 08:09

ikkuh