-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalysis.py
377 lines (307 loc) · 13.5 KB
/
analysis.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import operator
import csv
import os
import json
from datetime import datetime
from datetime import timezone
from tkinter import filedialog
from tkinter import *
from collections import Counter
from visualizing import show_bar_chart, show_line_chart
from config import (
PRIMARY_COLOR,
SECONDARY_COLOR,
TERTIARY_COLOR,
EXCLUDED_MEMBERS,
EXCLUDED_WORDS
)
root = Tk()
root.withdraw()
folder = filedialog.askdirectory(title="Select Folder")
folder_name = os.path.basename(os.path.normpath(folder)).split("_")[0]
def get_textual_messages():
messages = {}
x = 0
for file in os.listdir(folder):
if file.endswith(".json"):
file_path = os.path.realpath(folder) + "\\" + str(file)
with open(file_path, "r") as read_file:
data = json.load(read_file)
for message in data['messages']:
if 'content' in message:
message_content = message['content']
message_timestamp = message['timestamp_ms']
message_sender = message['sender_name']
message_tuple = message_timestamp, message_sender, message_content
messages[x] = message_tuple
x += 1
return messages
def get_all_messages():
messages = {}
x = 0
for file in os.listdir(folder):
if file.endswith(".json"):
file_path = os.path.realpath(folder) + "\\" + str(file)
with open(file_path, "r") as read_file:
data = json.load(read_file)
for message in data['messages']:
message_timestamp = message['timestamp_ms']
message_sender = message['sender_name']
message_tuple = message_timestamp, message_sender
messages[x] = message_tuple
x += 1
return messages
def view_total_messages_sent_by_each_member(date_range=False, start=None, end=None):
messages = get_all_messages()
word_count_dict = {}
if date_range:
start_datetime = datetime.strptime(start, '%b %d %Y %I:%M%p')
start_datetime_timestamp = int(start_datetime.replace(
tzinfo=timezone.utc).timestamp()) * 1000
end_datetime = datetime.strptime(end, '%b %d %Y %I:%M%p')
end_datetime_timestamp = int(end_datetime.replace(
tzinfo=timezone.utc).timestamp()) * 1000
for x in messages:
if messages[x][1] not in EXCLUDED_MEMBERS:
y = messages[x][1]
if y not in word_count_dict:
word_count_dict[y] = 0
if date_range:
if start_datetime_timestamp <= messages[x][0] <= end_datetime_timestamp:
word_count_dict[y] += 1
else:
word_count_dict[y] += 1
formatted_count = {}
for x in word_count_dict.keys():
name_split = x.split(" ")
formatted_name = name_split[0] + " " + name_split[-1][:1]
formatted_count[formatted_name] = word_count_dict[x]
formatted_count = dict(sorted(formatted_count.items(),
key=operator.itemgetter(1), reverse=True))
if date_range:
chart_title = "Number of Messages Sent in " + \
folder_name + '\n' + " from " + start + " to " + end
else:
chart_title = "Number of Messages Sent in " + folder_name + '\n' + " All Time"
keys = formatted_count.keys()
values = formatted_count.values()
show_bar_chart(keys, values, PRIMARY_COLOR,
SECONDARY_COLOR, TERTIARY_COLOR, chart_title)
def view_total_characters_sent_by_each_member(date_range=False, start=None, end=None):
messages = get_textual_messages()
character_count_dict = {}
if date_range:
start_datetime = datetime.strptime(start, '%b %d %Y %I:%M%p')
start_datetime_timestamp = int(start_datetime.replace(
tzinfo=timezone.utc).timestamp()) * 1000
end_datetime = datetime.strptime(end, '%b %d %Y %I:%M%p')
end_datetime_timestamp = int(end_datetime.replace(
tzinfo=timezone.utc).timestamp()) * 1000
for x in messages:
if messages[x][1] not in EXCLUDED_MEMBERS:
y = messages[x][1]
if y not in character_count_dict:
character_count_dict[y] = 0
if date_range:
if start_datetime_timestamp <= messages[x][0] <= end_datetime_timestamp:
character_count_dict[y] += len(messages[x][2])
else:
character_count_dict[y] += len(messages[x][2])
formatted_count = {}
for x in character_count_dict.keys():
name_split = x.split(" ")
formatted_name = name_split[0] + " " + name_split[-1][:1]
formatted_count[formatted_name] = character_count_dict[x]
formatted_count = dict(sorted(formatted_count.items(),
key=operator.itemgetter(1), reverse=True))
if date_range:
chart_title = "Number of Characters Sent in " + \
folder_name + '\n' + " from " + start + " to " + end
else:
chart_title = "Number of Characters Sent in " + folder_name + '\n' + " All Time"
keys = formatted_count.keys()
values = formatted_count.values()
show_bar_chart(keys, values, PRIMARY_COLOR,
SECONDARY_COLOR, TERTIARY_COLOR, chart_title)
def view_most_used_words(limit, date_range=False, start=None, end=None):
messages = get_textual_messages()
all_words = []
if date_range:
start_datetime = datetime.strptime(start, '%b %d %Y %I:%M%p')
start_datetime_timestamp = int(start_datetime.replace(
tzinfo=timezone.utc).timestamp()) * 1000
end_datetime = datetime.strptime(end, '%b %d %Y %I:%M%p')
end_datetime_timestamp = int(end_datetime.replace(
tzinfo=timezone.utc).timestamp()) * 1000
for x in messages:
if date_range:
if not (start_datetime_timestamp <= messages[x][0] <= end_datetime_timestamp):
continue
if messages[x][1] not in EXCLUDED_MEMBERS:
split_words = messages[x][2].split()
for w in split_words:
if "âs" in str(w):
w = w.replace("â", "'")
all_words.append(w)
counted_words = Counter(all_words)
for w in EXCLUDED_WORDS:
if w.lower() in counted_words:
del counted_words[w]
most_common_words = counted_words.most_common(limit)
most_common_words = dict(most_common_words)
fileTypes = [('CSV', '*.csv')]
csvFile = filedialog.asksaveasfile(
type='a', filetypes=fileTypes, defaultextension=csv)
csvFile = csvFile.name
if date_range:
header = f"Top {limit} Words Used in {folder_name} from {start} to {end}"
else:
header = f"Top {limit} Words Used in {folder_name} All Time"
with open(csvFile, 'w', newline='', encoding="utf-8") as csvFile:
writer = csv.writer(csvFile)
writer.writerow(header)
for key, value in most_common_words.items():
writer.writerow([key, value])
def find_total_usage_for_each_specified_word(chosed_words, date_range=False, start=None, end=None):
messages = get_textual_messages()
count_dict = {}
if date_range:
start_datetime = datetime.strptime(start, '%b %d %Y %I:%M%p')
start_datetime_timestamp = int(start_datetime.replace(
tzinfo=timezone.utc).timestamp()) * 1000
end_datetime = datetime.strptime(end, '%b %d %Y %I:%M%p')
end_datetime_timestamp = int(end_datetime.replace(
tzinfo=timezone.utc).timestamp()) * 1000
for x in messages:
if date_range:
if not (start_datetime_timestamp <= messages[x][0] <= end_datetime_timestamp):
continue
if messages[x][1] not in EXCLUDED_MEMBERS:
y = messages[x][1]
if y not in count_dict:
count_dict[y] = 0
for w in chosed_words:
if w.lower() in messages[x][2].lower():
count_dict[y] += 1
formatted_count = {}
for x in count_dict.keys():
name_split = x.split(" ")
formatted_name = name_split[0] + " " + name_split[-1][:1]
formatted_count[formatted_name] = count_dict[x]
formatted_count = dict(sorted(formatted_count.items(),
key=operator.itemgetter(1), reverse=True))
if date_range:
chart_title = "Total Usage of the Following Words " + " in " + folder_name + '\n' + " from " \
+ start + " to " + end + ":" + '\n' + str(chosed_words)
else:
chart_title = "Total Usage of the Following Words " + " in " + \
folder_name + " All Time:" + '\n' + str(chosed_words)
keys = formatted_count.keys()
values = formatted_count.values()
show_bar_chart(keys, values, PRIMARY_COLOR,
SECONDARY_COLOR, TERTIARY_COLOR, chart_title)
def find_average_message_length_for_each_member(date_range=False, start=None, end=None):
messages = get_textual_messages()
character_count_dict = {}
message_count_dict = {}
if date_range:
# convert start date+time to datetime, then timestamp as integer
start_datetime = datetime.strptime(start, '%b %d %Y %I:%M%p')
start_datetime_timestamp = int(start_datetime.replace(
tzinfo=timezone.utc).timestamp()) * 1000
# convert end date+time to datetime, then timestamp as integer
end_datetime = datetime.strptime(end, '%b %d %Y %I:%M%p')
end_datetime_timestamp = int(end_datetime.replace(
tzinfo=timezone.utc).timestamp()) * 1000
# get unique names in messages
for x in messages:
if date_range:
if not (start_datetime_timestamp <= messages[x][0] <= end_datetime_timestamp):
continue
if messages[x][1] not in EXCLUDED_MEMBERS:
y = messages[x][1]
# if name is not in name list, add it and set the value to 0
if y not in character_count_dict:
character_count_dict[y] = 0
message_count_dict[y] = 0
# check if message is in date range
character_count_dict[y] += len(messages[x][2])
message_count_dict[y] += 1
average_character_count = {}
for c in character_count_dict.keys():
average_key = c
average_value = character_count_dict[c] / message_count_dict[c]
average_value = round(average_value, 2)
average_character_count[average_key] = average_value
# creates nwe formatted dict with first name and last initial
formatted_count = {}
for x in average_character_count.keys():
name_split = x.split(" ")
formatted_name = name_split[0] + " " + name_split[-1][:1]
formatted_count[formatted_name] = average_character_count[x]
formatted_count = dict(sorted(formatted_count.items(),
key=operator.itemgetter(1), reverse=True))
if date_range:
chart_title = "Average Length of Message Per Person in " + folder_name + '\n' + " from" \
+ start + " to " + end
else:
chart_title = "Average Length of Message Per Person in " + \
folder_name + '\n' + " All Time"
keys = formatted_count.keys()
values = formatted_count.values()
show_bar_chart(keys, values, PRIMARY_COLOR,
SECONDARY_COLOR, TERTIARY_COLOR, chart_title)
def message_count_by_month(date_range=False, start=None, end=None):
messages = get_all_messages()
month_list = []
if date_range:
start_datetime = datetime.strptime(start, '%b %d %Y %I:%M%p')
start_datetime_timestamp = int(start_datetime.replace(
tzinfo=timezone.utc).timestamp()) * 1000
end_datetime = datetime.strptime(end, '%b %d %Y %I:%M%p')
end_datetime_timestamp = int(end_datetime.replace(
tzinfo=timezone.utc).timestamp()) * 1000
for x in messages:
if date_range:
if not (start_datetime_timestamp <= messages[x][0] <= end_datetime_timestamp):
continue
timestamp = int(messages[x][0]) / 1000
dt = datetime.fromtimestamp(timestamp)
message_date = f"{dt.year}-{dt.month}"
if message_date not in month_list:
month_list.append(message_date)
month_list = sorted(month_list, key=lambda x: (
int(x.split("-")[0]), int(x.split("-")[-1])))
firstMonth = month_list[0]
last_month = month_list[-1]
end_month = int(month_list[-1].split("-")[-1])
end_year = int(last_month.split("-")[0])
year_iter = int(firstMonth.split("-")[0])
month_iter = int(firstMonth.split("-")[-1])
new_month_list = []
while year_iter <= end_year:
added_date = str(year_iter) + "-" + str(month_iter)
new_month_list.append(added_date)
month_iter += 1
if month_iter > 12:
year_iter += 1
month_iter = 1
if month_iter > end_month and year_iter >= end_year:
break
month_counts = []
for m in new_month_list:
month_counts.append(0)
for x in messages:
timestamp = int(messages[x][0]) / 1000
dt = datetime.fromtimestamp(timestamp)
message_date = f"{dt.year}-{dt.month}"
month_counts[(new_month_list.index(message_date))] += 1
if date_range:
chart_title = "Messages Sent in " + folder_name + '\n' + " from " \
+ start + " to " + end
else:
chart_title = "Messages Sent in " + folder_name + '\n' + " All Time"
keys = new_month_list
values = month_counts
show_bar_chart(keys, values, PRIMARY_COLOR,
SECONDARY_COLOR, TERTIARY_COLOR, chart_title)