In [32]:
import os
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from wordcloud import WordCloud, STOPWORDS

# Creates word cloud for each candidate in year, using the shape of imageMaskFileName
def createWordClouds(imageMaskFileName, year):
    for candidate_file in os.listdir('./Campaign Speeches/' + str(year)):
        text = open('./Campaign Speeches/2016/' + candidate_file, 'r+', encoding = 'utf8').read()

        mask = np.array(Image.open(imageMaskFileName))
        
        firstNameLastName = candidate_file.rstrip('.txt').split('-')
        stopwords = set(STOPWORDS)
        stopwords.add("applause")
        stopwords.add(firstNameLastName[0])
        stopwords.add(firstNameLastName[1])

        wc = WordCloud(background_color="white", max_words=2000, mask=mask, stopwords=stopwords)
        wc.generate(text)
        wc.to_file( "./clouds/" + candidate_file + ".png")

In [31]:
createWordClouds('./us-map-silhouette-vector.png', 2016)


Ben Carson
Bernie Sanders
Bobby Jindal
Carly Fiorina
Chris Christie
Donald Trump
George Pataki
Hillary Clinton
Jeb Bush
Jim Webb
John Kasich
Lincoln Chafee
Lindsey Graham
Marco Rubio
Martin O'Malley
Mike Huckabee
Rand Paul
Rick Perry
Rick Santorum
Scott Walker
Ted Cruz