/
seed.py
221 lines (154 loc) · 6.57 KB
/
seed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import sqlalchemy
from sqlalchemy import func
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import FlushError
from model import User, Tweet, Keyword, TweetKeyword, Candidate
from model import connect_to_db, db
from naive_bayes import run_classifier
from server import app
import datetime
import re
from nltk.tokenize import TweetTokenizer
import sys
reload(sys) # Reload does the trick!
sys.setdefaultencoding('UTF8')
def load_users():
"""Load twitter handles from scraped twitter data file into database"""
# Deleting all existing rows so we don't duplicate -might want to take out if I'm update my database?
# Or create different 'update' function
# User.query.delete()
for row in open("missing_dates_2.txt"):
row = row.rstrip()
tweet_data = row.split("|")
try:
user = User(handle=tweet_data[0])
print user.handle
db.session.add(user)
db.session.commit()
except IntegrityError:
# Uniqueness of handles enforced in class, as same user can have multiple tweets in file
print "Duplicate instance of handle, not added: {}".format(tweet_data[0])
db.session.rollback()
continue
db.session.commit()
################################################################################
def load_candidates():
"""Load presidential and vice presidential candidates"""
# Candidate.query.delete()
for row in open("seed_data/candidates.txt"):
row = row.rstrip()
name, full_name, position, party_affiliation = row.split("|")
candidate = Candidate(name=name,
full_name=full_name,
position=position,
party_affiliation=party_affiliation)
db.session.add(candidate)
db.session.commit()
################################################################################
def parsing_candidates(tweet):
"""
Parsing out references to candidates within tweet and returning either
Trump, Clinton, or Both
"""
Clinton = set(["hillary", "clinton", "hilary", "hrc"])
Trump = set(["trump", "donald"])
tweet = tweet.lower()
clinton_count = 0
trump_count = 0
for word in Clinton:
if tweet.find(word) != -1:
clinton_count += 1
print "Word: {}, Clinton Count: {}".format(word, clinton_count)
for word in Trump:
if tweet.find(word) != -1:
trump_count += 1
print "Word: {}, Trump Count: {}".format(word, trump_count)
if clinton_count > 0 and trump_count > 0:
return "Both"
if clinton_count > 0:
return "Clinton"
if trump_count > 0:
return "Trump"
################################################################################
def load_tweets():
"""Load tweet data from scrapped twitter data file into database"""
# Tweet.query.delete()
for row in open("missing_dates_2.txt"):
row = row.rstrip()
tweet_data = row.split("|")
handle = tweet_data[0]
# Foreign key to users table
user_id = User.query.filter(User.handle == handle).first()
# Converting UTC timestamp
timestamp = datetime.datetime.fromtimestamp(float(tweet_data[3]))
# For tweets that don't have location data
tweet_data[4] = tweet_data[4] or None
tweet_data[5] = tweet_data[5] or None
# Removing URLs from the tweet
clean_tweet = re.sub(r"http\S+", "", tweet_data[2])
nb_classification = run_classifier([clean_tweet])
# Returns Trump/Clinton/Both for sorting later
candidate = parsing_candidates(clean_tweet)
if candidate:
try:
tweet = Tweet(user_id=user_id.user_id,
tweet_id=tweet_data[1],
text=clean_tweet,
timestamp=timestamp,
profile_location=tweet_data[4],
place_id=tweet_data[5],
naive_bayes=nb_classification[0],
referenced_candidate=candidate)
db.session.add(tweet)
db.session.flush()
db.session.commit()
print "Tweet added: {}, {}".format(tweet.tweet_id, tweet.timestamp)
except sqlalchemy.exc.IntegrityError:
print "******flush or integrity error, rolling back! : {}, {}".format(tweet.tweet_id, tweet.timestamp)
# Preventing duplicate tweets from accidentally being added
db.session.rollback()
continue
################################################################################
# Need to actually create the keyword file!
def load_keywords():
"""Load keywords from predefined set of tagged keywords"""
# Keyword.query.delete()
for row in open("seed_data/keywords.txt"):
row = row.rstrip()
keyword, candidate, connotation = row.split("|")
keyword = Keyword(keyword=keyword, related_candidate=candidate, connotation=connotation)
db.session.add(keyword)
db.session.commit()
################################################################################
def load_tweetkeywords():
"""
Check and see which keywords are used in each tweet, and load the association
table linking tweets and keywords
"""
# TweetKeyword.query.delete()
tweets = Tweet.query.all()
keyword_query = Keyword.query.all()
keywords = []
[keywords.append(word.keyword) for word in keyword_query]
tknzr = TweetTokenizer()
for tweet in tweets:
tokenized_tweets = tknzr.tokenize(tweet.text)
for token in tokenized_tweets:
if token in keywords:
tweet_id = Tweet.query.filter(Tweet.tweet_id == tweet.tweet_id).one()
keyword_id = Keyword.query.filter(Keyword.keyword == token).one()
tweet_keyword = TweetKeyword(keyword_id=keyword_id.keyword_id, tweet_id=tweet_id.tweet_id)
print "Added to TweetKeyword table: {}".format(tweet_keyword.keyword_id)
db.session.add(tweet_keyword)
db.session.commit()
################################################################################
if __name__ == "__main__":
connect_to_db(app)
# In case tables haven't been created, create them
db.create_all()
# Import different types of data
load_users()
# load_candidates()
load_tweets()
# load_keywords()
# load_tweetkeywords()