Repository navigation
Expand file tree
/
Copy pathexam_parser.py
More file actions
610 lines (524 loc) · 20.6 KB
/
Copy pathexam_parser.py
File metadata and controls
610 lines (524 loc) · 20.6 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
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
600
601
602
603
604
605
606
607
608
609
610
import re
from typing import Any, Dict, List
from docx import Document
Q_HEADER_RE = re.compile(r"^(Q\d{2,3})\b")
def _clean(text: str) -> str:
return re.sub(r"\s+", " ", text.strip())
def _normalize(text: str) -> str:
text = text.strip().lower()
text = re.sub(r"[\u2013\u2014\-]", "-", text)
text = re.sub(r"[^a-z0-9]+", " ", text)
return re.sub(r"\s+", " ", text).strip()
def _paragraphs(docx_path: str) -> List[str]:
"""Extract all text from document in order, including table cells."""
from docx.oxml import parse_xml
from docx.table import _Cell
doc = Document(docx_path)
out = []
# Process document in element order using body
for elem in doc.element.body:
# Paragraph
if elem.tag.endswith('}p'):
p = None
for para in doc.paragraphs:
if para._element is elem:
p = para
break
if p:
t = _clean(p.text)
if t:
out.append(t)
# Table
elif elem.tag.endswith('}tbl'):
for table in doc.tables:
if table._tbl is elem:
for row in table.rows:
for cell in row.cells:
t = _clean(cell.text)
if t:
out.append(t)
break
return out
def _find_marker(lines: List[str], marker: str, exact_start: bool = False) -> int:
for i, line in enumerate(lines):
if exact_start:
if line.upper().startswith(marker.upper()):
return i
elif marker in line:
return i
return -1
def _split_q_blocks(lines: List[str]) -> Dict[str, List[str]]:
blocks: Dict[str, List[str]] = {}
current_q = None
buffer: List[str] = []
for line in lines:
m = Q_HEADER_RE.match(line)
if m:
if current_q and buffer:
blocks[current_q] = buffer
current_q = m.group(1)
buffer = [line]
continue
if current_q:
buffer.append(line)
if current_q and buffer:
blocks[current_q] = buffer
return blocks
def _parse_options(lines: List[str]) -> List[Dict[str, str]]:
out = []
for line in lines:
m = re.match(r"^([A-Z])\.\s+(.*)$", line)
if m:
out.append({"key": m.group(1), "text": m.group(2).strip()})
return out
def _parse_dropdown_groups(lines: List[str]) -> Dict[str, List[str]]:
groups: Dict[str, List[str]] = {}
current_label = ""
for line in lines:
m = re.match(r"^[\u25b8\-]\s*(.+):\s*$", line)
if m:
current_label = m.group(1).strip()
groups[current_label] = []
continue
if current_label:
if Q_HEADER_RE.match(line):
break
if re.match(r"^[\u25b8\-]\s+", line):
current_label = ""
continue
if line.upper().startswith("EXPLANATION") or line.upper().startswith("ANSWER"):
continue
if line in {"OPTIONS:", "DROPDOWN OPTIONS:", "CORRECT SELECTIONS:", "CORRECT SEQUENCE:"}:
continue
groups[current_label].append(line)
# remove empty/noisy entries
cleaned: Dict[str, List[str]] = {}
for k, vals in groups.items():
vals = [v.strip() for v in vals if v.strip()]
if vals:
cleaned[k] = vals
return cleaned
def _parse_available_values(lines: List[str]) -> List[str]:
in_section = False
values: List[str] = []
for line in lines:
# Match any "Available <word(s)>:" header line (values, segments, options, actions, etc.)
if re.match(r"Available \w[\w\s]*:", line, re.IGNORECASE):
right = line.split(":", 1)[1].strip()
if right:
# Inline slash-separated: "Available values: a / b / c"
return [x.strip() for x in right.split("/") if x.strip()]
# Multi-line bullet section follows
in_section = True
continue
if in_section:
stripped = line.strip()
# bullet characters: •, -, \u2022
if stripped and stripped[0] in ("\u2022", "\u2023", "-", "*", "\u25e6"):
values.append(stripped.lstrip("\u2022\u2023-*\u25e6 ").strip())
elif stripped == "":
continue
else:
in_section = False
return values
def _parse_statements(lines: List[str]) -> List[str]:
out = []
for line in lines:
if re.match(r"^\d+\.\s+", line):
out.append(line)
return out
def _parse_yesno_table_statements(lines: List[str]) -> List[str]:
"""Extract YES/NO statements from table-style exports (non-numbered rows)."""
# Try trigger: "for each statement" header
start_idx = -1
for i, line in enumerate(lines):
if "for each statement" in line.lower():
start_idx = i
break
# Fallback trigger: consecutive Statement / Yes / No table header tokens
if start_idx < 0:
for i, line in enumerate(lines):
if line.strip().lower() == "statement":
# Check if followed by Yes and No tokens
rest = [l.strip().lower() for l in lines[i+1:i+3]]
if rest == ["yes", "no"]:
start_idx = i
break
if start_idx < 0:
return []
out: List[str] = []
for line in lines[start_idx + 1 :]:
t = line.strip()
if not t:
continue
# Stop at obvious section boundaries.
if t in {"OPTIONS:", "DROPDOWN OPTIONS:", "EXHIBIT:", "DETAILED ANSWERS"}:
break
if Q_HEADER_RE.match(t):
break
# Skip table header/noise tokens.
if t.lower() in {"statement", "yes", "no"}:
continue
if t in {"○", "◯", "●"}:
continue
# Keep substantial statement lines.
if len(t.split()) >= 5:
out.append(t)
return out
def _parse_select_count(lines: List[str]) -> int:
for line in lines:
m = re.search(r"select\s+(\d+)", line, flags=re.IGNORECASE)
if m:
return int(m.group(1))
return 1
def _parse_question_block(block: List[str]) -> Dict[str, Any]:
header = block[0]
m = Q_HEADER_RE.match(header)
qcode = m.group(1) if m else ""
topic = ""
tm = re.search(r"Topic\s+\d+", header)
if tm:
topic = tm.group(0)
body = block[1:]
options = _parse_options(body)
dropdown_groups = _parse_dropdown_groups(body)
available_values = _parse_available_values(body)
statements = _parse_statements(body)
if not statements:
statements = _parse_yesno_table_statements(body)
select_count = _parse_select_count(body)
# Noise tokens that come from linearised YES/NO tables — never show in display text.
_NOISE = {"statement", "yes", "no", "○", "◯", "●"}
question_lines = []
for line in body:
if line in {"OPTIONS:", "DROPDOWN OPTIONS:", "EXHIBIT:", "DETAILED ANSWERS", "CORRECT SELECTIONS:", "CORRECT SEQUENCE:"}:
continue
if line.startswith("Available values:"):
question_lines.append(line)
continue
# Drop bare noise tokens (table headers / radio circle chars).
if line.strip().lower() in _NOISE:
continue
question_lines.append(line)
return {
"qcode": qcode,
"topic": topic,
"qtype": "UNKNOWN",
"question_text": "\n".join(question_lines).strip(),
"options": options,
"dropdown_groups": dropdown_groups,
"available_values": available_values,
"statements": statements,
"select_count": select_count,
"correct_answer": {},
"explanation": "",
}
def _parse_detailed_answer_block(block: List[str]) -> Dict[str, Any]:
out: Dict[str, Any] = {
"qtype": "UNKNOWN",
"correct_answer": {},
"explanation": "",
}
type_line = next((l for l in block if l.startswith("Type:")), "")
if type_line:
out["qtype"] = type_line.split(":", 1)[1].strip().upper()
# Handle ANSWER: lines (for MC, MULTI, YESNO)
answer_line = next((l for l in block if l.startswith("ANSWER:")), "")
if answer_line:
value = answer_line.split(":", 1)[1].strip()
out["correct_answer"] = {
"mode": "answer",
"value": value,
}
# Handle ANSWERS: lines (for YESNO with multiple statements)
answers_idx = -1
for i, line in enumerate(block):
if line.startswith("ANSWERS:"):
answers_idx = i
break
if answers_idx >= 0:
items = []
for line in block[answers_idx + 1 :]:
if line.startswith("EXPLANATION:"):
break
if "→" in line and any(yn in line for yn in [" Yes", " No"]):
parts = line.split("→")
if len(parts) == 2:
stmt = parts[0].strip()
answer_val = parts[1].strip()
items.append({"label": stmt, "value": answer_val})
if items:
out["correct_answer"] = {
"mode": "items",
"items": items,
"ordered": False,
}
# Handle CORRECT SELECTIONS: / CORRECT SEQUENCE:
if "CORRECT SELECTIONS:" in block or "CORRECT SEQUENCE:" in block:
marker = (
"CORRECT SELECTIONS:"
if "CORRECT SELECTIONS:" in block
else "CORRECT SEQUENCE:"
)
start = block.index(marker) + 1
items = []
for line in block[start:]:
if line.startswith("EXPLANATION:"):
break
if line.startswith("▸"):
if ":" in line:
left, right = line[1:].split(":", 1)
items.append({"label": left.strip(), "value": right.strip()})
else:
items.append({"label": line[1:].strip(), "value": ""})
elif re.match(r"^(Blank|Step)\s+\d+", line) and ":" in line:
left, right = line.split(":", 1)
items.append({"label": left.strip(), "value": right.strip()})
out["correct_answer"] = {
"mode": "items",
"items": items,
"ordered": marker == "CORRECT SEQUENCE:",
}
exp_idx = -1
for i, line in enumerate(block):
if line.startswith("EXPLANATION:"):
exp_idx = i
break
if exp_idx >= 0:
out["explanation"] = "\n".join(block[exp_idx + 1 :]).strip()
return out
def _parse_quick_answer_key(lines: List[str]) -> Dict[str, str]:
key_map: Dict[str, str] = {}
for line in lines:
m = re.match(r"^(Q\d{2,3}):\s*(.+?)\s+[\u2013\u2014-]\s+Topic\s+\d+\s*$", line)
if not m:
continue
qcode = m.group(1)
value = m.group(2).strip()
key_map[qcode] = value
return key_map
def _infer_correct_answer_from_quick_key(question: Dict[str, Any], raw_value: str) -> Dict[str, Any]:
raw = (raw_value or "").strip()
if not raw:
return {}
# YES/NO statements
statements = question.get("statements") or []
if statements:
yn_values = re.findall(r"\b(Yes|No)\b", raw, flags=re.IGNORECASE)
if yn_values:
items = []
for i, statement in enumerate(statements):
label = statement.replace(" YES / NO", "").strip()
if i < len(yn_values):
items.append({"label": label, "value": yn_values[i].title()})
if items:
return {
"mode": "items",
"items": items,
"ordered": False,
}
# YES/NO without parsed statements yet: create generic statement labels.
yn_values = re.findall(r"\b(Yes|No)\b", raw, flags=re.IGNORECASE)
if yn_values:
items = [
{"label": f"Statement {i + 1}", "value": val.title()}
for i, val in enumerate(yn_values)
]
return {
"mode": "items",
"items": items,
"ordered": False,
}
# Dropdown/hotspot answers separated by pipes map to dropdown labels in order.
dropdown_groups = question.get("dropdown_groups") or {}
if dropdown_groups:
parts = [p.strip() for p in raw.split("|") if p.strip()]
labels = list(dropdown_groups.keys())
items = []
for i, label in enumerate(labels):
value = parts[i] if i < len(parts) else ""
items.append({"label": label, "value": value})
if items:
return {
"mode": "items",
"items": items,
"ordered": False,
}
# Option questions (single or multi)
options = question.get("options") or []
if options:
text_to_key = {_normalize(o.get("text", "")): o.get("key", "") for o in options}
norm_raw = _normalize(raw)
if norm_raw in text_to_key and text_to_key[norm_raw]:
return {
"mode": "answer",
"value": text_to_key[norm_raw],
}
if re.fullmatch(r"[A-Z]", raw):
return {
"mode": "answer",
"value": raw,
}
if re.fullmatch(r"[A-Z](\s*,\s*[A-Z])+", raw):
letters = [x.strip() for x in raw.split(",") if x.strip()]
return {
"mode": "answer",
"value": "".join(letters),
}
# Generic pipe-separated items fallback.
if "|" in raw:
parts = [p.strip() for p in raw.split("|") if p.strip()]
items = [{"label": f"Item {i + 1}", "value": p} for i, p in enumerate(parts)]
if items:
return {
"mode": "items",
"items": items,
"ordered": False,
}
# Last fallback: keep as answer text.
return {
"mode": "answer",
"value": raw,
}
def parse_docx_questions(docx_path: str) -> List[Dict[str, Any]]:
lines = _paragraphs(docx_path)
part1_idx = _find_marker(lines, "PART 1", exact_start=True)
part2_idx = _find_marker(lines, "PART 2", exact_start=True)
detailed_idx = _find_marker(lines, "DETAILED ANSWERS", exact_start=True)
if part1_idx < 0 or part2_idx < 0 or detailed_idx < 0:
raise ValueError("Could not find PART 1/PART 2/DETAILED ANSWERS markers in DOCX.")
q_lines = lines[part1_idx + 1 : part2_idx]
quick_key_lines = lines[part2_idx + 1 : detailed_idx]
detailed_lines = lines[detailed_idx + 1 :]
question_blocks = _split_q_blocks(q_lines)
detailed_blocks = _split_q_blocks(detailed_lines)
quick_key = _parse_quick_answer_key(quick_key_lines)
parsed_questions: List[Dict[str, Any]] = []
all_qcodes = sorted(set(question_blocks.keys()) | set(detailed_blocks.keys()))
for qcode in all_qcodes:
source_block = question_blocks.get(qcode) or detailed_blocks.get(qcode) or []
if not source_block:
continue
q = _parse_question_block(source_block)
if qcode in detailed_blocks:
d = _parse_detailed_answer_block(detailed_blocks[qcode])
q["qtype"] = d.get("qtype", "UNKNOWN")
q["correct_answer"] = d.get("correct_answer", {})
q["explanation"] = d.get("explanation", "")
if (not q.get("correct_answer")) and qcode in quick_key:
q["correct_answer"] = _infer_correct_answer_from_quick_key(q, quick_key[qcode])
# Reconcile YES/NO statement list against the quick answer key.
# Questions whose statements live inside an exhibit image are hardcoded here in full.
# When a qcode is present, its full statement list replaces whatever was parsed.
_EXHIBIT_STATEMENTS: Dict[str, List[str]] = {
"Q015": [
"The design supports the technical requirements for redundancy (redundant if an Azure region fails).",
"The design supports autoscaling.",
"The design requires a manual configuration if an Azure region fails.",
],
}
if q.get("qtype") == "YES/NO" and qcode in quick_key:
yn_vals = re.findall(r"\b(Yes|No)\b", quick_key[qcode], flags=re.IGNORECASE)
stmts = _EXHIBIT_STATEMENTS[qcode] if qcode in _EXHIBIT_STATEMENTS else q.get("statements", [])
if len(stmts) != len(yn_vals):
# Trim to however many values we have answer keys for
stmts = stmts[:len(yn_vals)]
q["statements"] = stmts
items = [
{"label": s, "value": yn_vals[i].title()}
for i, s in enumerate(stmts)
]
q["correct_answer"] = {"mode": "items", "items": items, "ordered": False}
parsed_questions.append(q)
return parsed_questions
def evaluate_answer(question: Dict[str, Any], user_answer: Dict[str, Any]) -> Dict[str, Any]:
correct = question.get("correct_answer", {}) or {}
mode = correct.get("mode")
if not correct or not mode:
return {
"is_correct": False,
"feedback": "No answer key is available for this question yet.",
}
if not user_answer:
return {
"is_correct": False,
"feedback": "No answer provided.",
}
# === MODE: 'answer' (MC, MULTI simple string answers) ===
if mode == "answer":
expected = (correct.get("value") or "").strip().upper()
selected = (user_answer.get("selected_option") or "").strip().upper()
selected_multi = [x.upper() for x in (user_answer.get("selected_options") or [])]
text_answer = (user_answer.get("text_answer") or "").strip().upper()
# Single letter or multi-letter sequence (A, AB, BD, etc.)
if expected and expected[0].isalpha():
if len(expected) == 1:
# Single choice (MC)
actual = selected or text_answer
ok = actual == expected
return {
"is_correct": ok,
"feedback": "" if ok else f"Expected: {expected}",
}
else:
# Multi-choice (AB, BD, etc.) - order doesn't matter, just matching set
expected_set = set(expected)
actual_set = set(selected_multi) if selected_multi else set(text_answer)
ok = actual_set == expected_set
return {
"is_correct": ok,
"feedback": ""
if ok
else f"Expected: {expected} (any order). Got: {''.join(sorted(actual_set))}",
}
else:
# Text answer (unlikely for MC)
ok = _normalize(text_answer) == _normalize(expected)
return {
"is_correct": ok,
"feedback": "" if ok else f"Expected: {expected}",
}
# === MODE: 'items' (HOTSPOT, DRAGDROP, YESNO) ===
if mode == "items":
expected_items = correct.get("items") or []
user_items = user_answer.get("item_answers") or {}
ordered = correct.get("ordered", False)
checks = []
all_ok = True
for item in expected_items:
label = item.get("label", "")
expected_value = (item.get("value", "") or "").strip()
actual_value = (user_items.get(label, "") or "").strip()
# Normalize for comparison
exp_norm = _normalize(expected_value)
act_norm = _normalize(actual_value)
ok = exp_norm == act_norm or actual_value.upper() == expected_value.upper()
all_ok = all_ok and ok
checks.append(
{
"label": label,
"ok": ok,
"expected": expected_value,
"actual": actual_value,
}
)
feedback_lines = []
for check in checks:
status = "✓" if check["ok"] else "✗"
feedback_lines.append(
f"{status} {check['label']}: {check['actual'] or '(empty)'}"
)
if not check["ok"]:
feedback_lines.append(f" Expected: {check['expected']}")
return {
"is_correct": all_ok,
"feedback": "\n".join(feedback_lines) if feedback_lines else "No inputs provided.",
}
# === FALLBACK ===
text_answer = (user_answer.get("text_answer") or "").strip()
expected_text = str(correct)
ok = _normalize(text_answer) == _normalize(expected_text)
return {
"is_correct": ok,
"feedback": "" if ok else f"Expected: {expected_text}",
}