In [181]:
from string import punctuation
import numpy as np
import pandas as pd
from sklearn.feature_extraction.stop_words import ENGLISH_STOP_WORDS
from PIL import Image
import matplotlib.pyplot as plt
from wordcloud import WordCloud

%matplotlib inline

In [78]:
import spacy
nlp = spacy.load('en')

In [130]:
def read_text(filename):
    text = []
    with open(filename) as f:
        for line in f:
            line = line.replace('\n', ' ')
            text.append(unicode(line))
            
    return ''.join(text)

In [132]:
text = read_text('speech_text.txt')
df = pd.DataFrame([text])
df.columns = ['text']

In [133]:
df


Out[133]:
text
0 Chief Justice Roberts, President Carter, Presi...

In [134]:
x = nlp(df['text'][0].strip(punctuation))

In [135]:
list(x.ents)


Out[135]:
[Carter,
 Clinton,
 Bush,
 Obama,
 Americans,
 America,
 America,
 years,
 Every four years,
 Obama,
 First,
 Michelle Obama,
 Today,
 today,
 Washington,
 D.C.,
 American,
 Capital,
 Washington,
 today,
 America,
 the United States of America,
 January 20th 2017,
 the day,
 tens of millions,
 Americans,
 American,
 one,
 one,
 one,
 today,
 Americans,
 American,
 trillions of dollars,
 America,
 One,
 millions,
 millions,
 American,
 today,
 this day,
 America First,
 American,
 American,
 America,
 American,
 American,
 two,
 American,
 American,
 first,
 Islamic,
 Earth,
 the United States of America,
 Bible,
 America,
 America,
 America,
 America,
 Earth,
 tomorrow,
 American,
 Detroit,
 Nebraska,
 Creator,
 Americans,
 American,
 America,
 America,
 America,
 America,
 God,
 God,
 America]

In [136]:
nlp_lst = [(tok.lemma_, tok.pos_) for tok in x if tok.pos_ != 'PUNCT' and tok.lemma_ not in list(ENGLISH_STOP_WORDS)]

In [137]:
lemma = []
pos = set()
for tup in nlp_lst:
    lemma.append(tup[0])
    pos.add(tup[1])

In [138]:
print lemma
pos


