-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
599 lines (507 loc) · 14.6 KB
/
main.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
"""Generates book trivia game questions"""
import nltk
from sklearn.feature_extraction.text import TfidfVectorizer
import pandas as pd
from collections import Counter
import numpy as np
from sklearn.neighbors import KernelDensity
from pathlib import Path
from gutenberg.acquire import load_etext
from gutenberg.cleanup import strip_headers
from nltk.corpus import stopwords
import string
import json
from functools import lru_cache
kernel_bandwidth = 1.5
outlier_quantile = 0.98
basic_stopwords_set = set(stopwords.words("english"))
punctuation_set = set(string.punctuation).union(
{"”", "’", "``", "“", "''", "n't", "--", "'ll", "'s", "'re", "'em", "'d", "'m"}
)
custom_stop_words = {
"mr.",
"miss",
"mrs.",
"said",
"mr",
"mrs",
"th",
"chapter",
"l.",
"well.",
"—the",
"—",
"....",
"de",
"niggers",
}
custom_non_adj = {
"alarmed",
"guildenstern",
"grimpen",
"jurgis",
"ii",
"e",
"osric",
"iii",
"iv",
"polonius",
"unto",
"hath",
"doth",
"forc",
"rous",
"stay",
"ahab",
"starbuck",
"moby",
"stubb",
"queequeg",
"mortimer",
"sherlock",
"ye",
"nigh",
"fast-fish",
"whale",
"sperm",
"loose-fish",
"astern",
"not.",
"northumberland",
"ona",
"packingtown",
"twenty-five",
"teta",
"marija",
"saturday",
"mast-head",
"baskerville",
"hundred-dollar",
"tree",
"marlow",
"kurtz",
"six-inch",
"diary",
"to-night",
"to-day",
"dr.",
"madam",
"quincey",
"to-morrow",
"van",
"dublin",
"kathleen",
"next-door",
"frank",
"lenehan",
"ivy",
"admiral",
"dr.",
"bible",
"chest",
"flint",
"ben",
"israel",
"yo-ho-ho",
"livesey",
"n",
"captain",
"_jonathan",
"lucy",
"london",
"baggot",
"freddy",
"gabriel",
"constable",
"grafton",
"dooty",
"tavern",
"thimble",
"forecastle",
"ship",
"cap",
"agonised",
"_hail",
"music-hall",
"e.",
"spy-glass",
"sparred",
"grown",
"attendants._",
"hamlet._",
"soldier",
"good-bye",
"oxford",
"_times_",
"absent-minded",
"selden",
"tommy",
"antanas",
"unfold",
"hid",
"right.",
"curly-haired",
"aniele",
"polish",
"lucy's",
"whilst",
"working-man",
"cheese",
"round-shot",
"madness",
"lean-jawed",
"supra-orbital",
"arrive",
"black-bearded",
"gatsby",
"daisy",
"jordan",
"myrtle",
"nick",
"...",
"ga-od",
"neighbour",
"prep",
"wilson",
"jay",
"insisted",
"return",
"finger-tips",
"…",
"hampstead",
"aunt",
"enter",
"underwear",
"fitzpatrick",
}
custom_non_verb = {
"sir",
"jurgis",
"ahab",
"queequeg",
"leeward",
"stubb",
"play._",
"polonius",
"en",
"doth",
"laertes",
"hamlet",
"starbuck",
"pearl",
"hester",
"baskerville",
"dr",
"holmes",
"stapleton",
"coombe",
"devonshire",
"did.",
"think.",
"watson",
"dr.",
"baker",
"helsing",
"ca",
"godalming",
"lucy",
"huts",
"van",
"jonathan",
"arthur",
"'ve",
"mina",
"wo",
"m.",
"dantès",
"albert",
"franz",
"danglars",
"morrel",
"heaven",
"valentine",
"leatherhead",
"castruccio",
"rome",
"ai",
"quincey",
"renfield",
"whitby",
"fernand",
"hawkins",
"madam",
"mademoiselle",
"alexander",
"fortune",
"perkins",
"curved",
"outlying",
"merripit",
"barrymore",
"sake",
"selden",
"_is_",
"woking",
}
custom_non_noun = {
"ye",
"nigh",
"thou",
"gatsby",
"daisy",
"tom",
"jordan",
"wilson",
"hester",
"prynne",
"pearl",
"dimmesdale",
"scarlet",
}
all_stop_words = basic_stopwords_set.union(punctuation_set).union(custom_stop_words)
def unique_and_score_kde(kde, X):
X_unique = np.unique(X)
Y = np.exp(kde.score_samples(X_unique.reshape(len(X_unique), 1)))
result = np.concatenate(
(np.array([0, 0]).reshape(2, 1), np.unique(np.array((X_unique, Y)), axis=1)),
axis=1,
)
return [tuple(r) for r in result.T]
def trim_sent_len_outliers(arr):
return arr[arr < np.quantile(arr, outlier_quantile)]
@lru_cache
def load_and_strip(book_id):
return strip_headers(load_etext(book_id))
def get_probability_vec(lengths, max_sent_length):
result = np.zeros(max_sent_length + 1)
word_lengths, counts = np.unique(lengths, return_counts=True)
for i, word_length in enumerate(word_lengths):
result[word_length] = counts[i]
return result / result.sum()
def get_sentence_lengths_num_words(text):
return [len(nltk.word_tokenize(s)) for s in nltk.sent_tokenize(text)]
def get_word_lengths(text):
return [len(s) for s in nltk.word_tokenize(text)]
def word_length_question(q):
books = [load_and_strip(id) for id in q["answers"]]
# each book is its own row (along axis 0)
book_word_lengths = [np.array(get_word_lengths(text)) for text in books]
max_avg_index = np.argmax([b.mean() for b in book_word_lengths])
all_book_length = np.concatenate(book_word_lengths, axis=0)
all_book_length.sort()
kde_all = KernelDensity(bandwidth=kernel_bandwidth, kernel="gaussian")
kde_all.fit(all_book_length.reshape(len(all_book_length), 1))
answer_word_lengh = book_word_lengths[max_avg_index]
answer_word_lengh.sort()
kde_answer = KernelDensity(bandwidth=kernel_bandwidth, kernel="gaussian")
kde_answer.fit(answer_word_lengh.reshape(len(answer_word_lengh), 1))
return dict(
correct_answer=q["answers"][max_avg_index],
data_all_answer=dict(
all_points=unique_and_score_kde(kde_all, all_book_length),
answer_points=unique_and_score_kde(kde_answer, answer_word_lengh),
),
)
def sentence_length_question(q, min_or_max):
books = [load_and_strip(id) for id in q["answers"]]
book_sent_lengths = [get_sentence_lengths_num_words(text) for text in books]
book_median_sent_lengths = [np.median(lengths) for lengths in book_sent_lengths]
answer_choosing_function = np.argmin if min_or_max == "min" else np.argmax
answer_index = answer_choosing_function(book_median_sent_lengths)
# get kde estimates
other_sent_length = trim_sent_len_outliers(
np.array(
sorted(
[
s
for i, sents in enumerate(book_sent_lengths)
if i != answer_index
for s in sents
]
)
)
)
answer_sent_length = trim_sent_len_outliers(
np.array(sorted(book_sent_lengths[answer_index]))
)
kde_others = KernelDensity(bandwidth=kernel_bandwidth, kernel="gaussian")
kde_others.fit(other_sent_length.reshape(len(other_sent_length), 1))
kde_answer = KernelDensity(bandwidth=kernel_bandwidth, kernel="gaussian")
kde_answer.fit(answer_sent_length.reshape(len(answer_sent_length), 1))
return dict(
correct_answer=q["answers"][answer_index],
data_other_and_answer=dict(
other_points=unique_and_score_kde(kde_others, other_sent_length),
answer_points=unique_and_score_kde(kde_answer, answer_sent_length),
),
)
def get_adj_from_book(book_id):
return [
word
for word, tag in get_tagged_words_for_book(book_id)
if tag == "ADJ" and word not in custom_non_adj
]
def get_verb_from_book(book_id):
return [
word
for word, tag in get_tagged_words_for_book(book_id)
if tag == "VERB" and word not in custom_non_verb
]
def words_best_associated_with_book_question(words_per_book, q):
string_book_words = [" ".join(words) for words in words_per_book]
chosen_words = [x[0] for x in get_tf_idf_question(q, books=string_book_words)]
index_of_correct_answer = get_correct_answer_index(q)
freq_dist = nltk.FreqDist(words_per_book[index_of_correct_answer])
return dict(data_word_and_freq=[(w, freq_dist.get(w)) for w in chosen_words])
def pos_quesiton(q):
if q["meta"]["sub_type"] == "adj":
book_adjectives = [get_adj_from_book(book_id) for book_id in q["answers"]]
return words_best_associated_with_book_question(book_adjectives, q)
elif q["meta"]["sub_type"] == "verb":
book_verbs = [get_verb_from_book(book_id) for book_id in q["answers"]]
return words_best_associated_with_book_question(book_verbs, q)
elif q["meta"]["sub_type"] == "noun":
nouns = [
word
for word, tag in get_tagged_words_for_book(q["correct_answer"])
if tag == "NOUN" and word not in custom_non_noun
]
return dict(
data_word_and_freq=nltk.FreqDist(nouns).most_common(q["meta"]["num_words"])
)
raise Exception(
f"Question subtype {q['meta']['sub_type']} for meta {q['meta']} not supported"
)
def get_tf_idf_question(q, books=None):
books = books or [load_and_strip(book_id) for book_id in q["answers"]]
vectorizer = TfidfVectorizer(stop_words=all_stop_words)
X = vectorizer.fit_transform(books)
df = pd.DataFrame(
data=X.toarray(),
index=range(len(books)),
columns=vectorizer.get_feature_names_out(X),
)
index_of_correct_answer = get_correct_answer_index(q)
return [
(i, v)
for i, v in df.T.iloc[:, index_of_correct_answer]
.sort_values(ascending=False)[: q["meta"]["num_words"]]
.iteritems()
]
def filter_words(words):
return [w for w in words if w not in all_stop_words]
def get_words_per_book(book_ids):
return [
filter_words(nltk.word_tokenize(str.lower(load_and_strip(book_id))))
for book_id in book_ids
]
def get_unique_words(words_per_book, correct_answer_index):
correct_words = words_per_book[correct_answer_index]
other_sets = set(
[
word
for i, words in enumerate(words_per_book)
for word in words
if i != correct_answer_index
]
)
return set(correct_words) - other_sets
def get_unique_verbs(q):
verbs_per_book = [get_verb_from_book(book_id) for book_id in q["answers"]]
return get_unique_most_common(q, passed_words_per_book=verbs_per_book)
def get_unique_adj(q):
adj_per_book = [get_adj_from_book(book_id) for book_id in q["answers"]]
return get_unique_most_common(q, passed_words_per_book=adj_per_book)
def get_correct_answer_index(q):
return q["answers"].index(q["correct_answer"])
def get_unique_most_common(q, passed_words_per_book=None):
words_per_book = passed_words_per_book or get_words_per_book(q["answers"])
index_of_correct_answer = get_correct_answer_index(q)
unique_words = get_unique_words(words_per_book, index_of_correct_answer)
correct_words = words_per_book[index_of_correct_answer]
return Counter([w for w in correct_words if w in unique_words]).most_common(
q["meta"]["num_words"]
)
def get_unique_longest(q):
words_per_book = get_words_per_book(q["answers"])
index_of_correct_answer = get_correct_answer_index(q)
unique_words = get_unique_words(words_per_book, index_of_correct_answer)
words_sorted_by_length = sorted([(len(w), w) for w in unique_words], reverse=True)
# filter out words with hypens or punctuation
punc_set = set(string.punctuation).union("—")
return [
w for _, w in words_sorted_by_length if not set(punc_set).intersection(set(w))
][: q["meta"]["num_words"]]
@lru_cache
def get_tagged_words_for_book(book_id):
book = load_and_strip(book_id)
sents = [nltk.word_tokenize(str.lower(s)) for s in nltk.sent_tokenize(book)]
tagged_sents = nltk.pos_tag_sents(sents)
tagged_words = [
(word, nltk.tag.map_tag("en-ptb", "universal", tag))
for s in tagged_sents
for word, tag in s
]
return [x for x in tagged_words if x[0] not in all_stop_words]
def process_question(q):
if q["meta"]["type"] == "pos-question":
question_res = pos_quesiton(q)
return {
**q,
**dict(data_word_and_freq=question_res["data_word_and_freq"]),
**dict(
correct_answer=q.get(
"correct_answer", question_res.get("correct_answer")
)
),
}
elif q["meta"]["type"] == "tf-idf":
return {**q, **dict(data_word_and_freq=get_tf_idf_question(q))}
elif q["meta"]["type"] == "longest-median-sent-length":
return {**q, **sentence_length_question(q, "max")}
elif q["meta"]["type"] == "shortest-median-sent-length":
return {**q, **sentence_length_question(q, "min")}
elif q["meta"]["type"] == "word-length":
return {**q, **word_length_question(q)}
elif q["meta"]["type"] == "unique-most-common":
return {**q, **dict(data_word_and_freq=get_unique_most_common(q))}
elif q["meta"]["type"] == "unique-verb":
return {**q, **dict(data_word_and_freq=get_unique_verbs(q))}
elif q["meta"]["type"] == "unique-adj":
return {**q, **dict(data_word_and_freq=get_unique_adj(q))}
elif q["meta"]["type"] == "unique-longest":
return {**q, **dict(data_words_only=get_unique_longest(q))}
raise Exception(
f"Question type {q['meta']['type']} for meta {q['meta']} not supported"
)
def id_to_info(books_map, i):
entry = books_map[i]
return dict(id=i, title=entry["title"], author=entry["author"])
def build_game(path_to_game_json):
game_data = json.loads(Path(path_to_game_json).read_text())
books = game_data["books"]
book_map = {b["id"]: b for b in books}
answer_ids = [b["id"] for b in books]
questions = [{**q, **dict(answers=answer_ids)} for q in game_data["questions"]]
finished_questions = [process_question(q) for q in questions]
final_questions = [
{
**q,
**{"correct_answer": id_to_info(book_map, q["correct_answer"])},
**{"answers": [id_to_info(book_map, a_id) for a_id in q["answers"]]},
}
for q in finished_questions
]
return dict(questions=final_questions, books=books)
def main():
games = [
build_game("book_data/game_1.json"),
build_game("book_data/game_2.json"),
build_game("book_data/game_3.json"),
build_game("book_data/game_4.json"),
]
output_path = Path.cwd().parent.parent / "public" / "data"
output_path.mkdir(parents=True, exist_ok=True)
(output_path / "games.json").write_text(json.dumps(games))
if __name__ == "__main__":
main()