-
Notifications
You must be signed in to change notification settings - Fork 0
/
hierarchy_exctraction_spectrum_sci.py
524 lines (437 loc) · 20.1 KB
/
hierarchy_exctraction_spectrum_sci.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
import json
import string
import re
from collections import defaultdict
from copy import deepcopy
import pprint
def print_contents(box):
print ''
print 'actual ' + box['group_n']
print 'predicted ' + str(box['predicted_group_n'])
print box['contents']
print ''
def make_box_row(row_dict, pos_idx):
new_row = row_dict.copy()
rect = row_dict['rectangle']
new_row['start_x'] = rect[0][0]
new_row['start_y'] = rect[0][1]
new_row['end_x'] = rect[1][0]
new_row['end_y'] = rect[1][1]
new_row['vert_order'] = pos_idx
del new_row['rectangle']
return new_row
def write_predicted_groups(page_results, base_path, test_path):
page = page_results[0]
predicted_boxes = page_results[-1]
page_file_path = base_path + page.replace('jpeg', 'json')
new_file_path = test_path + page.replace('jpeg', 'json')
with open(page_file_path) as f:
all_page_boxes = json.load(f)
for box in predicted_boxes:
all_page_boxes['question'][box['box_id']]['group_n'] = box['predicted_group_n']
with open(new_file_path, 'w') as f:
json.dump(all_page_boxes, f)
def assign_group_numbers_wo_labeled_qs(ordered_question_boxes):
current_group_n = 0
current_outer_indent = ordered_question_boxes[0]['start_x']
last_seen_question_type = 'Unlabeled'
indent_tolerance = 20
separation_tolerance = 20
last_box_end_y = 0
for idx, box in enumerate(ordered_question_boxes):
if box['category'] == 'Unlabeled': #or box['category'] == 'Question':
box['predicted_group_n'] = 0
last_seen_question_type = box['category']
last_box_end_y = box['end_y']
continue
# return 0
type_changed = last_seen_question_type != box['category']
last_seen_question_type = box['category'] if type_changed else last_seen_question_type
vertically_separated = int(box['start_y']) - int(last_box_end_y) > separation_tolerance
# print box['start_y']
# print last_box_end_y
last_box_end_y = box['end_y']
# print 'vs ' + str(vertically_seperated)
# print 'tc ' + str(type_changed)
if vertically_separated and (type_changed or box['start_x'] - indent_tolerance < current_outer_indent ):
current_group_n += 1
box['predicted_group_n'] = current_group_n
return ordered_question_boxes
def assign_group_numbers(ordered_question_boxes):
current_group_n = 0
current_outer_indent = ordered_question_boxes[0]['start_x']
last_seen_question_type = 'Unlabeled'
indent_tolerance = 20
separation_tolerance = 20
last_box_end_y = 0
for idx, box in enumerate(ordered_question_boxes):
if box['category'] == 'Question' or box['category'] == 'Unlabeled':
box['predicted_group_n'] = 0
last_seen_question_type = box['category']
last_box_end_y = box['end_y']
continue
type_changed = last_seen_question_type != box['category']
last_seen_question_type = box['category'] if type_changed else last_seen_question_type
vertically_separated = int(box['start_y']) - int(last_box_end_y) > separation_tolerance
# print box['start_y']
# print last_box_end_y
last_box_end_y = box['end_y']
# print 'vs ' + str(vertically_seperated)
# print 'tc ' + str(type_changed)
if vertically_separated and (type_changed or abs(box['start_x'] - current_outer_indent) < indent_tolerance):
current_group_n += 1
current_outer_indent = box['start_x']
box['group_n'] = current_group_n
return ordered_question_boxes
def check_group_numbers(ordered_boxes_w_pred):
n_wrong = 0
for box in ordered_boxes_w_pred:
if int(box['group_n']) != box['predicted_group_n']:
n_wrong += 1
return n_wrong, len(ordered_boxes_w_pred), 1 - n_wrong / float(len(ordered_boxes_w_pred))
def predict_groups(pages, base_path):
total_num_boxes = 0
for page in pages:
page_file_path = base_path + page.replace('jpeg', 'json')
with open(page_file_path) as f:
page_boxes = json.load(f)
for qn, qv in page_boxes['question'].items():
total_num_boxes += 1
del qv['source']
del qv['score']
del qv['v_dim']
q_series = page_boxes['question']
vertically_ordered_question = sorted(q_series.values(), key=lambda x: (x['rectangle'][0][1], x['rectangle'][0][0]))
vertically_ordered_question_feat = [make_box_row(box, idx) for idx, box in enumerate(vertically_ordered_question)]
boxes_w_predicts = assign_group_numbers(vertically_ordered_question_feat)
return boxes_w_predicts
def predict_and_verify_groups(pages, base_path):
results_by_page = []
total_num_boxes = 0
for page in pages:
page_file_path = base_path + page.replace('jpeg', 'json')
with open(page_file_path) as f:
page_boxes = json.load(f)
for qn, qv in page_boxes['question'].items():
total_num_boxes += 1
del qv['source']
del qv['score']
del qv['v_dim']
q_series = page_boxes['question']
vertically_ordered_question = sorted(q_series.values(), key=lambda x: (x['rectangle'][0][1], x['rectangle'][0][0]))
vertically_ordered_question_feat = [make_box_row(box, idx) for idx, box in enumerate(vertically_ordered_question)]
boxes_w_predicts = assign_group_numbers(vertically_ordered_question_feat)
if boxes_w_predicts:
n_wrong, box_n_this_page, fraction_right = check_group_numbers(boxes_w_predicts)
total_num_boxes += box_n_this_page
if fraction_right:
results_by_page.append((page, n_wrong, fraction_right, boxes_w_predicts))
total_wrong = sum([res[1] for res in results_by_page])
overall_accuracy = 1 - total_wrong / float(total_num_boxes)
return overall_accuracy, total_wrong, total_num_boxes, results_by_page
def label_multi_choice_components(base_path, pages):
results_by_page = []
total_num_boxes = 0
for page in pages:
page_file_path = base_path + page.replace('jpeg', 'json')
with open(page_file_path) as f:
page_boxes = json.load(f)
for qn, qv in page_boxes['question'].items():
total_num_boxes += 1
q_series = page_boxes['question']
vertically_ordered_question = sorted(q_series.values(), key=lambda x: (x['rectangle'][0][1], x['rectangle'][0][0]))
vertically_ordered_question_feat = [make_box_row(box, idx) for idx, box in enumerate(vertically_ordered_question)]
boxes_w_predicts = assign_group_numbers(vertically_ordered_question_feat)
if boxes_w_predicts:
n_wrong, box_n_this_page, fraction_right = check_group_numbers(boxes_w_predicts)
total_num_boxes += box_n_this_page
if fraction_right:
results_by_page.append((page, n_wrong, fraction_right, boxes_w_predicts))
total_wrong = sum([res[1] for res in results_by_page])
overall_accuracy = 1 - total_wrong / float(total_num_boxes)
return overall_accuracy, total_wrong, total_num_boxes, results_by_page
def parse_tf_questions(question_group, first_pass=True):
def make_question_entry(ask_index, complete_questions, qv, structural_label):
last_type_seen[0] = 'question'
current_question_id[0] = 'full_Q_' + structural_label
complete_questions[current_group_n][current_question_id[0]] = {
"question_id": current_question_id[0],
"category": box_category,
"structural_id": structural_label,
"asks": {'Qc' + str(ask_index): qv},
"answer_choices": {},
}
def make_instructions_entry(instuctions, inst_id):
instructions[current_group_n] = {
"instruction": {'I' + str(inst_id): qv},
}
numeric_starters = [str(n) + '.' for n in range(10)]
complete_questions = defaultdict(dict)
instructions = defaultdict(dict)
last_type_seen = [0]
current_question_id = [0]
ask_index = 0
current_group_n = 0
instructions_idx = 0
for qv in question_group:
current_group_n = 'G' + str(qv['group_n'])
box_category = qv['category']
box_text = qv['contents'].replace('(', '').replace(')', '').replace('O', '')
print box_text
del qv['source']
del qv['score']
del qv['v_dim']
del qv['category']
if box_text[:2] in numeric_starters:
print 'qv'
last_question_structural_id = box_text[:2]
ask_index += 1
make_question_entry(ask_index, complete_questions, qv, last_question_structural_id)
elif ask_index and not ('true' in box_text.lower() or 'false' in box_text.lower()):
ask_index += 1
complete_questions[current_group_n][current_question_id[0]]['asks']['Qc' + str(ask_index)] = qv
return complete_questions, instructions
def parse_fib_questions(question_group, first_pass=True):
def make_possible_answer_entry(structural_label, complete_questions, qv):
last_type_seen[0] = 'possible_answer'
choice_id = "answer_choice_" + structural_label
complete_questions[current_group_n][current_question_id[0]]['answer_choices'][choice_id] = {
"structural_label": structural_label,
"possible_answer": qv
}
def make_question_entry(ask_index, complete_questions, qv, structural_label):
last_type_seen[0] = 'question'
box_text = qv['contents']
current_question_id[0] = 'full_Q_' + structural_label
complete_questions[current_group_n][current_question_id[0]] = {
"question_id": current_question_id[0],
"category": box_category,
"structural_id": structural_label,
"asks": {'Qc' + str(ask_index): qv},
"answer_choices": {},
}
def make_instructions_entry(instuctions, inst_id):
instructions[current_group_n] = {
"instruction": {'I' + str(inst_id): qv},
}
numeric_starters = [str(n) + '.' for n in range(10)]
letter_punct_starters = [char + '.' for char in string.ascii_uppercase[:6]]
complete_questions = defaultdict(dict)
instructions = defaultdict(dict)
q_series = deepcopy(question_group)
vertically_ordered_question = sorted(q_series.values(),
key=lambda x: (x['rectangle'][0][1], x['rectangle'][0][0]))
last_type_seen = [0]
misfits_created = False
current_question_id = [0]
ask_index = 0
unlabeled_choice_idx = 1
current_group_n = 0
instructions_idx = 0
for qv in vertically_ordered_question:
current_group_n = 'G' + str(qv['group_n'])
box_category = qv['category']
box_text = qv['contents'].replace('(', '').replace(')', '').replace('O', '')
del qv['source']
del qv['score']
del qv['v_dim']
del qv['category']
if box_text[:2] in letter_punct_starters:
if first_pass:
instructions_idx += 1
make_instructions_entry(instructions, instructions_idx)
else:
last_question_structural_id = box_text[:2]
ask_index += 1
make_question_entry(ask_index, complete_questions, qv, last_question_structural_id)
elif box_text[:2] in numeric_starters:
last_question_structural_id = box_text[:2]
ask_index += 1
make_question_entry(ask_index, complete_questions, qv, last_question_structural_id)
elif ask_index:
ask_index += 1
complete_questions[current_group_n][current_question_id[0]]['asks']['Qc' + str(ask_index)] = qv
elif not first_pass:
if not misfits_created:
complete_questions[current_group_n]['misfits'] = {
}
complete_questions[current_group_n]['misfits'][qv['box_id']] = {
"category": box_category,
"asks": {'M' + str(ask_index): qv},
"answer_choices": {}
}
misfits_created = True
elif misfits_created:
complete_questions[current_group_n]['misfits'][qv['box_id']] = {
"category": box_category,
"asks": {'M' + str(ask_index): qv},
"answer_choices": {}
}
elif not first_pass:
pass
print 'miss'
print box_text
print
if not complete_questions and instructions:
pass
return parse_sa_questions(question_group, False)
return complete_questions, instructions
def parse_sa_questions(question_group, first_pass=True):
def make_question_entry(ask_index, complete_questions, qv, structural_label):
last_type_seen[0] = 'question'
current_question_id[0] = 'full_Q_' + structural_label
complete_questions[current_group_n][current_question_id[0]] = {
"question_id": current_question_id[0],
"category": box_category,
"structural_id": structural_label,
"asks": {'Qc' + str(ask_index): qv},
"answer_choices": {},
}
def make_instructions_entry(instuctions, inst_id):
instructions[current_group_n] = {
"instruction": {'I' + str(inst_id): qv},
}
numeric_starters = [str(n) + '.' for n in range(20)]
letter_punct_starters = [char + '.' for char in string.ascii_uppercase[:6]]
complete_questions = defaultdict(dict)
instructions = defaultdict(dict)
last_type_seen = [0]
misfits_created = False
current_question_id = [0]
ask_index = 0
current_group_n = 0
instructions_idx = 0
for qv in question_group:
current_group_n = 'G' + str(qv['group_n'])
box_category = qv['category']
box_text = qv['contents'].replace('(', '').replace(')', '').replace('O', '')
del qv['source']
del qv['score']
del qv['v_dim']
del qv['category']
if box_text[:2] or box_text[:3] in numeric_starters:
last_question_structural_id = box_text[:2]
ask_index += 1
make_question_entry(ask_index, complete_questions, qv, last_question_structural_id)
elif box_text[:2] in letter_punct_starters:
last_question_structural_id = box_text[:2]
ask_index += 1
make_question_entry(ask_index, complete_questions, qv, last_question_structural_id)
elif ask_index:
ask_index += 1
complete_questions[current_group_n][current_question_id[0]]['asks']['Qc' + str(ask_index)] = qv
elif not first_pass:
if not misfits_created:
complete_questions[current_group_n]['misfits'] = {
}
complete_questions[current_group_n]['misfits'][qv['box_id']] = {
"category": box_category,
"asks": {'M' + str(ask_index): qv},
"answer_choices": {}
}
misfits_created = True
elif misfits_created:
complete_questions[current_group_n]['misfits'][qv['box_id']] = {
"category": box_category,
"asks": {'M' + str(ask_index): qv},
"answer_choices": {}
}
elif not first_pass:
pass
print 'miss'
print box_text
print
else:
instructions_idx += 1
make_instructions_entry(instructions, instructions_idx)
# if not complete_questions and instructions:
# pass
# return parse_sa_questions(question_group, False)
return complete_questions, instructions
def parse_mc_questions(question_group):
def make_possible_answer_entry(structural_label, complete_questions, qv):
last_type_seen[0] = 'possible_answer'
choice_id = "answer_choice_" + structural_label
complete_questions[current_group_n][current_question_id[0]]['answer_choices'][choice_id] = {
"structural_label": structural_label,
"possible_answer": qv
}
def make_question_entry(ask_index, complete_questions, qv, structural_label):
last_type_seen[0] = 'question'
box_text = qv['contents']
current_question_id[0] = 'full_Q_' + structural_label
complete_questions[current_group_n][current_question_id[0]] = {
"question_id": current_question_id[0],
"category": box_category,
"structural_id": structural_label,
"asks": {'Qc' + str(ask_index): qv},
"answer_choices": {},
}
def make_instructions_entry(instuctions, inst_id):
instructions[current_group_n] = {
"instruction": {'I' + str(inst_id): qv},
}
numeric_starters = [str(n) + '.' for n in range(10)]
letter_starters = [char + '.' for char in string.ascii_uppercase[:6]]
letter_starters += [char + '.' for char in string.ascii_lowercase[:6]]
complete_questions = defaultdict(dict)
instructions = defaultdict(dict)
last_type_seen = [0]
current_question_id = [0]
ask_index = 0
unlabeled_choice_idx = 1
current_group_n = 0
instructions_idx = 0
for qv in question_group:
current_group_n = 'G' + str(qv['group_n'])
box_category = qv['category']
box_text = qv['contents'].replace('(', '').replace(')', '').replace('O', '')
del qv['source']
del qv['score']
del qv['v_dim']
del qv['category']
if box_text[:2] in numeric_starters:
last_question_structural_id = box_text[:2]
ask_index += 1
make_question_entry(ask_index, complete_questions, qv, last_question_structural_id)
elif box_text[:2] in letter_starters:
structural_label = box_text[:2]
make_possible_answer_entry(structural_label, complete_questions, qv)
elif current_question_id[0]:
if last_type_seen[0] == 'possible_answer':
structural_label = 'Z' + str(unlabeled_choice_idx)
unlabeled_choice_idx += 1
make_possible_answer_entry(structural_label, complete_questions, qv)
elif last_type_seen[0] == 'question':
ask_index += 1
complete_questions[current_group_n][current_question_id[0]]['asks']['Qc' + str(ask_index)] = qv
else:
instructions_idx += 1
make_instructions_entry(instructions, instructions_idx)
return complete_questions, instructions
def parse_book(pages, file_path):
for page in pages:
page_file_path = file_path + page.replace('jpeg', 'json')
with open(page_file_path) as f:
page_json = json.load(f)
parsed_pages = parse_page_questions(page_json)
return parsed_pages
def parse_page_questions(question_boxes):
all_questions = {}
q_series = question_boxes['question']
vertically_ordered_question = sorted(q_series.values(), key=lambda x: (x['rectangle'][0][1], x['rectangle'][0][0]))
vertically_ordered_question_feat = [make_box_row(box, idx) for idx, box in enumerate(vertically_ordered_question)]
all_question_boxes = assign_group_numbers(vertically_ordered_question_feat)
mc_questions = [box for box in all_question_boxes if box['category'] == 'Multiple Choice']
sa_questions = [box for box in all_question_boxes if box['category'] == 'Short Answer']
tf_questions = [box for box in all_question_boxes if box['category'] == 'True/False']
fib_questions = [box for box in all_question_boxes if box['category'] == 'Fill-In-the-Blank']
parsed_mc_qs, parsed_mc_instructions = parse_mc_questions(mc_questions)
parsed_sa_qs, parsed_sa_instructions = parse_sa_questions(sa_questions)
parsed_tf_qs, parsed_tf_instructions = parse_tf_questions(tf_questions)
# parsed_fib_qs, parsed_fib_instructions = parse_fib_questions(fib_questions)
all_questions.update(parsed_sa_qs)
all_questions.update(parsed_mc_qs)
all_questions.update(parsed_tf_qs)
# all_questions.update(parsed_fib_qs)
return all_questions