[u'chief', u'justice', u'roberts', u'president', u'carter', u'president', u'clinton', u'president', u'bush', u'president', u'obama', u'fellow', u'americans', u'people', u'world', u'thank', u'citizen', u'america', u'join', u'great', u'national', u'effort', u'rebuild', u'country', u'restore', u'promise', u'people', u'determine', u'course', u'america', u'world', u'year', u'come', u'face', u'challenge', u'confront', u'hardship', u'job', u'year', u'gather', u'step', u'carry', u'orderly', u'peaceful', u'transfer', u'power', u'grateful', u'president', u'obama', u'lady', u'michelle', u'obama', u'gracious', u'aid', u'transition', u'magnificent', u'today', u"'s", u'ceremony', u'special', u'meaning', u'today', u'merely', u'transfer', u'power', u'administration', u'party', u'transfer', u'power', u'washington', u'd.c.', u'american', u'people', u'word', u'past', u'inauguration', u'speech', u'library', u'long', u'small', u'group', u'nation', u"'s", u'capital', u'reap', u'reward', u'government', u'people', u'bear', u'cost', u'washington', u'flourish', u'people', u'share', u'wealth', u'politician', u'prosper', u'job', u'leave', u'factory', u'close', u'establishment', u'protect', u'citizen', u'country', u'victory', u'victory', u'triumph', u'triumph', u'celebrate', u'nation', u"'s", u'capital', u'little', u'celebrate', u'struggle', u'family', u'land', u'change', u'start', u'right', u'right', u'moment', u'moment', u'belong', u'belong', u'gather', u'today', u'watch', u'america', u'day', u'celebration', u'united', u'states', u'america', u'country', u'truly', u'matter', u'party', u'control', u'government', u'government', u'control', u'people', u'january', u'20th', u'2017', u'remember', u'day', u'people', u'ruler', u'nation', u'forget', u'man', u'woman', u'country', u'forget', u'longer', u'listen', u'come', u'million', u'historic', u'movement', u'like', u'world', u'center', u'movement', u'crucial', u'conviction', u'nation', u'exist', u'serve', u'citizen', u'americans', u'want', u'great', u'school', u'child', u'safe', u'neighborhood', u'family', u'good', u'job', u'just', u'reasonable', u'demand', u'righteous', u'public', u'citizen', u'different', u'reality', u'exist', u'mother', u'child', u'trap', u'poverty', u'inner', u'city', u'rust', u'factory', u'scatter', u'like', u'tombstone', u'landscape', u'nation', u'education', u'flush', u'cash', u'leave', u'young', u'beautiful', u'student', u'deprive', u'knowledge', u'crime', u'gang', u'drug', u'steal', u'life', u'rob', u'country', u'unrealized', u'potential', u'american', u'carnage', u'stop', u'right', u'stop', u'right', u'nation', u'pain', u'pain', u'dream', u'dream', u'success', u'success', u'share', u'heart', u'home', u'glorious', u'destiny', u'oath', u'office', u'today', u'oath', u'allegiance', u'americans', u'decade', u'enrich', u'foreign', u'industry', u'expense', u'american', u'industry', u'subsidize', u'army', u'country', u'allow', u'sad', u'depletion', u'military', u'defend', u'nation', u"'s", u'border', u'refuse', u'defend', u'spend', u'trillion', u'dollar', u'overseas', u'america', u"'s", u'infrastructure', u'fall', u'disrepair', u'decay', u'make', u'country', u'rich', u'wealth', u'strength', u'confidence', u'country', u'disappear', u'horizon', u'factory', u'shutter', u'leave', u'shore', u'thought', u'million', u'million', u'american', u'worker', u'leave', u'wealth', u'middle', u'class', u'rip', u'home', u'redistribute', u'entire', u'world', u'past', u'look', u'future', u'assemble', u'today', u'issue', u'new', u'decree', u'hear', u'city', u'foreign', u'capital', u'hall', u'power', u'day', u'forward', u'new', u'vision', u'govern', u'land', u'moment', u"'", u'america', u'decision', u'trade', u'tax', u'immigration', u'foreign', u'affair', u'make', u'benefit', u'american', u'worker', u'american', u'family', u'protect', u'border', u'ravage', u'country', u'make', u'product', u'steal', u'company', u'destroy', u'job', u'protection', u'lead', u'great', u'prosperity', u'strength', u'fight', u'breath', u'body', u'let', u'america', u'start', u'win', u'win', u'like', u'bring', u'job', u'bring', u'border', u'bring', u'wealth', u'bring', u'dream', u'build', u'new', u'road', u'highway', u'bridge', u'airport', u'tunnel', u'railway', u'wonderful', u'nation', u'people', u'welfare', u'work', u'rebuild', u'country', u'american', u'hand', u'american', u'labor', u'follow', u'simple', u'rule', u'buy', u'american', u'hire', u'american', u'seek', u'friendship', u'goodwill', u'nation', u'world', u'understanding', u'right', u'nation', u'seek', u'impose', u'way', u'life', u'let', u'shine', u'example', u'follow', u'reinforce', u'old', u'alliance', u'form', u'new', u'unite', u'civilized', u'world', u'radical', u'islamic', u'terrorism', u'eradicate', u'completely', u'face', u'earth', u'bedrock', u'politics', u'total', u'allegiance', u'united', u'states', u'america', u'loyalty', u'country', u'rediscover', u'loyalty', u'open', u'heart', u'patriotism', u'room', u'prejudice', u'bible', u'tell', u'good', u'pleasant', u'god', u"'s", u'people', u'live', u'unity', u'speak', u'mind', u'openly', u'debate', u'disagreement', u'honestly', u'pursue', u'solidarity', u'america', u'unite', u'america', u'totally', u'unstoppable', u'fear', u'protect', u'protect', u'protect', u'great', u'man', u'woman', u'military', u'law', u'enforcement', u'importantly', u'protect', u'god', u'finally', u'think', u'big', u'dream', u'big', u'america', u'understand', u'nation', u'live', u'long', u'strive', u'longer', u'accept', u'politician', u'talk', u'action', u'constantly', u'complain', u'time', u'talk', u'arrive', u'hour', u'action', u'let', u'tell', u'challenge', u'match', u'heart', u'fight', u'spirit', u'america', u'fail', u'country', u'thrive', u'prosper', u'stand', u'birth', u'new', u'millennium', u'ready', u'unlock', u'mystery', u'space', u'free', u'earth', u'misery', u'disease', u'harness', u'energy', u'industry', u'technology', u'tomorrow', u'new', u'national', u'pride', u'stir', u'soul', u'lift', u'sight', u'heal', u'division', u'time', u'remember', u'old', u'wisdom', u'soldier', u'forget', u'black', u'brown', u'white', u'bleed', u'red', u'blood', u'patriot', u'enjoy', u'glorious', u'freedom', u'salute', u'great', u'american', u'flag', u'child', u'bear', u'urban', u'sprawl', u'detroit', u'windswept', u'plain', u'nebraska', u'look', u'night', u'sky', u'heart', u'dream', u'infuse', u'breath', u'life', u'almighty', u'creator', u'americans', u'city', u'near', u'far', u'small', u'large', u'mountain', u'mountain', u'ocean', u'ocean', u'hear', u'word', u'ignore', u'voice', u'hope', u'dream', u'define', u'american', u'destiny', u'courage', u'goodness', u'love', u'forever', u'guide', u'way', u'make', u'america', u'strong', u'make', u'wealthy', u'make', u'america', u'proud', u'make', u'america', u'safe', u'yes', u'make', u'america', u'great', u'thank', u'god', u'bless', u'god', u'bless', u'america']
Out[138]:
{u'ADJ', u'ADP', u'ADV', u'INTJ', u'NOUN', u'NUM', u'PART', u'PROPN', u'VERB'}

In [260]:
def process_text(text_col):
    lemma_col = []
    pos_col = []
    ents_col = []
    for line in text_col:
        lemma_lst = []
        pos_set = set()
        x = nlp(line)
        ents_lst = list(x.ents)
        nlp_lst = [(tok.lemma_, tok.pos_) for tok in x \
                   if tok.pos_ not in ['PUNCT',] and tok.lemma_ not in list(ENGLISH_STOP_WORDS)]
        for lemma, pos in nlp_lst:
            lemma_lst.append(lemma)
            pos_set.add(pos)
        lemma_col.append(' '.join(lemma_lst))
        pos_col.append(pos_set)
        ents_col.append(ents_lst)
    return lemma_col, pos_col, ents_col

In [261]:
lemma, pos, ents = process_text(df['text'])

In [262]:
df['lemma'] = lemma
df['pos'] = pos
df['ents'] = ents

In [263]:
pos


Out[263]:
[{u'ADJ',
  u'ADP',
  u'ADV',
  u'INTJ',
  u'NOUN',
  u'NUM',
  u'PART',
  u'PROPN',
  u'VERB'}]

In [264]:
df


Out[264]:
text lemma pos ents
0 Chief Justice Roberts, President Carter, Presi... chief justice roberts president carter preside... {ADV, NOUN, ADP, PROPN, INTJ, PART, NUM, ADJ, ... [(Carter), (Clinton), (Bush), (Obama), (Americ...

In [265]:
lemma = lemma[0].split()

In [266]:
word_freq = set([(word, lemma.count(word)) for word in lemma if word != "'s"])

In [267]:
word_freq


Out[267]:
{(u"'", 1),
 (u'2017', 1),
 (u'20th', 1),
 (u'accept', 1),
 (u'action', 2),
 (u'administration', 1),
 (u'affair', 1),
 (u'aid', 1),
 (u'airport', 1),
 (u'allegiance', 2),
 (u'alliance', 1),
 (u'allow', 1),
 (u'almighty', 1),
 (u'america', 17),
 (u'american', 12),
 (u'americans', 4),
 (u'army', 1),
 (u'arrive', 1),
 (u'assemble', 1),
 (u'bear', 2),
 (u'beautiful', 1),
 (u'bedrock', 1),
 (u'belong', 2),
 (u'benefit', 1),
 (u'bible', 1),
 (u'big', 2),
 (u'birth', 1),
 (u'black', 1),
 (u'bleed', 1),
 (u'bless', 2),
 (u'blood', 1),
 (u'body', 1),
 (u'border', 3),
 (u'breath', 2),
 (u'bridge', 1),
 (u'bring', 4),
 (u'brown', 1),
 (u'build', 1),
 (u'bush', 1),
 (u'buy', 1),
 (u'capital', 3),
 (u'carnage', 1),
 (u'carry', 1),
 (u'carter', 1),
 (u'cash', 1),
 (u'celebrate', 2),
 (u'celebration', 1),
 (u'center', 1),
 (u'ceremony', 1),
 (u'challenge', 2),
 (u'change', 1),
 (u'chief', 1),
 (u'child', 3),
 (u'citizen', 4),
 (u'city', 3),
 (u'civilized', 1),
 (u'class', 1),
 (u'clinton', 1),
 (u'close', 1),
 (u'come', 2),
 (u'company', 1),
 (u'complain', 1),
 (u'completely', 1),
 (u'confidence', 1),
 (u'confront', 1),
 (u'constantly', 1),
 (u'control', 2),
 (u'conviction', 1),
 (u'cost', 1),
 (u'country', 12),
 (u'courage', 1),
 (u'course', 1),
 (u'creator', 1),
 (u'crime', 1),
 (u'crucial', 1),
 (u'd.c.', 1),
 (u'day', 3),
 (u'debate', 1),
 (u'decade', 1),
 (u'decay', 1),
 (u'decision', 1),
 (u'decree', 1),
 (u'defend', 2),
 (u'define', 1),
 (u'demand', 1),
 (u'depletion', 1),
 (u'deprive', 1),
 (u'destiny', 2),
 (u'destroy', 1),
 (u'determine', 1),
 (u'detroit', 1),
 (u'different', 1),
 (u'disagreement', 1),
 (u'disappear', 1),
 (u'disease', 1),
 (u'disrepair', 1),
 (u'division', 1),
 (u'dollar', 1),
 (u'dream', 6),
 (u'drug', 1),
 (u'earth', 2),
 (u'education', 1),
 (u'effort', 1),
 (u'energy', 1),
 (u'enforcement', 1),
 (u'enjoy', 1),
 (u'enrich', 1),
 (u'entire', 1),
 (u'eradicate', 1),
 (u'establishment', 1),
 (u'example', 1),
 (u'exist', 2),
 (u'expense', 1),
 (u'face', 2),
 (u'factory', 3),
 (u'fail', 1),
 (u'fall', 1),
 (u'family', 3),
 (u'far', 1),
 (u'fear', 1),
 (u'fellow', 1),
 (u'fight', 2),
 (u'finally', 1),
 (u'flag', 1),
 (u'flourish', 1),
 (u'flush', 1),
 (u'follow', 2),
 (u'foreign', 3),
 (u'forever', 1),
 (u'forget', 3),
 (u'form', 1),
 (u'forward', 1),
 (u'free', 1),
 (u'freedom', 1),
 (u'friendship', 1),
 (u'future', 1),
 (u'gang', 1),
 (u'gather', 2),
 (u'glorious', 2),
 (u'god', 4),
 (u'good', 2),
 (u'goodness', 1),
 (u'goodwill', 1),
 (u'govern', 1),
 (u'government', 3),
 (u'gracious', 1),
 (u'grateful', 1),
 (u'great', 6),
 (u'group', 1),
 (u'guide', 1),
 (u'hall', 1),
 (u'hand', 1),
 (u'hardship', 1),
 (u'harness', 1),
 (u'heal', 1),
 (u'hear', 2),
 (u'heart', 4),
 (u'highway', 1),
 (u'hire', 1),
 (u'historic', 1),
 (u'home', 2),
 (u'honestly', 1),
 (u'hope', 1),
 (u'horizon', 1),
 (u'hour', 1),
 (u'ignore', 1),
 (u'immigration', 1),
 (u'importantly', 1),
 (u'impose', 1),
 (u'inauguration', 1),
 (u'industry', 3),
 (u'infrastructure', 1),
 (u'infuse', 1),
 (u'inner', 1),
 (u'islamic', 1),
 (u'issue', 1),
 (u'january', 1),
 (u'job', 5),
 (u'join', 1),
 (u'just', 1),
 (u'justice', 1),
 (u'knowledge', 1),
 (u'labor', 1),
 (u'lady', 1),
 (u'land', 2),
 (u'landscape', 1),
 (u'large', 1),
 (u'law', 1),
 (u'lead', 1),
 (u'leave', 4),
 (u'let', 3),
 (u'library', 1),
 (u'life', 3),
 (u'lift', 1),
 (u'like', 3),
 (u'listen', 1),
 (u'little', 1),
 (u'live', 2),
 (u'long', 2),
 (u'longer', 2),
 (u'look', 2),
 (u'love', 1),
 (u'loyalty', 2),
 (u'magnificent', 1),
 (u'make', 8),
 (u'man', 2),
 (u'match', 1),
 (u'matter', 1),
 (u'meaning', 1),
 (u'merely', 1),
 (u'michelle', 1),
 (u'middle', 1),
 (u'military', 2),
 (u'millennium', 1),
 (u'million', 3),
 (u'mind', 1),
 (u'misery', 1),
 (u'moment', 3),
 (u'mother', 1),
 (u'mountain', 2),
 (u'movement', 2),
 (u'mystery', 1),
 (u'nation', 11),
 (u'national', 2),
 (u'near', 1),
 (u'nebraska', 1),
 (u'neighborhood', 1),
 (u'new', 6),
 (u'night', 1),
 (u'oath', 2),
 (u'obama', 3),
 (u'ocean', 2),
 (u'office', 1),
 (u'old', 2),
 (u'open', 1),
 (u'openly', 1),
 (u'orderly', 1),
 (u'overseas', 1),
 (u'pain', 2),
 (u'party', 2),
 (u'past', 2),
 (u'patriot', 1),
 (u'patriotism', 1),
 (u'peaceful', 1),
 (u'people', 9),
 (u'plain', 1),
 (u'pleasant', 1),
 (u'politician', 2),
 (u'politics', 1),
 (u'potential', 1),
 (u'poverty', 1),
 (u'power', 4),
 (u'prejudice', 1),
 (u'president', 5),
 (u'pride', 1),
 (u'product', 1),
 (u'promise', 1),
 (u'prosper', 2),
 (u'prosperity', 1),
 (u'protect', 6),
 (u'protection', 1),
 (u'proud', 1),
 (u'public', 1),
 (u'pursue', 1),
 (u'radical', 1),
 (u'railway', 1),
 (u'ravage', 1),
 (u'ready', 1),
 (u'reality', 1),
 (u'reap', 1),
 (u'reasonable', 1),
 (u'rebuild', 2),
 (u'red', 1),
 (u'rediscover', 1),
 (u'redistribute', 1),
 (u'refuse', 1),
 (u'reinforce', 1),
 (u'remember', 2),
 (u'restore', 1),
 (u'reward', 1),
 (u'rich', 1),
 (u'right', 5),
 (u'righteous', 1),
 (u'rip', 1),
 (u'road', 1),
 (u'rob', 1),
 (u'roberts', 1),
 (u'room', 1),
 (u'rule', 1),
 (u'ruler', 1),
 (u'rust', 1),
 (u'sad', 1),
 (u'safe', 2),
 (u'salute', 1),
 (u'scatter', 1),
 (u'school', 1),
 (u'seek', 2),
 (u'serve', 1),
 (u'share', 2),
 (u'shine', 1),
 (u'shore', 1),
 (u'shutter', 1),
 (u'sight', 1),
 (u'simple', 1),
 (u'sky', 1),
 (u'small', 2),
 (u'soldier', 1),
 (u'solidarity', 1),
 (u'soul', 1),
 (u'space', 1),
 (u'speak', 1),
 (u'special', 1),
 (u'speech', 1),
 (u'spend', 1),
 (u'spirit', 1),
 (u'sprawl', 1),
 (u'stand', 1),
 (u'start', 2),
 (u'states', 2),
 (u'steal', 2),
 (u'step', 1),
 (u'stir', 1),
 (u'stop', 2),
 (u'strength', 2),
 (u'strive', 1),
 (u'strong', 1),
 (u'struggle', 1),
 (u'student', 1),
 (u'subsidize', 1),
 (u'success', 2),
 (u'talk', 2),
 (u'tax', 1),
 (u'technology', 1),
 (u'tell', 2),
 (u'terrorism', 1),
 (u'thank', 2),
 (u'think', 1),
 (u'thought', 1),
 (u'thrive', 1),
 (u'time', 2),
 (u'today', 5),
 (u'tombstone', 1),
 (u'tomorrow', 1),
 (u'total', 1),
 (u'totally', 1),
 (u'trade', 1),
 (u'transfer', 3),
 (u'transition', 1),
 (u'trap', 1),
 (u'trillion', 1),
 (u'triumph', 2),
 (u'truly', 1),
 (u'tunnel', 1),
 (u'understand', 1),
 (u'understanding', 1),
 (u'unite', 2),
 (u'united', 2),
 (u'unity', 1),
 (u'unlock', 1),
 (u'unrealized', 1),
 (u'unstoppable', 1),
 (u'urban', 1),
 (u'victory', 2),
 (u'vision', 1),
 (u'voice', 1),
 (u'want', 1),
 (u'washington', 2),
 (u'watch', 1),
 (u'way', 2),
 (u'wealth', 4),
 (u'wealthy', 1),
 (u'welfare', 1),
 (u'white', 1),
 (u'win', 2),
 (u'windswept', 1),
 (u'wisdom', 1),
 (u'woman', 2),
 (u'wonderful', 1),
 (u'word', 2),
 (u'work', 1),
 (u'worker', 2),
 (u'world', 6),
 (u'year', 2),
 (u'yes', 1),
 (u'young', 1)}

In [268]:
word_freq = list(word_freq)
word_freq.sort(key=lambda x: x[1])

In [269]:
len(word_freq)


Out[269]:
385

In [270]:
wc = WordCloud(width=2000, height=1000, max_words=len(word_freq), background_color='white')
wc.fit_words(word_freq)
fig = plt.figure(figsize=(16, 8))
ax = fig.add_subplot(111)
ax.axis('off')
ax.imshow(wc)
plt.show()



In [ ]: