-
Notifications
You must be signed in to change notification settings - Fork 11
/
analyser.py
285 lines (238 loc) · 9.58 KB
/
analyser.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
import json
import datetime
import pytz
import gender_guesser.detector as gender
import pandas as pd
from tzwhere import tzwhere
gender_guesser = gender.Detector(case_sensitive=False)
tzwhere_ = tzwhere.tzwhere()
# READ JSON FILES FROM TWITTER ARCHIVE!
def check_hashtag(single_tweet):
'''check whether tweet has any hashtags'''
return len(single_tweet['entities']['hashtags']) > 0
def check_media(single_tweet):
'''check whether tweet has any media attached'''
return len(single_tweet['entities']['media']) > 0
def check_url(single_tweet):
'''check whether tweet has any urls attached'''
return len(single_tweet['entities']['urls']) > 0
def check_retweet(single_tweet):
'''
check whether tweet is a RT. If yes:
return name & user name of the RT'd user.
otherwise just return nones
'''
if 'retweeted_status' in single_tweet.keys():
return (single_tweet['retweeted_status']['user']['screen_name'],
single_tweet['retweeted_status']['user']['name'])
else:
return (None, None)
def check_coordinates(single_tweet):
'''
check whether tweet has coordinates attached.
if yes return the coordinates
otherwise just return nones
'''
if 'coordinates' in single_tweet['geo'].keys():
return (single_tweet['geo']['coordinates'][0],
single_tweet['geo']['coordinates'][1])
else:
return (None, None)
def check_reply_to(single_tweet):
'''
check whether tweet is a reply. If yes:
return name & user name of the user that's replied to.
otherwise just return nones
'''
if 'in_reply_to_screen_name' in single_tweet.keys():
name = None
for user in single_tweet['entities']['user_mentions']:
if user['screen_name'] == single_tweet['in_reply_to_screen_name']:
name = user['name']
break
return (single_tweet['in_reply_to_screen_name'], name)
else:
return (None, None)
def convert_time(coordinates, time_utc):
'''
Does this tweet have a geo location? if yes
we can easily convert the UTC timestamp to true local time!
otherwise return nones
'''
if coordinates[0] and coordinates[1]:
timezone_str = tzwhere_.tzNameAt(coordinates[0], coordinates[1])
if timezone_str:
timezone = pytz.timezone(timezone_str)
time_obj_local = datetime.datetime.astimezone(time_utc, timezone)
return time_obj_local
def create_dataframe(tweets):
'''
create a pandas dataframe from our tweet jsons
'''
# initalize empty lists
utc_time = []
longitude = []
latitude = []
local_time = []
hashtag = []
media = []
url = []
retweet_user_name = []
retweet_name = []
reply_user_name = []
reply_name = []
text = []
# iterate over all tweets and extract data
for single_tweet in tweets:
utc_time.append(datetime.datetime.strptime(
single_tweet['created_at'], '%Y-%m-%d %H:%M:%S %z'))
coordinates = check_coordinates(single_tweet)
latitude.append(coordinates[0])
longitude.append(coordinates[1])
local_time.append(convert_time(coordinates, datetime.datetime.strptime(
single_tweet['created_at'], '%Y-%m-%d %H:%M:%S %z')))
hashtag.append(check_hashtag(single_tweet))
media.append(check_media(single_tweet))
url.append(check_url(single_tweet))
retweet = check_retweet(single_tweet)
retweet_user_name.append(retweet[0])
retweet_name.append(retweet[1])
reply = check_reply_to(single_tweet)
reply_user_name.append(reply[0])
reply_name.append(reply[1])
text.append(single_tweet['text'])
# convert the whole shebang into a pandas dataframe
dataframe = pd.DataFrame(data={
'utc_time': utc_time,
'local_time': local_time,
'latitude': latitude,
'longitude': longitude,
'hashtag': hashtag,
'media': media,
'url': url,
'retweet_user_name': retweet_user_name,
'retweet_name': retweet_name,
'reply_user_name': reply_user_name,
'reply_name': reply_name,
'text': text
})
return dataframe
def read_file_index(index_file):
'''
read file that lists all
tweet-containing json files
'''
with open(index_file) as f:
d = f.readlines()[1:]
d = "".join(d)
d = "[{" + d
files = json.loads(d)
return files
def read_single_file(fpath):
'''
read in the json of a single tweet.json
'''
with open(fpath) as f:
d = f.readlines()[1:]
d = "".join(d)
tweets = json.loads(d)
return tweets
def read_files(file_list, base_path):
'''
use the file list as generated by
read_file_index() to read in the json
of all tweet.json files and convert them
into individual data frames.
Returns them so far not concatenated
'''
data_frames = []
for single_file in file_list:
tweets = read_single_file(base_path + '/' + single_file['file_name'])
df_tweets = create_dataframe(tweets)
data_frames.append(df_tweets)
return data_frames
def create_main_dataframe(tweet_index='twitter_archive/data/js/tweet_index.js',
base_directory='twitter_archive'):
file_index = read_file_index(tweet_index)
dataframes = read_files(file_index, base_directory)
dataframe = pd.concat(dataframes)
dataframe = dataframe.sort_values('utc_time', ascending=False)
dataframe = dataframe.set_index('utc_time')
dataframe = dataframe.replace(to_replace={
'url': {False: None},
'hashtag': {False: None},
'media': {False: None}
})
return dataframe
# GENERATE JSON FOR GRAPHING ON THE WEB
def create_all_tweets(dataframe, rolling_frame='180d'):
dataframe_grouped = dataframe.groupby(dataframe.index.date).count()
dataframe_grouped.index = pd.to_datetime(dataframe_grouped.index)
dataframe_mean_week = dataframe_grouped.rolling(rolling_frame).mean()
def create_hourly_stats(dataframe):
def get_hour(x): return x.hour
def get_weekday(x): return x.weekday()
local_times = dataframe.copy()
local_times = local_times.loc[dataframe['local_time'].notnull()]
local_times['weekday'] = local_times['local_time'].apply(get_weekday)
local_times['hour'] = local_times['local_time'].apply(get_hour)
local_times = local_times.replace(to_replace={'weekday':
{0: 'Weekday',
1: 'Weekday',
2: 'Weekday',
3: 'Weekday',
4: 'Weekday',
5: 'Weekend',
6: 'Weekend',
}
})
local_times = local_times.groupby(
[local_times['hour'], local_times['weekday']]).size().reset_index()
local_times['values'] = local_times[0]
local_times = local_times.set_index(local_times['hour'])
return local_times.pivot(columns='weekday', values='values').reset_index()
def predict_gender(dataframe, column_name, rolling_frame='180d'):
'''
take full dataframe w/ tweets and extract
gender for a name-column where applicable
returns two-column df w/ timestamp & gender
'''
def splitter(x): return x.split()[0]
temp = dataframe[column_name].notnull()
gender_column = dataframe.loc[temp][column_name].apply(
splitter).apply(
gender_guesser.get_gender)
gender_dataframe = pd.DataFrame(data={
'time': list(gender_column.index),
'gender': list(gender_column)
})
gender_dataframe = gender_dataframe.set_index('time')
group = [gender_dataframe.index.date, gender_dataframe['gender']]
gender_dataframe_tab = gender_dataframe.groupby(group).size().reset_index()
gender_dataframe_tab['date'] = gender_dataframe_tab['level_0']
gender_dataframe_tab['count'] = gender_dataframe_tab[0]
gender_dataframe_tab = gender_dataframe_tab.drop([0, 'level_0'], axis=1)
gender_dataframe_tab = gender_dataframe_tab.set_index('date')
gender_dataframe_tab.index = pd.to_datetime(gender_dataframe_tab.index)
gdf_pivot = gender_dataframe_tab.pivot(columns='gender', values='count')
gdf_pivot = gdf_pivot.rolling(rolling_frame).mean()
gdf_pivot = gdf_pivot.reset_index()
gdf_pivot['date'] = gdf_pivot['date'].astype(str)
gdf_pivot = gdf_pivot.drop(
['mostly_male', 'mostly_female', 'andy', 'unknown'], axis=1)
return gdf_pivot
# DUMP JSON FOR GRAPHING
def write_json_for_graph(dataframe,
outfile='graph.json',
format='records'):
json_object = dataframe.to_json(orient=format)
with open(outfile, 'w') as f:
f.write(json_object)
def __main__():
dataframe = create_main_dataframe()
retweet_gender = predict_gender(dataframe,'retweet_name','180d')
write_json_for_graph(retweet_gender,'gender_rt.json')
reply_gender = predict_gender(dataframe,'reply_name','180d')
write_json_for_graph(reply_gender, 'gender_reply.json')
if __name__ == "__main__":
main()