Making word coulds is pretty easy. I'm using the wordcloud module for this.
import re
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
First, I open the text file. Then I count the occurrance of each word in that text, excluding stopwords. And then with the help of the wordcloud module, I draw the cloud. And that's it!
def draw_wordcloud(filename):
file = open(filename, encoding="utf8")
text = file.read()
file.close()
stopwords = set(STOPWORDS)
stopwords.add('will') # also add 'will' into stopwords
wordcount = {}
for word in text.lower().split():
word = re.sub(r'\.|,|:|!|\?|\(|\)|\*|\/', '', word)
if word not in stopwords:
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
wordcloud = WordCloud(stopwords=stopwords,
background_color="white",
width=2000, height=1000).generate(text)
plt.figure(figsize=(20,40))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.savefig(filename.replace("txt", "") + "png", bbox_inches='tight', dpi=200)
plt.show()
draw_wordcloud("bush-2001.txt")
draw_wordcloud("bush-2005.txt")
draw_wordcloud("obama-2009.txt")
draw_wordcloud("obama-2013.txt")
draw_wordcloud("trump-2017.txt")