-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
361 lines (288 loc) · 11.2 KB
/
parser.py
File metadata and controls
361 lines (288 loc) · 11.2 KB
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
import json
from collections import defaultdict
from datetime import datetime
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import filedialog
# Dictionaries of the names for the label and author ID's
label_names = {
1: "",
2: "",
3: "",
4: "",
5: "",
6: "",
7: "",
8: "",
9: "",
10: ""
}
author_names = {}
events_per_month_year = defaultdict(int)
events_per_label = defaultdict(int)
events_per_author = defaultdict(int)
event_titles = []
# Loads the TimeTree JSON file
def load_json_file():
file_path = filedialog.askopenfilename()
if not file_path:
return
with open(file_path, 'r', encoding='utf-8') as file:
try:
data = json.load(file)
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
return
if 'events' not in data or not isinstance(data['events'], list):
print("Error: JSON data does not contain a list of events under the key 'events'")
return
events = data['events']
# Asks user to give names to the label ID's, filling the dictionary
ask_label_names(events)
def ask_label_names(events):
label_window = tk.Toplevel(root)
label_window.title("Enter Label Names")
tk.Label(label_window, text="Enter names for labels:").pack(pady=10)
entry_vars = {}
for label_id in label_names.keys():
frame = tk.Frame(label_window)
frame.pack(pady=2)
tk.Label(frame, text=f"Label {label_id}:").pack(side=tk.LEFT)
entry_var = tk.StringVar()
entry_vars[label_id] = entry_var
entry = tk.Entry(frame, textvariable=entry_var)
entry.pack(side=tk.LEFT)
def save_labels():
for label_id, entry_var in entry_vars.items():
label_names[label_id] = entry_var.get()
label_window.destroy()
ask_author_names(events)
tk.Button(label_window, text="Save", command=save_labels).pack(pady=10)
# Asks user to give names to the aurthor ID's, filling the dictionary
def ask_author_names(events):
unique_author_ids = set(event['author_id'] for event in events if 'author_id' in event)
author_window = tk.Toplevel(root)
author_window.title("Enter Author Names")
tk.Label(author_window, text="Enter names for authors:").pack(pady=10)
entry_vars = {}
for author_id in unique_author_ids:
frame = tk.Frame(author_window)
frame.pack(pady=2)
tk.Label(frame, text=f"Author {author_id}:").pack(side=tk.LEFT)
entry_var = tk.StringVar()
entry_vars[author_id] = entry_var
entry = tk.Entry(frame, textvariable=entry_var)
entry.pack(side=tk.LEFT)
def save_authors():
for author_id, entry_var in entry_vars.items():
author_names[author_id] = entry_var.get()
author_window.destroy()
process_events(events)
tk.Button(author_window, text="Save", command=save_authors).pack(pady=10)
# Get the event titles and the number of events per label, date (month/year) and user
def process_events(events):
for event in events:
if not isinstance(event, dict):
print(f"Error: Event data is not a dictionary: {event}")
continue
title = event.get('title', 'Untitled')
event_titles.append(title)
start_timestamp = event.get('start_at')
if start_timestamp:
start_datetime = datetime.fromtimestamp(start_timestamp / 1000)
month_year = start_datetime.strftime('%B %Y')
events_per_month_year[month_year] += 1
# Converts the label and author ID's to names from the dictionary
label_id = event.get('label_id')
if label_id in label_names:
label_name = label_names[label_id]
events_per_label[label_name] += 1
author_id = event.get('author_id')
if author_id in author_names:
author_name = author_names[author_id]
events_per_author[author_name] += 1
plot_graphs()
# Takes the result from process_events and plots three graphs showing which labels, months and users have the most events tied to them
def plot_graphs():
sorted_labels = sorted(events_per_label.items(), key=lambda x: x[1], reverse=True)
labels = [item[0] for item in sorted_labels]
counts_labels = [item[1] for item in sorted_labels]
sorted_months = sorted(events_per_month_year.items(), key=lambda x: x[1], reverse=True)
months = [item[0] for item in sorted_months]
counts_months = [item[1] for item in sorted_months]
sorted_authors = sorted(events_per_author.items(), key=lambda x: x[1], reverse=True)
authors = [item[0] for item in sorted_authors]
counts_authors = [item[1] for item in sorted_authors]
plt.figure(figsize=(10, 5))
plt.bar(labels, counts_labels, color='skyblue')
plt.xlabel('Labels')
plt.ylabel('Number of Events')
plt.title('Number of Events per Label')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
plt.figure(figsize=(10, 5))
plt.bar(months, counts_months, color='skyblue')
plt.xlabel('Month Year')
plt.ylabel('Number of Events')
plt.title('Number of Events per Month and Year')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
plt.figure(figsize=(10, 5))
plt.bar(authors, counts_authors, color='skyblue')
plt.xlabel('Authors')
plt.ylabel('Number of Events')
plt.title('Number of Events per Author')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
root = tk.Tk()
root.title("TimeTree Event Parser")
btn_load = tk.Button(root, text="Load JSON File", command=load_json_file)
btn_load.pack(pady=20)
root.mainloop()
import json
from collections import defaultdict
from datetime import datetime
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import filedialog
# Dictionaries of the names for the label and author ID's
label_names = {
1: "",
2: "",
3: "",
4: "",
5: "",
6: "",
7: "",
8: "",
9: "",
10: ""
}
author_names = {}
events_per_month_year = defaultdict(int)
events_per_label = defaultdict(int)
events_per_author = defaultdict(int)
event_titles = []
# Loads the TimeTree JSON file
def load_json_file():
file_path = filedialog.askopenfilename()
if not file_path:
return
with open(file_path, 'r', encoding='utf-8') as file:
try:
data = json.load(file)
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
return
if 'events' not in data or not isinstance(data['events'], list):
print("Error: JSON data does not contain a list of events under the key 'events'")
return
events = data['events']
# Asks user to give names to the label ID's, filling the dictionary
ask_label_names(events)
def ask_label_names(events):
label_window = tk.Toplevel(root)
label_window.title("Enter Label Names")
tk.Label(label_window, text="Enter names for labels:").pack(pady=10)
entry_vars = {}
for label_id in label_names.keys():
frame = tk.Frame(label_window)
frame.pack(pady=2)
tk.Label(frame, text=f"Label {label_id}:").pack(side=tk.LEFT)
entry_var = tk.StringVar()
entry_vars[label_id] = entry_var
entry = tk.Entry(frame, textvariable=entry_var)
entry.pack(side=tk.LEFT)
def save_labels():
for label_id, entry_var in entry_vars.items():
label_names[label_id] = entry_var.get()
label_window.destroy()
ask_author_names(events)
tk.Button(label_window, text="Save", command=save_labels).pack(pady=10)
# Asks user to give names to the aurthor ID's, filling the dictionary
def ask_author_names(events):
unique_author_ids = set(event['author_id'] for event in events if 'author_id' in event)
author_window = tk.Toplevel(root)
author_window.title("Enter Author Names")
tk.Label(author_window, text="Enter names for authors:").pack(pady=10)
entry_vars = {}
for author_id in unique_author_ids:
frame = tk.Frame(author_window)
frame.pack(pady=2)
tk.Label(frame, text=f"Author {author_id}:").pack(side=tk.LEFT)
entry_var = tk.StringVar()
entry_vars[author_id] = entry_var
entry = tk.Entry(frame, textvariable=entry_var)
entry.pack(side=tk.LEFT)
def save_authors():
for author_id, entry_var in entry_vars.items():
author_names[author_id] = entry_var.get()
author_window.destroy()
process_events(events)
tk.Button(author_window, text="Save", command=save_authors).pack(pady=10)
# Get the event titles and the number of events per label, date (month/year) and user
def process_events(events):
for event in events:
if not isinstance(event, dict):
print(f"Error: Event data is not a dictionary: {event}")
continue
title = event.get('title', 'Untitled')
event_titles.append(title)
start_timestamp = event.get('start_at')
if start_timestamp:
start_datetime = datetime.fromtimestamp(start_timestamp / 1000)
month_year = start_datetime.strftime('%B %Y')
events_per_month_year[month_year] += 1
# Converts the label and author ID's to names from the dictionary
label_id = event.get('label_id')
if label_id in label_names:
label_name = label_names[label_id]
events_per_label[label_name] += 1
author_id = event.get('author_id')
if author_id in author_names:
author_name = author_names[author_id]
events_per_author[author_name] += 1
plot_graphs()
# Takes the result from process_events and plots three graphs showing which labels, months and users have the most events tied to them
def plot_graphs():
sorted_labels = sorted(events_per_label.items(), key=lambda x: x[1], reverse=True)
labels = [item[0] for item in sorted_labels]
counts_labels = [item[1] for item in sorted_labels]
sorted_months = sorted(events_per_month_year.items(), key=lambda x: x[1], reverse=True)
months = [item[0] for item in sorted_months]
counts_months = [item[1] for item in sorted_months]
sorted_authors = sorted(events_per_author.items(), key=lambda x: x[1], reverse=True)
authors = [item[0] for item in sorted_authors]
counts_authors = [item[1] for item in sorted_authors]
plt.figure(figsize=(10, 5))
plt.bar(labels, counts_labels, color='skyblue')
plt.xlabel('Labels')
plt.ylabel('Number of Events')
plt.title('Number of Events per Label')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
plt.figure(figsize=(10, 5))
plt.bar(months, counts_months, color='skyblue')
plt.xlabel('Month Year')
plt.ylabel('Number of Events')
plt.title('Number of Events per Month and Year')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
plt.figure(figsize=(10, 5))
plt.bar(authors, counts_authors, color='skyblue')
plt.xlabel('Authors')
plt.ylabel('Number of Events')
plt.title('Number of Events per Author')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
root = tk.Tk()
root.title("TimeTree Event Parser")
btn_load = tk.Button(root, text="Load JSON File", command=load_json_file)
btn_load.pack(pady=20)
root.mainloop()