This notebook accompanies my Medium article: Is this presidential election more negative then years past? Yes.
Copy-paste your API Key here:
In [2]:
APIKEY="AIzaSyBNa0Hw5_SZpmQP2-iXgUfchVHa4Ot956M"
Note: Make sure you got an API Key and pasted it above. Mine won't work for you
From the same API console, choose "Dashboard" on the left-hand menu and "Enable API".
Finally, because we are calling the APIs from Python (clients in many other languages are available), let's install the Python package (it's not installed by default on Datalab)
In [3]:
# Copyright 2016 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
!pip install --upgrade google-api-python-client
Let's evaluate the sentiment of nomination acceptance speeches for the last 5 election cycles. The text of the speeches will be downloaded from U. California, Santa Barbara.
In [4]:
from googleapiclient.discovery import build
import pandas as pd
import numpy as np
import urllib2
lservice = build('language', 'v1beta1', developerKey=APIKEY)
speeches = [
['Hillary Clinton', 'D', 2016, 'http://www.presidency.ucsb.edu/ws/index.php?pid=118051'],
['Donald Trump', 'R', 2016, 'http://www.presidency.ucsb.edu/ws/index.php?pid=117935'],
['Barack Obama', 'D', 2012, 'http://www.presidency.ucsb.edu/ws/index.php?pid=101968'],
['Mitt Romney', 'R', 2012, 'http://www.presidency.ucsb.edu/ws/index.php?pid=101966'],
['Barack Obama', 'D', 2008, 'http://www.presidency.ucsb.edu/ws/index.php?pid=78284'],
['John McCain', 'R', 2008, 'http://www.presidency.ucsb.edu/ws/index.php?pid=78576'],
['John Kerry', 'D', 2004, 'http://www.presidency.ucsb.edu/ws/index.php?pid=25971'],
['George W Bush', 'R', 2004, 'http://www.presidency.ucsb.edu/ws/index.php?pid=72727'],
['Al Gore', 'D', 2000, 'http://www.presidency.ucsb.edu/ws/index.php?pid=25963'],
['George W Bush', 'R', 2000, 'http://www.presidency.ucsb.edu/ws/index.php?pid=25954']
]
sentiment = []
for (speaker, party, year, url) in speeches:
text_of_speech = urllib2.urlopen(url).read()
response = lservice.documents().analyzeSentiment(
body={
'document': {
'type': 'HTML',
'content': unicode(text_of_speech, errors='ignore')
}
}).execute()
polarity = response['documentSentiment']['polarity']
magnitude = response['documentSentiment']['magnitude']
print('POLARITY=%s MAGNITUDE=%s SPEAKER=%s' % (polarity, magnitude, speaker))
sentiment.extend([speaker, party, year, float(polarity), float(magnitude)])
In [6]:
df = pd.DataFrame(data=np.array(sentiment).reshape(10,5),
columns=['speaker', 'party', 'year', 'polarity', 'magnitude'])
for col in ['year', 'polarity', 'magnitude']:
df[col] = pd.to_numeric(df[col])
print df
In [7]:
df = df.sort_values('year')
df.plot(x='speaker', y='polarity', kind='bar')
Out[7]:
In [8]:
df.plot(x='speaker', y='magnitude', kind='bar')
Out[8]:
In [11]:
df[df['party'] == 'D'].mean()
Out[11]:
In [12]:
df[df['party'] == 'R'].mean()
Out[12]:
In [ ]: