In [2]:
import datetime

import pandas as pd
import thirdparty.tsearch.TwitterScraper as ts

import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer

In [7]:
bernie_search = ['Bernie', 'Sanders', 'BernieSanders']
rubio_search = ['Marco', 'Rubio', 'marcorubio']
cruz_search = ['Ted', 'Cruz', 'tedcruz']
clinton_search = ['Hillary', 'Clinton', 'HillaryClinton']
trump_search = ['Donald', 'Trump', 'DonaldTrump']

bernie_keywords = ['Bernie', 'Sanders', 'BernieSanders', 'Feel the Bern', 'Bern', 'feelthebern']
rubio_keywords = ['Marco', 'Rubio', 'marcorubio', '"A New American Century"']
cruz_keywords = ['Ted', 'Cruz', 'tedcruz', 'CruzCrew', '"Together, we will win"']
clinton_keywords = ['Hillary', 'Clinton', 'HillaryClinton', '"I\'m with her"', '"Stronger Together"', 'imwithher']
trump_keywords = ['Donald', 'Trump', 'DonaldTrump', 'NeverTrump', '"Make America Great Again"', 'maga']

primaries = {'Democrat': [('Clinton', clinton_search), 
                          ('Sanders', bernie_search)],
             'Republican': [('Rubio', rubio_search),
                            ('Cruz', cruz_search),
                            ('Trump', trump_search)]}

In [8]:
# Generate all the time-based and geo-based components from the csv
state_dict = dict()

with open('data/raw_calc_lat_lon.csv', 'r') as csvfile:
    tab = csv.reader(csvfile, delimiter=',')
    for row in tab:
        if row[1] != 'latitude':
            state = row[0]
            latitude = row[1]
            longitude = row[2]
            radius = row[3]
            date = row[4]
            party = row[5]
            after = row[6]
            until = row[7]
            
            if state not in state_dict:
                state_dict[state] = dict()
            
            state_dict[state][party] = (latitude, longitude, radius, after, until)

In [11]:
# Get all the tweets for a person
qs_list = []
tweets = dict()
for state in state_dict.keys():
    tweets[state] = dict()
    
    for primary in primaries.keys():
        tweets[state][primary] = dict()
        for candidate in primaries[primary]:
            
            latitude, longitude, radius, since, until = state_dict[state][primary]
            geo = ' geocode:%.6f,%.6f,%dkm' % (float(latitude), float(longitude), float(radius))
            query_string = " OR ".join(candidate[1]) + geo
            
            since = datetime.datetime.strptime(since, '%m/%d/%y')
            until = datetime.datetime.strptime(until, '%m/%d/%y')
    
            twitSlice = ts.TwitterSlicer(0, 5, since, until, 5)
            twitSlice.search(query_string)
            
            tweets[state][primary][candidate[0]] = twitSlice.tweets
            print('%s %s %s: Downloaded %d tweets.' % (state, primary, candidate[0], twitSlice.counter))


Alabama Democrat Clinton: Downloaded 20 tweets.
Alabama Democrat Sanders: Downloaded 5 tweets.
Alabama Republican Rubio: Downloaded 136 tweets.
Alabama Republican Cruz: Downloaded 52 tweets.
Alabama Republican Trump: Downloaded 176 tweets.
Alaska Democrat Clinton: Downloaded 0 tweets.
Alaska Democrat Sanders: Downloaded 4 tweets.
Alaska Republican Rubio: Downloaded 2 tweets.
Alaska Republican Cruz: Downloaded 9 tweets.
Alaska Republican Trump: Downloaded 0 tweets.
Arizona Democrat Clinton: Downloaded 43 tweets.
Arizona Democrat Sanders: Downloaded 91 tweets.
Arizona Republican Rubio: Downloaded 4 tweets.
Arizona Republican Cruz: Downloaded 35 tweets.
Arizona Republican Trump: Downloaded 161 tweets.
Arkansas Democrat Clinton: Downloaded 16 tweets.
Arkansas Democrat Sanders: Downloaded 2 tweets.
Arkansas Republican Rubio: Downloaded 15 tweets.
Arkansas Republican Cruz: Downloaded 10 tweets.
Arkansas Republican Trump: Downloaded 8 tweets.
California Democrat Clinton: Downloaded 59 tweets.
California Democrat Sanders: Downloaded 231 tweets.
California Republican Rubio: Downloaded 6 tweets.
California Republican Cruz: Downloaded 311 tweets.
California Republican Trump: Downloaded 132 tweets.
Colorado Democrat Clinton: Downloaded 15 tweets.
Colorado Democrat Sanders: Downloaded 48 tweets.
Colorado Republican Rubio: Downloaded 48 tweets.
Colorado Republican Cruz: Downloaded 55 tweets.
Colorado Republican Trump: Downloaded 163 tweets.
Connecticut Democrat Clinton: Downloaded 16 tweets.
Connecticut Democrat Sanders: Downloaded 50 tweets.
Connecticut Republican Rubio: Downloaded 0 tweets.
Connecticut Republican Cruz: Downloaded 4 tweets.
Connecticut Republican Trump: Downloaded 16 tweets.
Delaware Democrat Clinton: Downloaded 0 tweets.
Delaware Democrat Sanders: Downloaded 0 tweets.
Delaware Republican Rubio: Downloaded 0 tweets.
Delaware Republican Cruz: Downloaded 0 tweets.
Delaware Republican Trump: Downloaded 8 tweets.
District of Columbia Democrat Clinton: Downloaded 0 tweets.
District of Columbia Democrat Sanders: Downloaded 0 tweets.
District of Columbia Republican Rubio: Downloaded 0 tweets.
District of Columbia Republican Cruz: Downloaded 0 tweets.
District of Columbia Republican Trump: Downloaded 0 tweets.
Florida Democrat Clinton: Downloaded 22 tweets.
Florida Democrat Sanders: Downloaded 119 tweets.
Florida Republican Rubio: Downloaded 60 tweets.
Florida Republican Cruz: Downloaded 56 tweets.
Florida Republican Trump: Downloaded 196 tweets.
Georgia Democrat Clinton: Downloaded 26 tweets.
Georgia Democrat Sanders: Downloaded 35 tweets.
Georgia Republican Rubio: Downloaded 70 tweets.
Georgia Republican Cruz: Downloaded 52 tweets.
Georgia Republican Trump: Downloaded 156 tweets.
Hawaii Democrat Clinton: Downloaded 0 tweets.
Hawaii Democrat Sanders: Downloaded 16 tweets.
Hawaii Republican Rubio: Downloaded 2 tweets.
Hawaii Republican Cruz: Downloaded 4 tweets.
Hawaii Republican Trump: Downloaded 9 tweets.
Idaho Democrat Clinton: Downloaded 0 tweets.
Idaho Democrat Sanders: Downloaded 34 tweets.
Idaho Republican Rubio: Downloaded 3 tweets.
Idaho Republican Cruz: Downloaded 4 tweets.
Idaho Republican Trump: Downloaded 6 tweets.
Illinois Democrat Clinton: Downloaded 10 tweets.
Illinois Democrat Sanders: Downloaded 24 tweets.
Illinois Republican Rubio: Downloaded 2 tweets.
Illinois Republican Cruz: Downloaded 8 tweets.
Illinois Republican Trump: Downloaded 29 tweets.
Indiana Democrat Clinton: Downloaded 21 tweets.
Indiana Democrat Sanders: Downloaded 104 tweets.
Indiana Republican Rubio: Downloaded 2 tweets.
Indiana Republican Cruz: Downloaded 76 tweets.
Indiana Republican Trump: Downloaded 99 tweets.
Iowa Democrat Clinton: Downloaded 31 tweets.
Iowa Democrat Sanders: Downloaded 75 tweets.
Iowa Republican Rubio: Downloaded 37 tweets.
Iowa Republican Cruz: Downloaded 21 tweets.
Iowa Republican Trump: Downloaded 42 tweets.
Kansas Democrat Clinton: Downloaded 2 tweets.
Kansas Democrat Sanders: Downloaded 14 tweets.
Kansas Republican Rubio: Downloaded 8 tweets.
Kansas Republican Cruz: Downloaded 11 tweets.
Kansas Republican Trump: Downloaded 36 tweets.
Kentucky Democrat Clinton: Downloaded 4 tweets.
Kentucky Democrat Sanders: Downloaded 9 tweets.
Kentucky Republican Rubio: Downloaded 5 tweets.
Kentucky Republican Cruz: Downloaded 8 tweets.
Kentucky Republican Trump: Downloaded 65 tweets.
Louisiana Democrat Clinton: Downloaded 2 tweets.
Louisiana Democrat Sanders: Downloaded 2 tweets.
Louisiana Republican Rubio: Downloaded 2 tweets.
Louisiana Republican Cruz: Downloaded 4 tweets.
Louisiana Republican Trump: Downloaded 12 tweets.
Maine Democrat Clinton: Downloaded 2 tweets.
Maine Democrat Sanders: Downloaded 2 tweets.
Maine Republican Rubio: Downloaded 0 tweets.
Maine Republican Cruz: Downloaded 4 tweets.
Maine Republican Trump: Downloaded 0 tweets.
Maryland Democrat Clinton: Downloaded 31 tweets.
Maryland Democrat Sanders: Downloaded 117 tweets.
Maryland Republican Rubio: Downloaded 5 tweets.
Maryland Republican Cruz: Downloaded 56 tweets.
Maryland Republican Trump: Downloaded 89 tweets.
Massachusetts Democrat Clinton: Downloaded 9 tweets.
Massachusetts Democrat Sanders: Downloaded 13 tweets.
Massachusetts Republican Rubio: Downloaded 4 tweets.
Massachusetts Republican Cruz: Downloaded 4 tweets.
Massachusetts Republican Trump: Downloaded 9 tweets.
Michigan Democrat Clinton: Downloaded 0 tweets.
Michigan Democrat Sanders: Downloaded 4 tweets.
Michigan Republican Rubio: Downloaded 0 tweets.
Michigan Republican Cruz: Downloaded 0 tweets.
Michigan Republican Trump: Downloaded 5 tweets.
Minnesota Democrat Clinton: Downloaded 0 tweets.
Minnesota Democrat Sanders: Downloaded 4 tweets.
Minnesota Republican Rubio: Downloaded 2 tweets.
Minnesota Republican Cruz: Downloaded 0 tweets.
Minnesota Republican Trump: Downloaded 2 tweets.
Mississippi Democrat Clinton: Downloaded 20 tweets.
Mississippi Democrat Sanders: Downloaded 2 tweets.
Mississippi Republican Rubio: Downloaded 2 tweets.
Mississippi Republican Cruz: Downloaded 0 tweets.
Mississippi Republican Trump: Downloaded 6 tweets.
Missouri Democrat Clinton: Downloaded 6 tweets.
Missouri Democrat Sanders: Downloaded 39 tweets.
Missouri Republican Rubio: Downloaded 6 tweets.
Missouri Republican Cruz: Downloaded 12 tweets.
Missouri Republican Trump: Downloaded 27 tweets.
Montana Democrat Clinton: Downloaded 4 tweets.
Montana Democrat Sanders: Downloaded 2 tweets.
Montana Republican Rubio: Downloaded 0 tweets.
Montana Republican Cruz: Downloaded 0 tweets.
Montana Republican Trump: Downloaded 4 tweets.
Nebraska Democrat Clinton: Downloaded 0 tweets.
Nebraska Democrat Sanders: Downloaded 2 tweets.
Nebraska Republican Rubio: Downloaded 0 tweets.
Nebraska Republican Cruz: Downloaded 0 tweets.
Nebraska Republican Trump: Downloaded 0 tweets.
Nevada Democrat Clinton: Downloaded 2 tweets.
Nevada Democrat Sanders: Downloaded 0 tweets.
Nevada Republican Rubio: Downloaded 0 tweets.
Nevada Republican Cruz: Downloaded 3 tweets.
Nevada Republican Trump: Downloaded 0 tweets.
New Hampshire Democrat Clinton: Downloaded 7 tweets.
New Hampshire Democrat Sanders: Downloaded 8 tweets.
New Hampshire Republican Rubio: Downloaded 8 tweets.
New Hampshire Republican Cruz: Downloaded 6 tweets.
New Hampshire Republican Trump: Downloaded 9 tweets.
New Jersey Democrat Clinton: Downloaded 19 tweets.
New Jersey Democrat Sanders: Downloaded 18 tweets.
New Jersey Republican Rubio: Downloaded 10 tweets.
New Jersey Republican Cruz: Downloaded 12 tweets.
New Jersey Republican Trump: Downloaded 63 tweets.
New Mexico Democrat Clinton: Downloaded 11 tweets.
New Mexico Democrat Sanders: Downloaded 74 tweets.
New Mexico Republican Rubio: Downloaded 0 tweets.
New Mexico Republican Cruz: Downloaded 4 tweets.
New Mexico Republican Trump: Downloaded 6 tweets.
New York Democrat Clinton: Downloaded 32 tweets.
New York Democrat Sanders: Downloaded 28 tweets.
New York Republican Rubio: Downloaded 0 tweets.
New York Republican Cruz: Downloaded 13 tweets.
New York Republican Trump: Downloaded 23 tweets.
North Carolina Democrat Clinton: Downloaded 35 tweets.
North Carolina Democrat Sanders: Downloaded 125 tweets.
North Carolina Republican Rubio: Downloaded 14 tweets.
North Carolina Republican Cruz: Downloaded 38 tweets.
North Carolina Republican Trump: Downloaded 110 tweets.
North Dakota Democrat Clinton: Downloaded 0 tweets.
North Dakota Democrat Sanders: Downloaded 0 tweets.
North Dakota Republican Rubio: Downloaded 0 tweets.
North Dakota Republican Cruz: Downloaded 0 tweets.
North Dakota Republican Trump: Downloaded 0 tweets.
Ohio Democrat Clinton: Downloaded 26 tweets.
Ohio Democrat Sanders: Downloaded 106 tweets.
Ohio Republican Rubio: Downloaded 22 tweets.
Ohio Republican Cruz: Downloaded 43 tweets.
Ohio Republican Trump: Downloaded 113 tweets.
Oklahoma Democrat Clinton: Downloaded 16 tweets.
Oklahoma Democrat Sanders: Downloaded 47 tweets.
Oklahoma Republican Rubio: Downloaded 27 tweets.
Oklahoma Republican Cruz: Downloaded 23 tweets.
Oklahoma Republican Trump: Downloaded 27 tweets.
Oregon Democrat Clinton: Downloaded 0 tweets.
Oregon Democrat Sanders: Downloaded 0 tweets.
Oregon Republican Rubio: Downloaded 0 tweets.
Oregon Republican Cruz: Downloaded 0 tweets.
Oregon Republican Trump: Downloaded 8 tweets.
Pennsylvania Democrat Clinton: Downloaded 12 tweets.
Pennsylvania Democrat Sanders: Downloaded 16 tweets.
Pennsylvania Republican Rubio: Downloaded 0 tweets.
Pennsylvania Republican Cruz: Downloaded 10 tweets.
Pennsylvania Republican Trump: Downloaded 24 tweets.
Rhode Island Democrat Clinton: Downloaded 4 tweets.
Rhode Island Democrat Sanders: Downloaded 26 tweets.
Rhode Island Republican Rubio: Downloaded 0 tweets.
Rhode Island Republican Cruz: Downloaded 6 tweets.
Rhode Island Republican Trump: Downloaded 6 tweets.
South Carolina Democrat Clinton: Downloaded 7 tweets.
South Carolina Democrat Sanders: Downloaded 43 tweets.
South Carolina Republican Rubio: Downloaded 29 tweets.
South Carolina Republican Cruz: Downloaded 22 tweets.
South Carolina Republican Trump: Downloaded 22 tweets.
South Dakota Democrat Clinton: Downloaded 0 tweets.
South Dakota Democrat Sanders: Downloaded 2 tweets.
South Dakota Republican Rubio: Downloaded 0 tweets.
South Dakota Republican Cruz: Downloaded 0 tweets.
South Dakota Republican Trump: Downloaded 0 tweets.
Tennessee Democrat Clinton: Downloaded 7 tweets.
Tennessee Democrat Sanders: Downloaded 38 tweets.
Tennessee Republican Rubio: Downloaded 21 tweets.
Tennessee Republican Cruz: Downloaded 40 tweets.
Tennessee Republican Trump: Downloaded 58 tweets.
Texas Democrat Clinton: Downloaded 43 tweets.
Texas Democrat Sanders: Downloaded 163 tweets.
Texas Republican Rubio: Downloaded 113 tweets.
Texas Republican Cruz: Downloaded 139 tweets.
Texas Republican Trump: Downloaded 278 tweets.
Utah Democrat Clinton: Downloaded 0 tweets.
Utah Democrat Sanders: Downloaded 40 tweets.
Utah Republican Rubio: Downloaded 0 tweets.
Utah Republican Cruz: Downloaded 12 tweets.
Utah Republican Trump: Downloaded 13 tweets.
Vermont Democrat Clinton: Downloaded 0 tweets.
Vermont Democrat Sanders: Downloaded 4 tweets.
Vermont Republican Rubio: Downloaded 0 tweets.
Vermont Republican Cruz: Downloaded 0 tweets.
Vermont Republican Trump: Downloaded 5 tweets.
Virginia Democrat Clinton: Downloaded 4 tweets.
Virginia Democrat Sanders: Downloaded 14 tweets.
Virginia Republican Rubio: Downloaded 9 tweets.
Virginia Republican Cruz: Downloaded 7 tweets.
Virginia Republican Trump: Downloaded 20 tweets.
Washington Democrat Clinton: Downloaded 0 tweets.
Washington Democrat Sanders: Downloaded 9 tweets.
Washington Republican Rubio: Downloaded 0 tweets.
Washington Republican Cruz: Downloaded 0 tweets.
Washington Republican Trump: Downloaded 4 tweets.
West Virginia Democrat Clinton: Downloaded 0 tweets.
West Virginia Democrat Sanders: Downloaded 10 tweets.
West Virginia Republican Rubio: Downloaded 0 tweets.
West Virginia Republican Cruz: Downloaded 0 tweets.
West Virginia Republican Trump: Downloaded 12 tweets.
Wisconsin Democrat Clinton: Downloaded 4 tweets.
Wisconsin Democrat Sanders: Downloaded 60 tweets.
Wisconsin Republican Rubio: Downloaded 0 tweets.
Wisconsin Republican Cruz: Downloaded 10 tweets.
Wisconsin Republican Trump: Downloaded 23 tweets.
Wyoming Democrat Clinton: Downloaded 0 tweets.
Wyoming Democrat Sanders: Downloaded 0 tweets.
Wyoming Republican Rubio: Downloaded 0 tweets.
Wyoming Republican Cruz: Downloaded 2 tweets.
Wyoming Republican Trump: Downloaded 0 tweets.

In [36]:
for state in tweets.keys():
    total = 0
    for party in tweets[state].keys():
        for candidate in tweets[state][party].keys():
            total += len(tweets[state][party][candidate])
    print(str(total) + ',')


389,
15,
334,
51,
739,
329,
86,
8,
0,
453,
339,
31,
47,
73,
302,
206,
71,
91,
22,
8,
298,
39,
9,
8,
30,
90,
10,
2,
5,
38,
122,
95,
96,
322,
0,
310,
140,
8,
62,
42,
123,
2,
164,
736,
65,
9,
54,
13,
22,
97,
2,

In [19]:
sid = SentimentIntensityAnalyzer()

In [30]:
df = pd.DataFrame(columns=['State', 'Primary', 'Candidate',])

In [31]:
i = 0
for _, state in enumerate(tweets):
    for party in tweets[state].keys():
        for candidate in tweets[state][party].keys():
            df.set_value(i, 'State', state)
            df.set_value(i, 'Primary', party)
            df.set_value(i, 'Candidate', candidate)
            for k in range(len(tweets[state][party][candidate])):
                tweet_text = tweets[state][party][candidate][k]['text']
                score = sid.polarity_scores(str(tweet_text))['compound']
                df.set_value(i, 't' + str(k), score)
            
            i += 1

In [39]:
df[:10]


Out[39]:
State Primary Candidate t0 t1 t2 t3 t4 t5 t6 ... t301 t302 t303 t304 t305 t306 t307 t308 t309 t310
0 Alabama Democrat Clinton 0.0000 0.4995 0.0000 0.0000 0.0000 0.0000 0.0000 ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 Alabama Democrat Sanders -0.1779 -0.1779 0.0000 0.6696 0.0000 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 Alabama Republican Rubio 0.6705 0.5106 0.0000 -0.2481 -0.0516 0.7615 0.0000 ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
3 Alabama Republican Cruz -0.6705 0.0000 0.5719 0.8720 0.0000 -0.3382 0.4389 ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
4 Alabama Republican Trump -0.2023 0.3976 0.0772 0.4215 -0.5469 0.5916 -0.2212 ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
5 Alaska Democrat Clinton NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
6 Alaska Democrat Sanders 0.8221 0.8221 0.0000 0.0000 NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
7 Alaska Republican Rubio 0.0000 0.0000 NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
8 Alaska Republican Cruz 0.4404 0.4404 0.0000 0.0000 0.0000 0.0000 0.0000 ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
9 Alaska Republican Trump NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

10 rows × 314 columns


In [38]:
df.to_csv('data/twitter/twitter_sentiment_scores.csv')