In NLP, when you want to discover words that occur most in your text data, you can build a
A sample WordCloud image is shown below:
In the WordCloud above, we can see that the words Shall, State, and United are the most important words in the complete text.
Now, let’s build our own WordCloud. However, before we can do that, we need to install some packages. Do this by running:
pip install wordcloud pip install matplotlib pip install numpy
Take a look at the code:
from wordcloud import WordCloud import matplotlib.pyplot as plt text = open('./constitution.txt').read() wordcloud = WordCloud().generate(text) fig = plt.figure() plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") fig.savefig('output/img.png') plt.close(fig)
bilinearto make the image look smoother.
When you run the code, you will see a WordCloud getting created. You can then test it using your own text data.
View all Courses