-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsent.py
351 lines (306 loc) · 12.4 KB
/
sent.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from tweepy.streaming import StreamListener
from flask import Flask, render_template
from flask_googlemaps import GoogleMaps
from flask_googlemaps import Map, icons
from train.train import KNNClassifier
from tweepy import OAuthHandler
from tweepy import Stream
import os.path as path
from config import *
import pandas as pd
import threading
import random
import json
## Creating Flask app. Setting Template location
app = Flask(__name__, template_folder="templates")
app.config['GOOGLEMAPS_KEY'] = google_token_key
GoogleMaps(app, key=google_token_key)
##Possible Marker Colors
icons = [icons.dots.brown, icons.dots.green, icons.dots.yellow, icons.dots.red, icons.dots.orange, icons.dots.blue, icons.dots.black]
class Markers(object):
'''Marker Class: A Singleton list of Markers for the map'''
class __Markers:
def __init__(self):
self.val = [None]
def __str__(self):
return ('self' + '\n'.join(self.val))
instance = None
def __new__(cls): # __new__ always a classmethod
if not Markers.instance:
Markers.instance = Markers.__Markers()
return Markers.instance
def __getattr__(self, name):
return getattr(self.instance, name)
def __setattr__(self, name):
return setattr(self.instance, name)
@app.route("/")
def fullmap():
'''Full Map Flask Route function. Loads Markers'''
m = Markers()
fullmap = Map(
identifier="fullmap",
varname="fullmap",
style=(
"height:100%;"
"width:100%;"
"top:0;"
"left:0;"
"position:absolute;"
"z-index:200;"
),
lat=37.0902,
lng=-98.35,
markers= m.val,
zoom = "5",
)
return render_template('example_fullmap.html', fullmap=fullmap)
@app.route("/statistics/<k>")
def statistics(k):
'''Basic Statics Information Function.'''
k = int(k)
df = pd.read_csv("saved_data/stats.csv", names=["", "author", "city", "state", "lat", "lng", "text","sentiment", "category"])
df = df.drop_duplicates()
labels = []
values = []
limits = []
## City Count - Top k
labs = list(df.city.unique())
vals = []
for label in labs:
vals.append((df[df.city == label]).shape[0])
zipped = list(zip(labs,vals))
sort = sorted(zipped, key=lambda x: x[1])
if k < len(labs):
sort = sort[-k:]
values.append([y for x,y in sort if not (y=="None" or (x=="None" or x =="USA")) ])
labels.append([x for x,y in sort if not (y=="None" or (x=="None" or x =="USA"))])
limits.append(max([y for x,y in sort if not (y=="None" or (x=="None" or x =="USA"))]))
## Author Count - Top k
labs = list(df.author.unique())
vals = []
for label in labs:
vals.append((df[df.author == label]).shape[0])
zipped = list(zip(labs,vals))
sort = sorted(zipped, key=lambda x: x[1])
if k < len(labs):
sort = sort[-k:]
values.append([y for x,y in sort])
labels.append([x for x,y in sort])
limits.append(max([y for x,y in sort]))
## States Count - Top k
labs = list(df.state.unique())
vals = []
for label in labs:
vals.append((df[df.state == label]).shape[0])
zipped = list(zip(labs,vals))
sort = sorted(zipped, key=lambda x: x[1])
if k < len(labs):
sort = sort[-k:]
values.append([y for x,y in sort if not (y=="None" or (x=="None" or x =="USA"))])
labels.append([x for x,y in sort if not (y=="None" or (x=="None" or x =="USA"))])
limits.append(max([y for x,y in sort if not (y=="None" or (x=="None" or x =="USA"))]))
## City avg - Top k
labs = list(df.city.unique())
vals = []
for label in labs:
vals.append((df[df.city == label]).sentiment.mean())
zipped = list(zip(labs,vals))
sort = sorted(zipped, key=lambda x: x[1])
if k < len(labs):
sort = sort[-k:]
values.append([y for x,y in sort if not (y=="None" or (x=="None" or x =="USA")) ])
labels.append([x for x,y in sort if not (y=="None" or (x=="None" or x =="USA"))])
limits.append(max([y for x,y in sort if not (y=="None" or (x=="None" or x =="USA"))]))
## States avg - Top k
labs = list(df.state.unique())
vals = []
for label in labs:
vals.append((df[df.state == label]).sentiment.mean())
zipped = list(zip(labs,vals))
sort = sorted(zipped, key=lambda x: x[1])
if k < len(labs):
sort = sort[-k:]
values.append([y for x,y in sort if not (y=="None" or (x=="None" or x =="USA"))])
labels.append([x for x,y in sort if not (y=="None" or (x=="None" or x =="USA"))])
limits.append(max([y for x,y in sort if not (y=="None" or (x=="None" or x =="USA"))]))
## Author avg - Top k
labs = list(df.author.unique())
vals = []
for label in labs:
vals.append((df[df.author == label]).sentiment.mean())
zipped = list(zip(labs,vals))
sort = sorted(zipped, key=lambda x: x[1])
if k < len(labs):
sort = sort[-k:]
values.append([y for x,y in sort])
labels.append([x for x,y in sort])
limits.append(max([y for x,y in sort]))
return render_template('statistics.html', values=values, labels=labels, limit=limits)
@app.route("/city_statistics/<city>/<k>")
def city_statistics(city, k):
'''Statistics for a given City: Top -k most Negative and least Negative people'''
k = int(k)
df = pd.read_csv("saved_data/stats.csv", names=["", "author", "city", "state", "lat", "lng", "text","sentiment", "category"])
df = df.drop_duplicates()
labels = []
values = []
limits = []
## K Most negative people in City
labs = list(df[df.city == city].author.unique())
vals = []
for label in labs:
vals.append((df[df.author == label])[df.sentiment == 5].shape[0])
zipped = list(zip(labs,vals))
sort = sorted(zipped, key=lambda x: x[1])
if k < len(labs):
sort = sort[-k:]
values.append([y for x,y in sort])
labels.append([x for x,y in sort])
limits.append((max([y for x,y in sort])))
## K Most positive people in City
labs = list(df[df.city == city].author.unique())
vals = []
for label in labs:
vals.append((df[df.author == label])[df.sentiment == 1].shape[0])
zipped = list(zip(labs,vals))
sort = sorted(zipped, key=lambda x: x[1])
if k < len(labs):
sort = sort[-k:]
values.append([y for x,y in sort])
labels.append([x for x,y in sort])
limits.append((max([y for x,y in sort])))
## Pie Data Category Ratios
labs = list(df[df.city == city].category.unique())
vals = []
r = lambda: random.randint(0,255)
for label in labs:
vals.append((df[df.city == city])[df.category == label].shape[0])
values.append(vals)
labels.append([typefind(l) for l in labs])
limits.append(max(vals))
return render_template('city_statistics.html', values=values, labels=labels, limit=limits)
@app.route("/state_statistics/<state>/<k>")
def state_statistics(state, k):
'''Statistics for a given State: Top -k most Negative and least Negative people'''
k = int(k)
df = pd.read_csv("saved_data/stats.csv", names=["", "author", "city", "state", "lat", "lng", "text", "sentiment", "category"])
df = df.drop_duplicates()
labels = []
values = []
limits = []
## K Most negative people in City
labs = list(df[df.state == state].author.unique())
vals = []
for label in labs:
vals.append((df[df.author == label])[df.sentiment == 5].shape[0])
zipped = list(zip(labs,vals))
sort = sorted(zipped, key=lambda x: x[1])
if k < len(labs):
sort = sort[-k:]
values.append([y for x,y in sort])
labels.append([x for x,y in sort])
limits.append((max([y for x,y in sort])))
## K Most positive people in City
labs = list(df[df.state == state].author.unique())
vals = []
for label in labs:
vals.append((df[df.author == label])[df.sentiment == 1].shape[0])
zipped = list(zip(labs,vals))
sort = sorted(zipped, key=lambda x: x[1])
if k < len(labs):
sort = sort[-k:]
values.append([y for x,y in sort])
labels.append([x for x,y in sort])
limits.append((max([y for x,y in sort])))
return render_template('state_statistics.html', values=values, labels=labels, limit = limits)
def typefind(val):
if(val == 1):
return("Religion")
elif(val == 2):
return("Gender")
elif(val == 3):
return("Racial")
elif(val == 4):
return("Sexual Orientation")
elif(val == 5):
return("Disability")
elif(val == 6):
return("Political")
elif(val == 7):
return("Other")
class TweetStreamListener(StreamListener):
def __init__(self):
self.classifier = KNNClassifier('labels/train.csv')
# on success
def on_data(self, data):
# decode json
dict_data = json.loads(data)
#Must have a place object, must have a text object
if "text" not in dict_data:
return
if not dict_data.get("place"):
return
#print(json.dumps(dict_data, indent=4, sort_keys=True))
m = Markers() #Get the Markers instance
#Use full_name if exists, otherwise, set None, None - May need changing
if dict_data["place"]["full_name"]:
c1 = dict_data["place"]["full_name"]
c1 = c1.strip().split(", ")
else:
c1 = ["None","None"]
#User info
author = [dict_data["user"]["screen_name"]]
#City from full_name
city = [c1[0]]
try:
#Due to how full_name is weirdly set up, sometimes a place can be Region, Country rather than City, Region, may need to handle that as well
state = [c1[1]]
except IndexError:
state = 'None'
# Use the average of the lat/lngs of the bounding box for the marker coordinates.
lng = [(dict_data["place"]["bounding_box"]["coordinates"][0][0][0] + dict_data["place"]["bounding_box"]["coordinates"][0][1][0] + dict_data["place"]["bounding_box"]["coordinates"][0][2][0] + dict_data["place"]["bounding_box"]["coordinates"][0][3][0])/4]
lat = [(dict_data["place"]["bounding_box"]["coordinates"][0][0][1] + dict_data["place"]["bounding_box"]["coordinates"][0][1][1] + dict_data["place"]["bounding_box"]["coordinates"][0][2][1] + dict_data["place"]["bounding_box"]["coordinates"][0][3][1])/4]
text = [(dict_data["text"])]
#Only US tweets
if(dict_data["place"]["country_code"] == "US"):
sentiment = self.classifier.classify(dict_data['text'], KNNClassifier.SENTIMENT)
category = self.classifier.classify(dict_data['text'], KNNClassifier.CATEGORY)
#Append to Makers list for display purposes
m.val.append({'icon': icons[sentiment], 'lng': lng, 'lat': lat, 'infobox': typefind(category) + ": " + text[0] })
d = {'author': author, 'city': city, 'state':state, 'lat':lat, 'lng': lng, 'text': text, 'sentiment': sentiment, 'category': category}
df = pd.DataFrame(data=d, columns=["author", "city", "state", "lat", "lng", "text", "sentiment", "category"])
with open('saved_data/stats.csv', 'a') as f:
if not path.exists("saved_data/stats.csv"):
df.to_csv(f, header = True)
else:
df.to_csv(f, header = False)
return True
# on failure
def on_error(self, status):
print (status)
def begin_stream():
# create instance of the tweepy tweet stream listener
listener = TweetStreamListener()
# set twitter keys/tokens
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Keywords for tracking:
keywords = [line.strip() for line in open('keywords.txt')]
# create instance of the tweepy stream
stream = Stream(auth, listener)
#Apply filter on keywords
stream.filter(track=keywords, languages = ['en'])
if __name__ == '__main__':
m = Markers() #Load Markers singleton class
old = pd.read_csv('saved_data/stats.csv')
old = old.drop_duplicates() # Handle duplication behavior
## Load up previously pulled tweets into Marker Singleton
for index, row in old.iterrows():
lat = row[4]
lng = row[5]
text = row[6]
sent = row[7]
typ = row[8]
m.val.append({'icon': icons[sent], 'lng': lng, 'lat': lat, 'infobox': typefind(typ) + ": " + text})
threading.Thread(target=begin_stream).start()
app.run(debug=True, use_reloader=True, host= '0.0.0.0')