Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a word cloud from a corpus in Python?

From Creating a subset of words from a corpus in R, the answerer can easily convert a term-document matrix into a word cloud easily.

Is there a similar function from python libraries that takes either a raw word textfile or NLTK corpus or Gensim Mmcorpus into a word cloud?

The result will look somewhat like this: enter image description here

like image 450
alvas Avatar asked May 20 '13 08:05

alvas


People also ask

How do I create a word cloud in Matplotlib?

The three steps are: Extract the review (text document) Create and generate a wordcloud image. Display the cloud using matplotlib.


1 Answers

from wordcloud import WordCloud, STOPWORDS import matplotlib.pyplot as plt stopwords = set(STOPWORDS)  def show_wordcloud(data, title = None):     wordcloud = WordCloud(         background_color='white',         stopwords=stopwords,         max_words=200,         max_font_size=40,          scale=3,         random_state=1 # chosen at random by flipping a coin; it was heads     ).generate(str(data))      fig = plt.figure(1, figsize=(12, 12))     plt.axis('off')     if title:          fig.suptitle(title, fontsize=20)         fig.subplots_adjust(top=2.3)      plt.imshow(wordcloud)     plt.show()  show_wordcloud(Samsung_Reviews_Negative['Reviews']) show_wordcloud(Samsung_Reviews_positive['Reviews']) 

enter image description here

like image 98
HeadAndTail Avatar answered Sep 21 '22 06:09

HeadAndTail