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

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


from wordcloud import WordCloud, STOPWORDSimport matplotlib.pyplot as pltstopwords = 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


In case you require these word clouds for showing them in website or web app you can convert your data to json or csv format and load it to a JavaScript visualisation library such as d3. Word Clouds on d3

If not, Marcin's answer is a good way for doing what you describe.