Spaces:
Running
Running
Download scripts/13_ebsi_solution.py from lilyjeongwon/math-tutor: direct link, hf CLI and curl.
- Browser
- Download file 46.9 kB
-
https://huggingface.co/spaces/lilyjeongwon/math-tutor/resolve/main/scripts/13_ebsi_solution.py
- Command line
-
hf download hf://spaces/lilyjeongwon/math-tutor/scripts/13_ebsi_solution.py
-
curl -L -o 13_ebsi_solution.py https://huggingface.co/spaces/lilyjeongwon/math-tutor/resolve/main/scripts/13_ebsi_solution.py
46.9 kB
| r"""EBSi ์ ๋ต ๋ฐ ํด์ค PDF -> ๋ฌธํญ๋ณ ์ ๋ต + ํด์ค(LaTeX) (3๋จ๊ณ, ๋ฌด๋ฃ) | |
| * ์ ๋ตํ(1ํ์ด์ง 5์นธ x 6์ค ๊ฒฉ์)๋ฅผ ์ฝ์ด 1~21๋ฒ ๋ณด๊ธฐ ๋ฒํธ, 22~30๋ฒ ๋จ๋ต ์ซ์๋ฅผ ์ป๋๋ค. | |
| ๋จ๋ต ์ซ์๋ ์์ ๊ธ๊ผด ์ฝ๋(๋ณํํ๋ก ์ซ์ํ) ๋๋ ์ผ๋ฐ ์ซ์. ์ ๋ต์ด ๋ฒํธ๋ณด๋ค ์กฐ๊ธ ์๋์ธ ์ํ, | |
| ๋ฒํธ๋ง ๊ธฐ์ธ์์ฒด์ธ ์ํ, ์ ๋จ ํด์ค์ด ๊ฐ์ ์ค์ ์๋ ์ํ๋ ์ฒ๋ฆฌํ๋ค. (28ํ ๋ชจ๋ 30/30 ํ์ธ) | |
| 2021_11 ์ โ ~โค ๋ pdfplumber ๊ฐ ๋ชป ์ฝ์ด์ pypdfium2 ๋ก ๋ณด์ถฉํ๋ค (answer_source="table_pdfium"). | |
| * ํด์ค์ "N. [์ถ์ ์๋]" ๋ก ์์ํ๋ ๋ฌธํญ ๋จ์๋ก ๋๋๋ค. (3๋จ ํธ์ง: ํ์ด์ง -> ๋จ -> ์์์ ์๋) | |
| ๊ฐ ๋ฌธํญ ํด์ค์ 12_ebsi_latex.py ์ ๊ฐ์ ๋ฐฉ์์ผ๋ก LaTeX ๋ก ๋ณต์ํ๋ค. | |
| * ๊ฐ๊ด์ ๋์กฐ(์ฐธ๊ณ ์ฉ): ํด์ค์ ๋ณดํต ๋ณด๊ธฐ ๋ฒํธ ์์ด ๊ฐ์ผ๋ก ๋๋๋ฏ๋ก, ํด์ค ๋งจ ๋ โ ~โค ๊ฐ ์์ผ๋ฉด | |
| ๋ง์ง๋ง ์์์ ๋ง์ง๋ง '=' ๋ค ๊ฐ์ 2๋จ๊ณ ๋ณด๊ธฐ(data/ebsi/latex)์ ๋น๊ตํ๋ค. | |
| answer_check = ok / mismatch / no_choice_in_explanation / n/a(๋จ๋ต). ์ฑ์ ๊ธฐ์ค์ ํญ์ ์ ๋ตํ. | |
| ์ฌ์ฉ ์ (PowerShell) | |
| python scripts\13_ebsi_solution.py --dir data\ebsi\h1 --out data\ebsi\solutions | |
| python scripts\13_ebsi_solution.py --pdf data\ebsi\h1\2022_09_hs1_solution.pdf --show 1 22 | |
| """ | |
| from __future__ import annotations | |
| import argparse, importlib.util, json, re, statistics, sys | |
| from collections import Counter | |
| from pathlib import Path | |
| import pdfplumber | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| import ebsi_tables as TBL | |
| HERE = Path(__file__).resolve().parent | |
| CIRC = "โ โกโขโฃโค" | |
| def _load(name, fname): | |
| spec = importlib.util.spec_from_file_location(name, HERE / fname) | |
| m = importlib.util.module_from_spec(spec) | |
| spec.loader.exec_module(m) | |
| return m | |
| LX = _load("ebsi_latex", "12_ebsi_latex.py") | |
| PUA_DIGIT = {k: v["latex"] for k, v in LX.PUA_MAP.items() if v.get("kind") == "digit" and v["latex"].isdigit()} | |
| def isdig(c): | |
| t = c["text"] | |
| return t.isascii() and t.isdigit() | |
| # ------------------------------------------------------------ ์ ๋ตํ | |
| def _cy(c): | |
| return (c["top"] + c["bottom"]) / 2 | |
| def _kind(c): | |
| t = c["text"] | |
| return "circ" if t in CIRC else "asc" if isdig(c) else "pua" if f"{ord(t[0]):04X}" in PUA_DIGIT else None | |
| def _toks_of(cs): | |
| """๊ธ์ -> ํ ํฐ. ๊ฐ์ ์ค(์ค์ฌ ์ฐจ 3pt ์ด๋ด)์์ ๋ฐ๋ก ์ด์ด์ง๋ ๊ฐ์ ์ข ๋ฅ ์ซ์๋ ํ ๋ฉ์ด๋ฆฌ.""" | |
| toks = [] | |
| for c in sorted(cs, key=lambda c: (round(_cy(c)), c["x0"])): | |
| k = _kind(c) | |
| if k is None: | |
| continue | |
| last = toks[-1] if toks else None | |
| prev = last["cs"][-1] if last else None | |
| if (last and k == last["kind"] != "circ" and abs(_cy(c) - last["cy"]) < 3 | |
| # ๋ฒํธ '29' + ์ ๋ต '126' ๋ถ๋ฆฌ: ๊ธ๊ผดยทํฌ๊ธฐยท๊ธฐ์ธ์(๋ฒํธ๋ง ๊ธฐ์ธ์ธ ์ํ ์์)์ด ๊ฐ์์ผ ๋ฌถ์ | |
| and c["fontname"] == prev["fontname"] and abs(c["size"] - prev["size"]) < 0.3 | |
| and abs(c["matrix"][2] - prev["matrix"][2]) < 0.01 | |
| and c["x0"] > last["cs"][-1]["x0"] + 1 and c["x0"] - last["cs"][-1]["x1"] < 3): # ๊ธ์ ์์๋ ๊ฒน์น ์ ์์ | |
| last["cs"].append(c) | |
| else: | |
| toks.append({"kind": k, "cs": [c], "cy": _cy(c)}) | |
| for t in toks: | |
| t["x0"], t["x1"] = t["cs"][0]["x0"], t["cs"][-1]["x1"] | |
| return toks | |
| def _tok_value(t): | |
| if t["kind"] == "circ": | |
| return {"kind": "choice", "value": CIRC.index(t["cs"][0]["text"]) + 1} | |
| if t["kind"] == "pua": | |
| return {"kind": "integer", "value": int("".join(PUA_DIGIT[f"{ord(c['text'][0]):04X}"] for c in t["cs"]))} | |
| return {"kind": "integer", "value": int("".join(c["text"] for c in t["cs"]))} | |
| # 2021_11 ์ ๋ตํ์ โ ~โค๋ pdfminer(pdfplumber)๊ฐ ๊ธ์๋ฅผ ์์ ๋ฒ๋ฆฌ๊ณ , pdfium ์ ํ์๋ก ์๋ชป ์ฝ๋๋ค. | |
| # (ํ๋ฉด ๋ ๋๋ง๊ณผ ๋์กฐํด ํ์ธํ ๋์. ์ฝ๋ ์์๋ โ ->โค ์) | |
| PDFIUM_CIRC_FIX = {"ๆฆ": "โ ", "ๆถ": "โก", "ๆนง": "โข", "ๆบถ": "โฃ", "็": "โค"} | |
| def pdfium_extra_chars(path: Path): | |
| """pdfplumber ๊ฐ ๋ชป ์ฝ์ ์ ๋ตํ ๋ณด๊ธฐ ๋ฒํธ๋ฅผ pypdfium2(pdfplumber ์ค์น ์ ํจ๊ป ๊น๋ฆผ)๋ก ๋ณด์ถฉ.""" | |
| import pypdfium2 as pdfium | |
| doc = pdfium.PdfDocument(str(path)) | |
| try: | |
| pg = doc[0] | |
| H = pg.get_height() | |
| tp = pg.get_textpage() | |
| out = [] | |
| for i in range(tp.count_chars()): | |
| t = tp.get_text_range(i, 1) | |
| if t in CIRC or t in PDFIUM_CIRC_FIX: | |
| l, b, r, top = tp.get_charbox(i) | |
| out.append({"text": PDFIUM_CIRC_FIX.get(t, t), "x0": l, "x1": r, "top": H - top, "bottom": H - b, | |
| "fontname": "pdfium", "size": 0, "matrix": (0, 0, 0, 0, 0, 0), "pdfium": True}) | |
| return out | |
| finally: | |
| doc.close() | |
| def parse_answers(pdf: pdfplumber.PDF, extra_chars=None): | |
| """1ํ์ด์ง ์ ๋ตํ(5์นธ x 6์ค, ์นธ๋ง๋ค [๋ฒํธ][์ ๋ต])๋ฅผ ๊ธ์ ์์น๋ก ์ฝ๋๋ค. | |
| 1) '1 2 3 4 5' ๊ฐ ์ฐจ๋ก๋ก ๋์ค๋ ์ค์ ์ฐพ์ ์นธ์ x ์์น(๋ฒํธ ์ค๋ฅธ์ชฝ ๋)์ ๊ฐ๊ฒฉ์ ์ป๋๋ค. | |
| 2) ํ์ x ๋ฒ์ ๋ฐ ๊ธ์(์ ๋จ ํด์ค)๋ ๋ฒ๋ฆฐ๋ค. | |
| 3) ๋ฒํธ ํ ํฐ = ์นธ ์์น์ ์ค๋ฅธ์ชฝ ๋์ด ๋ง๋ ASCII ์ซ์. ๋ฒํธ๋ 1,2,...,30 ์์๋ก๋ง ๋ฐ์๋ค์ธ๋ค. | |
| 4) ์ ๋ต = ๊ฐ์ ์นธ(๋ฒํธ ์ค๋ฅธ์ชฝ ~ ๋ค์ ์นธ ์์) ์์์ ์ธ๋ก ์ค์ฌ์ด ๊ฐ๊น์ด(9pt) ์ฒซ ํ ํฐ. | |
| ์ ๋ต ์ค์ด ๋ฒํธ ์ค๋ณด๋ค ์กฐ๊ธ ์๋์ ์๋ ์ํ(2021_11 ๋ฑ)๋ ์ค์ฌ ๊ธฐ์ค์ด๋ผ ์กํ๋ค.""" | |
| p = pdf.pages[0] | |
| allc = [c for c in p.chars if c["text"].strip() and _kind(c)] | |
| if extra_chars: # pdfplumber ๊ธ์์ ๊ฒน์น์ง ์๋ ๊ฒ๋ง | |
| allc += [e for e in extra_chars | |
| if not any(abs(c["x0"] - e["x0"]) < 2 and abs(_cy(c) - _cy(e)) < 2 for c in allc)] | |
| toks = _toks_of(allc) | |
| # 1) ๊ธฐ์ค ์ค ์ฐพ๊ธฐ | |
| head = None | |
| for t in toks: | |
| if t["kind"] != "asc" or "".join(c["text"] for c in t["cs"]) != "1": | |
| continue | |
| row = sorted([u for u in toks if u["kind"] == "asc" and abs(u["cy"] - t["cy"]) < 3 and u["x0"] >= t["x0"]], | |
| key=lambda u: u["x0"]) | |
| nums = ["".join(c["text"] for c in u["cs"]) for u in row] | |
| if nums[:5] == ["1", "2", "3", "4", "5"]: | |
| head = row[:5] | |
| break | |
| if head is None: | |
| return {}, "์ ๋ตํ ์์น๋ฅผ ๋ชป ์ฐพ์" | |
| M = [(u["x0"] + u["x1"]) / 2 for u in head] # ๋ฒํธ ์นธ ๊ฐ์ด๋ฐ (๋ฒํธ๋ ๊ฐ์ด๋ฐ ์ ๋ ฌ) | |
| R = [u["x1"] for u in head] | |
| pitch = (M[4] - M[0]) / 4 | |
| y0 = head[0]["cy"] - 8 | |
| x_lo, x_hi = head[0]["x0"] - pitch * 0.4, R[4] + pitch * 0.8 | |
| box = [t for t in toks if x_lo <= t["x0"] <= x_hi and t["cy"] >= y0] | |
| def col(t): # ๋ฒํธ ์นธ์ด๋ฉด ์นธ ๋ฒํธ, ์๋๋ฉด None | |
| if t["kind"] != "asc": | |
| return None | |
| mid = (t["x0"] + t["x1"]) / 2 | |
| for j, m in enumerate(M): | |
| if abs(mid - m) < 3.5: | |
| return j | |
| return None | |
| # 3) ๋ฒํธ ํ ํฐ์ ์์๋๋ก | |
| numbers, want = {}, 1 | |
| for t in sorted(box, key=lambda t: (round(t["cy"] / 4), t["x0"])): | |
| j = col(t) | |
| if j is None or want > 30: | |
| continue | |
| v = int("".join(c["text"] for c in t["cs"])) | |
| if v == want and j == (want - 1) % 5: | |
| numbers[v] = t | |
| want += 1 | |
| # 4) ์ ๋ต ์ง์ง๊ธฐ | |
| used = {id(t) for t in numbers.values()} | |
| ans = {} | |
| for n, nt in numbers.items(): | |
| j = (n - 1) % 5 | |
| right = R[j] + pitch - (R[0] - head[0]["x0"]) - 2 # ๋ค์ ์นธ ๋ฒํธ ์์ ์ง์ | |
| cand = [t for t in box if id(t) not in used and nt["x1"] - 1 <= t["x0"] < right | |
| and abs(t["cy"] - nt["cy"]) < 9 and col(t) is None] | |
| if cand: | |
| a = min(cand, key=lambda t: (abs(t["cy"] - nt["cy"]) > 3, t["x0"])) | |
| ans[n] = _tok_value(a) | |
| ans[n]["source"] = "table_pdfium" if a["cs"][0].get("pdfium") else "table" | |
| used.add(id(a)) | |
| err = None if len(numbers) == 30 else f"์ ๋ตํ ๋ฒํธ {len(numbers)}/30๊ฐ๋ง ์ฐพ์" | |
| return ans, err | |
| # ------------------------------------------------------------ ๊ณ 3 ์ ํ๊ณผ๋ชฉ ๊ตฌ๊ฐ (๊ต์ก์ฒญ ํ์) | |
| # ๊ณ 3 ํด์ค์ ๊ณตํต(1~22) ๋ค์ [ํ๋ฅ ๊ณผ ํต๊ณ] [๋ฏธ์ ๋ถ] [๊ธฐํ] ๊ตฌ๊ฐ์ด ์ฐจ๋ก๋ก ์๊ณ , ๊ตฌ๊ฐ๋ง๋ค 23~30๋ฒ ์ ๋ตํ๊ฐ ๋ฐ๋ก ์๋ค. | |
| # ์ด ์ฑ์ ํ๋ฅ ๊ณผ ํต๊ณ๋ง ์ด๋ค: ํํต ๊ตฌ๊ฐ ์ ๋ตํ๋ก 23~30 ์ ๋ต์ ์ฝ๊ณ , ๋ฏธ์ ๋ถยท๊ธฐํ ๊ตฌ๊ฐ๋ถํฐ๋ ํด์ค์์ ๋บ๋ค. | |
| SUBJ = (("ํํต", ("[ํ๋ฅ ๊ณผ", "ํ๋ฅ ๊ณผ")), ("๋ฏธ์ ๋ถ", ("[๋ฏธ์ ๋ถ",)), ("๊ธฐํ", ("[๊ธฐํ",))) | |
| PUA_DIG_CH = {chr(int(k, 16)): v for k, v in PUA_DIGIT.items()} # ์์ ๊ธ๊ผด ์ซ์ ๊ธ์ -> '0'~'9' | |
| PUA_DIG_CH |= {chr(0x24F5 + i): CIRC[i] for i in range(5)} # โต~โน(์ด์ค ์ ์ซ์, 2024_06 ๊ณ 3 ์ ๋ตํ) -> โ ~โค | |
| def section_headers(pdf): | |
| """์ ํ๊ณผ๋ชฉ ๊ตฌ๊ฐ ๋จธ๋ฆฌ ์์น. '[ํ๋ฅ ๊ณผ ํต๊ณ]'(3ยท4ยท5ยท10์) ๋๋ 'ํ๋ฅ ๊ณผ ํต๊ณ ์ ๋ต'(7์). | |
| ๋ฏธ์ ๋ถยท๊ธฐํ๋ '[๋ฏธ์ ๋ถ]' '[๊ธฐํ]' ๋๋ '๋ฏธ์ ๋ถ ์ ๋ต' '๊ธฐํ ์ ๋ต'.""" | |
| out = [] | |
| for pi, p in enumerate(pdf.pages): | |
| ws = p.extract_words() | |
| for j, w in enumerate(ws): | |
| t = w["text"] | |
| nxt = " ".join(x["text"] for x in ws[j + 1:j + 3]) | |
| name = None | |
| if t.startswith("[์ ํ"): # ํ๊ฐ์ ํ์ '[์ ํ: ํ๋ฅ ๊ณผ ํต๊ณ]' '[์ ํ: ๋ฏธ์ ๋ถ]' '[์ ํ: ๊ธฐํ]' | |
| t = "[" + nxt.split()[0] if nxt else t | |
| if t.startswith("[ํ๋ฅ ๊ณผ") or (t == "ํ๋ฅ ๊ณผ" and nxt.startswith("ํต๊ณ")): | |
| name = "ํํต" | |
| elif t.startswith("[๋ฏธ์ ๋ถ") or (t == "๋ฏธ์ ๋ถ" and nxt.startswith("์ ๋ต")): | |
| name = "๋ฏธ์ ๋ถ" | |
| elif t.startswith("[๊ธฐํ") or (t == "๊ธฐํ" and nxt.startswith("์ ๋ต")): | |
| name = "๊ธฐํ" | |
| if name and not any(h["name"] == name for h in out): | |
| out.append({"name": name, "page": pi, "x": w["x0"], "x1": w["x1"], "top": w["top"]}) | |
| return out | |
| def select_of(pdf, exam: str): | |
| """๊ณ 3 ํด์ค์ ์ ํ๊ณผ๋ชฉ ๊ตฌ๊ฐ ์ ๋ณด: {"skip": ํํต ๋จธ๋ฆฌ, "cut": ๋ค์ ์ ํ๊ณผ๋ชฉ ๋จธ๋ฆฌ} (์์ผ๋ฉด None). 13ยท14 ๊ฐ ๊ฐ์ด ์.""" | |
| if not exam.endswith("_hs3"): | |
| return None | |
| hd = {h["name"]: h for h in section_headers(pdf)} | |
| if "ํํต" not in hd: | |
| return None | |
| t = hd["ํํต"] | |
| later = [h for k, h in hd.items() if k != "ํํต" and (h["page"], h["x"], h["top"]) > (t["page"], t["x"], t["top"])] | |
| return {"skip": t, "cut": min(later, key=lambda h: (h["page"], h["x"], h["top"])) if later else None} | |
| def parse_select_answers(pdf, hdr): | |
| """ํํต ๋จธ๋ฆฌ ์๋ ์ ๋ตํ '23 โข 24 โฃ โฆ 29 55 30 97' (์ซ์๊ฐ ์์ ๊ธ๊ผด ์ ์ฉ ์ฝ๋์ผ ์ ์์, '29288' ์ฒ๋ผ ๋ถ๊ธฐ๋ ํจ).""" | |
| p = pdf.pages[hdr["page"]] | |
| cx = (hdr["x"] + hdr["x1"]) / 2 | |
| ws = [w for w in p.extract_words() if hdr["top"] + 3 < w["top"] < hdr["top"] + 60 and abs((w["x0"] + w["x1"]) / 2 - cx) < 190] | |
| return pair_answers(ws, r"(2[3-9]|30)\.?", range(23, 31), "table_select") | |
| def pair_answers(ws, num_pat, wanted, source, max_gap=60): | |
| """๋ฒํธ ๋จ์ด(num_pat) ์ ๊ฐ์ ์ค ์ค๋ฅธ์ชฝ ๊ฐ์ฅ ๊ฐ๊น์ด ๊ฐ(โ ~โค ๋๋ ์ซ์)์ ์ง์ง๋๋ค. | |
| '29288' '01.โก' ์ฒ๋ผ ๋ฒํธ์ ๊ฐ์ด ํ ๋จ์ด์ธ ๊ฒ๋ ๋ฐ๋๋ค. ๊ฐ์ด ์๋ ๋ฒํธ ๋จ์ด(๋ฌธํญ ์์ '28. [์ถ์ ์๋]')๋ ๋ฌด์. | |
| ๊ฐ์ ๋ฒํธ๊ฐ ์ฌ๋ฟ์ด๋ฉด ๊ฐ์ด ์๋ ๊ฒ ์ค ์์ชฝ ์ฒซ ๊ฒ.""" | |
| # ์ค(์ธ๋ก 4pt ์) ๋จ์๋ก ์ผ์ชฝ๋ถํฐ ์ฝ์ผ๋ฉฐ '๋ฒํธ โ ๊ฐ' ์ ์ฐจ๋ก๋ก ๋ฐ๋๋ค. ๋ฒํธ๋ wanted ์์๋๋ก๋ง. | |
| # (๊ฐ '25' ๊ฐ ๋ฒํธ์ฒ๋ผ ๋ณด์ฌ๋ ์์์ ๊ฐ ์๋ฆฌ์ด๋ฉด ๊ฐ: 2021_07 '29 25 30 51') | |
| toks = [] | |
| for w in ws: | |
| w["t"] = "".join(PUA_DIG_CH.get(ch, ch) for ch in w["text"]) | |
| if re.fullmatch(r"[โ โกโขโฃโค]|\d+\.?(?:[โ โกโขโฃโค]|\d+)?", w["t"]): | |
| toks.append(w) | |
| toks.sort(key=lambda w: w["top"]) | |
| lines = [] | |
| for w in toks: | |
| if lines and abs(w["top"] - lines[-1][0]["top"]) < 4: | |
| lines[-1].append(w) | |
| else: | |
| lines.append([w]) | |
| order = list(wanted) | |
| ans, k, pend = {}, 0, None | |
| for line in lines: | |
| pend = None | |
| for w in sorted(line, key=lambda w: w["x0"]): | |
| if pend is not None: # ๊ฐ ์๋ฆฌ | |
| pn, px = pend | |
| pend = None | |
| if re.fullmatch(r"[โ โกโขโฃโค]|\d+", w["t"]) and w["x0"] < px + max_gap: | |
| v = w["t"] | |
| ans[pn] = ({"kind": "choice", "value": CIRC.index(v) + 1} if v in CIRC | |
| else {"kind": "integer", "value": int(v)}) | {"source": source} | |
| k += 1 | |
| continue | |
| k += 1 # ๊ฐ์ด ๊ธ์๋ก ์ ๋์จ ์นธ(โ ~โค ๋๋ฝ): ๊ฑด๋๋ฐ๊ณ ์ด ๋จ์ด๋ฅผ ๋ฒํธ๋ก ๋ค์ ๋ด | |
| if k >= len(order): | |
| break | |
| m = re.fullmatch(num_pat + r"([โ โกโขโฃโค]|\d*)", w["t"]) | |
| if not m or int(m.group(1)) != order[k]: | |
| continue | |
| if m.group(2): # '29288' '01.โก' | |
| v = m.group(2) | |
| ans[order[k]] = ({"kind": "choice", "value": CIRC.index(v) + 1} if v in CIRC | |
| else {"kind": "integer", "value": int(v)}) | {"source": source} | |
| k += 1 | |
| else: | |
| pend = (order[k], w["x1"]) | |
| return ans | |
| def pdfium_list_answers(path: Path, wanted): | |
| """pdfplumber ๊ฐ โ ~โค๋ฅผ ๋ชป ์ฝ๋ ํด์ค(2024_06 ๊ณ 3 ๋ฑ): pdfium ๊ธ์๋ก 'NN. โฃ' ๋ชฉ๋ก์ ๋ค์ ์ฝ๋๋ค. | |
| ๋ฒํธ๊ฐ ์ฐจ๋ก๋ก(1์ฉ ์ฆ๊ฐ) ์ด์ด์ง๋ ๊ตฌ๊ฐ๋ง ๋ฐ๋๋ค(๋ณธ๋ฌธ ์ '23. ์ถ์ ์๋' ๋ฑ ์ค์ธ ๋ฐฉ์ง).""" | |
| import pypdfium2 as pdfium | |
| doc = pdfium.PdfDocument(str(path)) | |
| lo = min(wanted) | |
| for i in range(len(doc)): | |
| t = "".join(PUA_DIG_CH.get(ch, ch) for ch in doc[i].get_textpage().get_text_range()) | |
| found = {} | |
| for m in re.finditer(r"(?<!\d)(\d{2})\.\s*([โ โกโขโฃโค]|\d+)", t): | |
| n = int(m.group(1)) | |
| if n in wanted and n not in found and (n == lo or n - 1 in found): | |
| v = m.group(2) | |
| found[n] = ({"kind": "choice", "value": CIRC.index(v) + 1} if v in CIRC else {"kind": "integer", "value": int(v)}) \ | |
| | {"source": "table_pdfium"} | |
| if len(found) >= len(wanted) - 1: | |
| return found | |
| return {} | |
| def parse_list_answers(pdf): | |
| """ํ๊ฐ์ ํ์ 1์ชฝ ์ ๋ต ๋ชฉ๋ก '01. โฃ 02. โค โฆ 16. 2 โฆ 22. 61' (01~22). ๋ฒํธ ๋จ์ด ์ค๋ฅธ์ชฝ ๊ฐ์ ์ค์ ๊ฐ์ฅ ๊ฐ๊น์ด ๊ฐ. | |
| '01.โก' ์ฒ๋ผ ๋ถ์ ๋จ์ด๋ ๋ฐ๋๋ค. ๋ฌธํญ ์์ '16. ์ถ์ ์๋' ๋ ๊ฐ์ด ์๋์ด์ ๊ฑธ๋ฆฌ์ง ์์.""" | |
| return pair_answers(pdf.pages[0].extract_words(), r"(\d{2})\.", range(1, 23), "table_list", max_gap=30) | |
| # ------------------------------------------------------------ ํด์ค ๋๋๊ธฐ | |
| START = re.compile(r"(\d{1,2})\.(?:\[์ถ์ |์ถ์ ์๋)") # ํ๊ฐ์ ๊ณ 3 ํด์ค์ 'N. ์ถ์ ์๋ :' | |
| def _lines(chars, tol=1.5): | |
| """๊ณต๋ฐฑ ์๋ ๊ธ์๋ฅผ ๊ฐ์ top(ยฑtol)๋ผ๋ฆฌ ์ค๋ก ๋ฌถ๋๋ค. ์ค ์์ x ์์.""" | |
| cs = sorted((c for c in chars if c["text"].strip()), key=lambda c: (c["top"], c["x0"])) | |
| lines = [] | |
| for c in cs: | |
| if lines and abs(c["top"] - lines[-1][0]["top"]) <= tol: | |
| lines[-1].append(c) | |
| else: | |
| lines.append([c]) | |
| return [sorted(l, key=lambda c: c["x0"]) for l in lines] | |
| WORD_NUM = re.compile(r"^\d{1,2}\.$") | |
| def find_starts(pdf): | |
| """'N. [์ถ์ ์๋]' ํ์ง ์ฐพ๊ธฐ. ๋ ๋ฐฉ์์ ํฉ์น๋ค. | |
| 1) ๋จ์ด ๋ฐฉ์: '7.' ๋ค์ ๋จ์ด๊ฐ '[์ถ์ โฆ' (๊ธ๊ผด์ด ๋ฌ๋ผ top ์ด 1.5pt ๋๊ฒ ์ด๊ธ๋๋ ๋จ) | |
| 2) ๊ธ์ ๋ฐฉ์: ๊ฐ์ top ์ ๊ธ์๋ง ์ด์ด 'N.[์ถ์ '. extract_words ๊ฐ ๋จธ๋ฆฌ์ค๊ณผ ๋ฐ๋ก ์๋ซ์ค์ | |
| ์์ด ๋ฒ๋ฆฌ๋ ๊ฒฝ์ฐ(2024_10 5๋ฒ: '5. ์ฃผ[์ถ์ด์ ์ง์...')๋ฅผ ์ก๋๋ค. | |
| ๋จ์ด ๋ฐฉ์ ๊ฒฐ๊ณผ๋ฅผ ์ฐ์ ํ๊ณ , ๊ฑฐ๊ธฐ์ ๋น ์ง ๋ฒํธ๋ง ๊ธ์ ๋ฐฉ์์ผ๋ก ๋ณด์ถฉ.""" | |
| found = {} | |
| for pi, p in enumerate(pdf.pages): | |
| ws = p.extract_words() | |
| for j, w in enumerate(ws[:-1]): | |
| if WORD_NUM.match(w["text"]) and ws[j + 1]["text"].startswith(("[์ถ์ ", "์ถ์ ์๋")) and abs(ws[j + 1]["top"] - w["top"]) < 3: | |
| found.setdefault(int(w["text"][:-1]), {"page": pi, "x": w["x0"], "top": w["top"], "n": int(w["text"][:-1])}) | |
| for s in _find_starts_chars(pdf): | |
| found.setdefault(s["n"], s) | |
| return list(found.values()) | |
| def _find_starts_chars(pdf): | |
| out = [] | |
| for pi, p in enumerate(pdf.pages): | |
| for line in _lines(p.chars): | |
| s = "".join(c["text"] for c in line) | |
| for m in START.finditer(s): | |
| c0 = line[m.start(1)] | |
| if m.start(1) > 0 and line[m.start(1) - 1]["text"].isdigit(): | |
| continue # '123.[์ถ์ ' ๊ฐ์ ์ค์ธ ๋ฐฉ์ง | |
| out.append({"page": pi, "x": c0["x0"], "top": c0["top"], "n": int(m.group(1))}) | |
| return out | |
| def header_bottom(p, default=None): | |
| """ํ์ด์ง ๋จธ๋ฆฌ๋ง์ ์๋ ๋. ๋จธ๋ฆฌ๋ง = '์ ๋ต ๋ฐ ํด์ค' ์ค + ๊ทธ ์๋ 25pt ์์ 'โฆํ๋ ฅํ๊ฐ' ์ค. | |
| (1ํ์ด์ง ๋ณธ๋ฌธ ์๋ด๋ฌธ 'โป ๋ณธ ์ ๊ตญ์ฐํฉํ๋ ฅํ๊ฐ๋โฆ' ์ ๋จธ๋ฆฌ๋ง์ด ์๋) ๋ชป ์ฐพ์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ. | |
| ๊ธฐ๋ณธ๊ฐ: A3(๊ฐ๋ก 841) 152, A4(๊ฐ๋ก 595) 115 โ A4 ๋ณธ๋ฌธ์ 120 ๋ถํฐ๋ผ 152 ๋ก ์๋ฅด๋ฉด ๋จ๋ง๋ค ์ฒซ 2~3์ค์ด | |
| ์ฌ๋ผ์ก์ (2020_11 ํด์ค 18๋ฒ '์ P์ ์ขํ๋ (2/3,4)โฆ', 20๋ฒ ๋จธ๋ฆฌ ์ค; 2021_03ยท2022_11ยท2024_10 ๋ ๊ฐ์).""" | |
| if default is None: | |
| default = 152 if p.width > 700 else 115 | |
| lines = [(l, "".join(c["text"] for c in l)) for l in _lines([c for c in p.chars if c["top"] < 170], tol=3)] | |
| title = [l for l, s in lines if "์ ๋ต๋ฐํด์ค" in s] | |
| if not title: | |
| # ์ ๋ชฉ ์ค์ด ์๋ ์ชฝ: ๋จธ๋ฆฌ๋ง ์๋ ๊ฐ๋ก ๊ตฌ๋ถ์ (์ชฝ ๋๋น 60% ์ด์, 130pt ์)์ด ์์ผ๋ฉด ๊ทธ ๋ฐ๋ก ์๋๋ถํฐ ๋ณธ๋ฌธ. | |
| # (2021_06_hs3 ํด์ค: 2์ชฝ๋ถํฐ ์ ์ด 85pt, ๋ณธ๋ฌธ ์ฒซ ์ค์ด 99pt ๋ผ 115 ๋ก ์๋ฅด๋ฉด ๋จ๋ง๋ค ์ฒซ ์ค์ด ์ฌ๋ผ์ก์ | |
| # โ 5๋ฒ g'(1)=โฆ, 11ยท13ยท15๋ฒ ๋จธ๋ฆฌ ์ค) | |
| rules = [l["top"] for l in p.lines if l["top"] < 130 and (l["x1"] - l["x0"]) > p.width * 0.6] | |
| if rules: | |
| return min(default, max(rules) + 2) | |
| # ์ ๋ชฉ ์ค๋ ๊ตฌ๋ถ์ ๋ ์๋ A4 ์ชฝ: ๋ณธ๋ฌธ ์ฒซ ์ค์ด 99pt ๋ถ๊ทผ์ด๋ผ 115 ๋ก ์๋ฅด๋ฉด ๋จ๋ง๋ค ์ฒซ ์ค์ด ์ฌ๋ผ์ก์ | |
| # (2025_05_hs3 15ยท21ยท27ยท30๋ฒ ๋จธ๋ฆฌ ์ค, 2025_06ยท2025_09 3๋ฒ ๋จธ๋ฆฌ, 2022_09ยท2024_09 ์ฒซ ์ค ๋ฑ). | |
| # 85 ์์๋ ์ํ ์ ๋ชฉ๋ง ์๊ณ , 85~115 ์ '์ต๊ทผ ์์ ์ผ'ยท'โ [๊ณตํตโฆ]' ๊ฐ์ ๋จธ๋ฆฌ ๊ธ์ ์ฒซ ๋ฌธํญ๋ณด๋ค ์์ด๋ผ ๋ฒ๋ ค์ง. | |
| if p.width <= 700: | |
| return 85 | |
| return default | |
| t0 = min(c["top"] for c in title[0]) | |
| bot = max(max(c["bottom"] for c in l) for l, s in lines | |
| if ("์ ๋ต๋ฐํด์ค" in s or "ํ๋ ฅํ๊ฐ" in s) and min(c["top"] for c in l) < t0 + 25) | |
| return bot + 2 | |
| def column_lefts(starts): | |
| xs = sorted(s["x"] for s in starts) | |
| cols = [] | |
| for x in xs: | |
| if cols and abs(x - cols[-1][-1]) < 12: | |
| cols[-1].append(x) | |
| else: | |
| cols.append([x]) | |
| return [min(c) for c in cols] | |
| def col_of(x, lefts): | |
| k = 0 | |
| for i, l in enumerate(lefts): | |
| if x >= l - 6: | |
| k = i | |
| return k | |
| def _in_any(cx, cy, boxes, pad=0.5): | |
| return any(b[0] + pad <= cx <= b[2] - pad and b[1] + pad <= cy <= b[3] - pad for b in boxes) | |
| def split_solutions(pdf, drop_figure_labels: bool = True, select=None): | |
| """drop_figure_labels: ๊ทธ๋ฆผ(์ด๋ฏธ์ง) ์์ ๊ฒน์ณ ์ด ๋ผ๋ฒจ ๊ธ์(๊ผญ์ง์ A, ์ถ xยทy, ์ ์์ ์ ๋ฑ)๋ ๋ณธ๋ฌธ์์ ๋บ๋ค. | |
| (2020_06 ํด์ค 6๋ฒ: ์ด๋ฏธ์ง ๋ ์ฅ ์์ '-2xยฒ, xยณ+1, A, D' ๋ผ๋ฒจ์ด ๊ธ์๋ก ์ฌ๋ผ๊ฐ ๋ณธ๋ฌธ ์์ ์์์) | |
| ํ๋ฉด ๋์กฐ์ฉ ์๋ผ๋ด๊ธฐ(14_ebsi_review.py)๋ ๊ทธ๋ฆผ ๋ฒ์๋ฅผ ๋ณด๋ ค๊ณ False ๋ก ๋ถ๋ฅธ๋ค.""" | |
| starts = find_starts(pdf) | |
| if not starts: | |
| return {}, "ํด์ค ์์ ํ์ง๋ฅผ ๋ชป ์ฐพ์" | |
| lefts = column_lefts(starts) | |
| for s in starts: | |
| s["col"] = col_of(s["x"], lefts) | |
| # ๊ณ 3 ์ ํ๊ณผ๋ชฉ: select = {"skip": ํํต ๋จธ๋ฆฌ(์ ๋ตํ ๊ตฌ์ญ), "cut": ๋ฏธ์ ๋ถ/๊ธฐํ ๋จธ๋ฆฌ(์ฌ๊ธฐ๋ถํฐ ํด์ค์์ ๋บ)} | |
| select = select or {} | |
| cut = select.get("cut") | |
| cut_key = (cut["page"], col_of(cut["x"], lefts), cut["top"] - 2) if cut else None | |
| skip = select.get("skip") | |
| if cut_key: | |
| starts = [s for s in starts if (s["page"], s["col"], s["top"]) < cut_key] | |
| skip_zone = None | |
| if skip: | |
| sc = col_of(skip["x"], lefts) | |
| below = [s["top"] for s in starts if s["page"] == skip["page"] and s["col"] == sc and s["top"] > skip["top"]] | |
| skip_zone = (skip["page"], sc, skip["top"] - 2, min(below) - 2 if below else skip["top"] + 60) | |
| starts.sort(key=lambda s: (s["page"], s["col"], s["top"])) | |
| out: dict[int, dict] = {s["n"]: {"segs": {}} for s in starts} | |
| for pi, p in enumerate(pdf.pages): | |
| body_bot = p.height * 0.91 | |
| # ์ชฝ ๋ฒํธ(์๋์ชฝ ๊ฐ์ด๋ฐ์ ํฐ ์ซ์, 13pt ์ด์)๊ฐ 91% ์ ๋ณด๋ค ์์ ์์ผ๋ฉด ๊ทธ ์๊น์ง๋ง ๋ณธ๋ฌธ. | |
| # (A4 ํ ์ชฝ ๋ฒํธ๋ 753pt ์ ์์ด ๋ณธ๋ฌธ 'โฆ์ ๋ฆฌํ๋ฉด y=โฆ์ด๋ค. 1 ์ด๋ฅผ ใก์โฆ' ์ฒ๋ผ ์์์: 2021_06 ํด์ค 13๋ฒ) | |
| pn = [c for c in p.chars if c["text"].isdigit() and c["top"] > p.height * 0.85 and c["size"] >= 13 | |
| and abs((c["x0"] + c["x1"]) / 2 - p.width / 2) < 40] | |
| if pn: | |
| body_bot = min(body_bot, min(c["top"] for c in pn) - 1) | |
| head = header_bottom(p) | |
| pst = [s for s in starts if s["page"] == pi] | |
| figs = [] | |
| if drop_figure_labels: | |
| for im in p.images: | |
| box = (im["x0"], im["top"], im["x1"], im["bottom"]) | |
| # ์ด๋ฏธ์ง ์์ ํ๊ธ์ด 4์ ์ด์์ด๋ฉด ๋ณธ๋ฌธ ์์ ๊ฑธ์น ์ด๋ฏธ์ง(๋ฐฐ๊ฒฝยท์์)๋ก ๋ณด๊ณ ๊ฑด๋๋ฆฌ์ง ์์. | |
| # (2020_03 12๋ฒ ๋ณธ๋ฌธ, 14๋ฒ ๋ฌธํญ ๋ฒํธ๊ฐ ์ง์์ก๋ ๋ฌธ์ ) ๊ทธ๋ฆผ ๋ผ๋ฒจ์ ๊ฑฐ์ ํ๊ธ์ด ์๋. | |
| hang = sum(1 for c in p.chars if _hangul(c["text"]) | |
| and _in_any((c["x0"] + c["x1"]) / 2, (c["top"] + c["bottom"]) / 2, [box])) | |
| if hang < 4: | |
| # ์์ญ์ ๋ํ์ง ์์: 8pt ๋ํ๋ฉด ๊ทธ๋ฆผ ์ ๋ณธ๋ฌธ์ ์ฒจ์ยท๋ถ์๊น์ง ์ง์์ก์(2024_09 9ยท17ยท21๋ฒ). | |
| # ๊ฒฝ๊ณ์ ๊ฑธ์น ๋ผ๋ฒจ ์กฐ๊ฐ(') )' ๋ฑ)์ ํ๋ฉด ๋์กฐ์์ ์ฒ๋ฆฌ. | |
| figs.append(box) | |
| hrows: dict[int, list[float]] = {} # ๋จ๋ณ ํ๊ธ ๊ธ์ ๋์ด (์ ๋จ ์ค์ ๋ณด์ง ์์) | |
| if figs: | |
| for c in p.chars: | |
| if _hangul(c["text"]): | |
| hrows.setdefault(col_of(c["x0"], lefts), []).append((c["top"] + c["bottom"]) / 2) | |
| for v in hrows.values(): | |
| v.sort() | |
| def hangul_row(cy, col, tol=2): | |
| import bisect | |
| rows = hrows.get(col, []) | |
| i = bisect.bisect_left(rows, cy - tol) | |
| return i < len(rows) and rows[i] <= cy + tol | |
| def in_fig_band(c, cy, col): | |
| """๊ทธ๋ฆผ๊ณผ ๊ฐ์ ๋์ด ๋ฒ์(๊ฐ์ ๋จ)์ ์๋ ๋ผ๋ฒจ: ๊ทธ๋ฆผ ์ 'y=g(x)', ์ 'A' ์ฒ๋ผ ์ด๋ฏธ์ง ์์ ๋ฐ์ ๋ถ์ ๊ฒ. | |
| ๋ณธ๋ฌธ ๋ณดํธ: ํ๊ธ ์ค ยฑ7pt(์ฒจ์ยท๋ถ์ยท๋ถ๋ชจ ๋์ด) ์์ ๊ธ์๋ ์ ์ธํ์ง ์์.""" | |
| cx = (c["x0"] + c["x1"]) / 2 | |
| for b in figs: | |
| # ๊ทธ๋ฆผ์ ๋จ์ ๊ฐ์ด๋ฐ x ๋ก ํ์ (์ผ์ชฝ ๋์ผ๋ก ํ๋ฉด ์ ๋จ ๊ฒฝ๊ณ์ ๊ฑธ์น ๊ทธ๋ฆผ์ด ์ด ๋จ ๊ฒ์ผ๋ก ์กํ | |
| # ๊ทธ ๋์ด์ ์์ ์ค์ด ๋ผ๋ฒจ๋ก ์ง์์ก์: 2022_07_hs3 11๋ฒ 'b-a=4 โฏใ ', 20๋ฒ ๋ ๊ณ์ฐ) | |
| if b[1] <= cy <= b[3] and col_of((b[0] + b[2]) / 2, lefts) == col and not hangul_row(cy, col, 7): | |
| return True | |
| return False | |
| for c in p.chars: | |
| if not c["text"].strip(): | |
| continue | |
| cy = (c["top"] + c["bottom"]) / 2 | |
| if cy > body_bot or cy < head: | |
| continue | |
| col = col_of(c["x0"], lefts) | |
| if cut_key and (pi, col, cy) >= cut_key: | |
| continue # ๋ฏธ์ ๋ถยท๊ธฐํ ๊ตฌ๊ฐ | |
| if skip_zone and pi == skip_zone[0] and col == skip_zone[1] and skip_zone[2] <= cy < skip_zone[3]: | |
| continue # ํํต ๋จธ๋ฆฌยท์ ๋ตํ | |
| if figs and ((_in_any((c["x0"] + c["x1"]) / 2, cy, figs) and not hangul_row(cy, col)) | |
| or in_fig_band(c, cy, col)): | |
| continue # ๊ทธ๋ฆผ ๋ผ๋ฒจ (๊ฐ์ ๋จยท๊ฐ์ ๋์ด์ ํ๊ธ์ด ์์ผ๋ฉด ๋ณธ๋ฌธ ์ค์ด๋ผ ๋จ๊น: 2023_06 27๋ฒ 'โ nโค5์ธ ๊ฒฝ์ฐ') | |
| same = [s for s in pst if s["col"] == col and s["top"] <= cy + 6] | |
| if same: | |
| owner = same[-1] | |
| else: | |
| prev = [s for s in starts if (s["page"], s["col"]) < (pi, col)] | |
| if not prev: | |
| continue # ์ฒซ ๋ฌธํญ๋ณด๋ค ์(์ ๋ตํ ๋ฑ) | |
| owner = prev[-1] | |
| # ๋ฌธํญ ๋ฒํธ ๊ธ์ ์์ฒด๋ ์ ์ธ | |
| out[owner["n"]]["segs"].setdefault((pi, col), []).append(c) | |
| return out, None | |
| def _hangul(ch: str) -> bool: | |
| return bool(ch) and "๊ฐ" <= ch <= "ํฃ" | |
| def render_solution(segs, ctx): | |
| """๋จ/ํ์ด์ง ์กฐ๊ฐ์ ์ด์ด ๋ถ์ธ๋ค. ์กฐ๊ฐ ๊ฒฝ๊ณ(๋จ ๋ = ๊ฝ ์ฐฌ ์ค)๋ ํ๊ธ-ํ๊ธ์ด๋ฉด ์ค๋ฐ๊ฟ ํ์ง.""" | |
| out = "" | |
| for key in sorted(segs): | |
| t = LX.render_question(segs[key], ctx, mark_breaks=True) | |
| if not t: | |
| continue | |
| if out: | |
| out += LX.LINE_BREAK if (_hangul(out[-1]) and _hangul(t[0])) else " " | |
| out += t | |
| return out | |
| # ------------------------------------------------------------ ์ค๋ฐ๊ฟ ์๋ฆฌ: ๋ถ์ผ๊น, ๋์ธ๊น | |
| # ๋ถ์ธ ํํ๊ฐ ์๋ฃ์ ์์ ๋๋ง ์ฐ๋ '๋ท์กฐ๊ฐ์ด ์กฐ์ฌยท์ด๋ฏธ๋ฟ' ๋ชฉ๋ก. | |
| # 'ํ๋ฉด/ํ๊ณ /ํ์ฌ' ๋ ๋ฃ์ง ์์: '~๋ผ ํ๋ฉด', '~์ผ ํ๊ณ ' ์ฒ๋ผ ๋์ด ์ฐ๋ ๊ฒฝ์ฐ๊ฐ ํํจ. | |
| JOSA = {"์", "๋ฅผ", "์ด", "๊ฐ", "์", "๋", "์", "์", "๋", "์", "๊ณผ", "๋ก", "์ผ๋ก", "์์", "์ด๋ค", "๋ค", | |
| "์ด๊ณ ", "์ด๋ฏ๋ก", "๋ฏ๋ก", "์ด๋ฉด", "๋ฉด", "๋ถํฐ", "๊น์ง", "๋ง", "๊ณ ", "๋ฉฐ", "๋ผ", "์ด๋ผ"} | |
| UNITS = ("๊ฐ์ง", "๊ฐ", "๋ฐฐ", "๋ฒ", "๋ช ", "์งธ", "๋งํผ", "์๊ฐ", "๋ฑ๋ถ", "์๋ฆฌ", "์ฅ", "ํ", "์ธต", "cm", "m") | |
| _PUNCT = ".,)]}ยทโฆ:;!?'\"" | |
| def _strip(w: str) -> str: | |
| return w.rstrip(_PUNCT) | |
| def word_counts(texts): | |
| """์ค ์์ชฝ(์ค๋ฐ๊ฟ ํ์ง๊ฐ ์๋) ์ด์ ๋น๋. ๋ ๋ฌธ์ฅ๋ถํธ๋ฅผ ๋ ํํ๋ ํจ๊ป ์ผ๋ค.""" | |
| c = Counter() | |
| for t in texts: | |
| for w in t.split(" "): | |
| if not w or LX.LINE_BREAK in w: | |
| continue | |
| c[w] += 1 | |
| s = _strip(w) | |
| if s != w: | |
| c[s] += 1 | |
| return c | |
| def resolve_breaks(text: str, counts: Counter) -> str: | |
| """'์์ด์ <ํ์ง>๋ท์ด์ ' -> ๋ถ์ธ ํํ๊ฐ ์๋ฃ์ ์๊ณ ๋ท์กฐ๊ฐ๋ณด๋ค ํํ๋ฉด ๋ถ์, ์๋๋ฉด ๊ณต๋ฐฑ. | |
| ๋ถ์ธ ํํ๊ฐ ์๋ฃ์ ์์ ๋๋ ๋ท์กฐ๊ฐ์ด ์กฐ์ฌยท์ด๋ฏธ๋ฟ์ด๋ฉด ๋ถ์.""" | |
| def decide(m): | |
| a, b = m.group(1), m.group(2) | |
| a_word = a.rsplit("$", 1)[-1] if "$" in a else a # ์์ ๋ค์ ๋ถ์ ํ๊ธ๋ง ๋น๊ต | |
| if not a_word: # ์์ด ์์์ผ๋ก ๋๋จ: ๋ท์กฐ๊ฐ์ด ์กฐ์ฌ์ผ ๋๋ง ๋ถ์ ('$\angle BAC$|๋ฅผ'), ์๋๋ฉด ์ ๋ง('๋ฐ๋ผ์') | |
| # ์ซ์๋ก ๋๋๋ ์์ ๋ค์ ๋จ์ ๋ช ์ฌ๋ ๋ถ์ ('$2$|๊ฐ์ง', '$13$|๊ฐ์ฉ', '$\frac{1}{3}$|๋ฐฐ') | |
| numeric = re.search(r"[0-9}]\$$", a) is not None | |
| if _strip(b) in JOSA or (numeric and _strip(b).startswith(UNITS)): | |
| return a + b | |
| return a + " " + b | |
| joined = _strip(a_word + b) | |
| cj, cb, ca = counts[joined], counts[_strip(b)], counts[_strip(a_word.lstrip("([{'\""))] | |
| # ๋ถ์ด๋ ๊ฒฝ์ฐ: | |
| # - ๋ท์กฐ๊ฐ์ด ์กฐ์ฌยท์ด๋ฏธ (๋จ๋ ๋จ์ด๊ฐ ๋ ์ ์์; '์ด' ๋ ์ง์์ด์ผ ์ ์์ด ์ ์ธ) | |
| # - ๋ถ์ธ ํํ๊ฐ ๋ท์กฐ๊ฐ ๋จ๋ ๋ณด๋ค ํํจ | |
| # - ์์กฐ๊ฐ์ด ๋จ๋ ์ผ๋ก ๋์จ ์ ์ด ์๊ณ ๋ถ์ธ ํํ๋ ์์ ('๋์ด|ํ๋ฉด', '๊ผญ์ง|์ ์') | |
| # - ์๋ค ์กฐ๊ฐ ๋ชจ๋ ๋จ๋ ์ผ๋ก ๋์จ ์ ์ด ์์ ('์ง|๋๊ฒ', '๋ฐ|๋ผ๊ฐ์ผ') | |
| # (๋ค ๋ ๊ท์น์ ์์กฐ๊ฐ์ด ์กฐ์ฌ๋ก ๋๋๋ฉด ์ฐ์ง ์์: '[๊ทธ๋ฆผ9]์|๊ฒฝ์ฐ๊ฐ', '์ข๋ณ๊ณผ|์ฐ๋ณ์' ๋ ๋์ด ์. | |
| # ๋ง์ง๋ง ๊ท์น์ ์์กฐ๊ฐ์ด ์ธ ๊ธ์ ์ดํ์ธ ์กฐ๊ฐ์ผ ๋๋ง) | |
| a_core = _strip(a_word.lstrip("([{'\"")) | |
| a_josa_end = a_core[-1:] in "์๋ก๊ณผ์์๋ฅผ์๋์๊ฐ๋" | |
| if ((_strip(b) in JOSA and _strip(b) != "์ด") or cj > cb or (cj == 0 and _strip(b) in JOSA) | |
| or (not a_josa_end and ca == 0 and cj > 0) | |
| or (not a_josa_end and len(a_core) <= 3 and ca == 0 and cb == 0) | |
| # ์์กฐ๊ฐ์ด ํ ๊ธ์์ด๊ณ ๋ถ์ธ ํํ๊ฐ ์๋ฃ์ 3๋ฒ ์ด์ ('๋์ด๋ฅผ ๊ตฌ|ํ๋': 'ํ๋' ๋จ๋ ๋ ํํด ๋์ด์ก์) | |
| or (len(a_core) == 1 and cj >= 3)): | |
| return a + b | |
| return a + " " + b | |
| pat = re.compile(r"(\S*)" + LX.LINE_BREAK + r"([^\s" + LX.LINE_BREAK + r"]*)") | |
| while LX.LINE_BREAK in text: | |
| new = pat.sub(decide, text, count=1) | |
| if new == text: | |
| text = text.replace(LX.LINE_BREAK, " ") | |
| break | |
| text = new | |
| return text | |
| def _norm(s: str) -> str: | |
| s = s.replace("$", "").replace("\\left", "").replace("\\right", "").replace("\\,", "") | |
| s = re.sub(r"\s+", "", s) | |
| s = re.sub(r"[.,ใ]+$", "", s) | |
| return s | |
| def explanation_choice(text: str, choices: list[str] | None): | |
| """ํด์ค์ด ๊ฐ๋ฆฌํค๋ ๋ณด๊ธฐ ๋ฒํธ์ ๊ทธ ๊ทผ๊ฑฐ. | |
| 1) ํด์ค ๋งจ ๋(๋ง์ง๋ง 20์)์ โ ~โค๊ฐ ์์ผ๋ฉด ๊ทธ๊ฒ ('circle') | |
| 2) ์๋๋ฉด ๋ง์ง๋ง ์์์ ๋ง์ง๋ง '=' ๋ค ๊ฐ์ด ๋ณด๊ธฐ ์ค ์ ํํ ํ๋์ ๊ฐ์ผ๋ฉด ๊ทธ ๋ฒํธ ('value')""" | |
| tail = text.rstrip()[-20:] | |
| circ = [ch for ch in tail if ch in CIRC] | |
| if circ: | |
| return CIRC.index(circ[-1]) + 1, "circle" | |
| if not choices or len(choices) != 5: | |
| return None, None | |
| maths = re.findall(r"\$([^$]+)\$", text) | |
| if not maths: | |
| return None, None | |
| last = maths[-1].split("=")[-1] | |
| hit = [i + 1 for i, c in enumerate(choices) if _norm(c) and _norm(c) == _norm(last)] | |
| if len(hit) == 1: | |
| return hit[0], "value" | |
| return None, None | |
| def load_choices(latex_dir: Path | None, exam: str): | |
| f = latex_dir / f"{exam}.jsonl" if latex_dir else None | |
| if not f or not f.exists(): | |
| return {} | |
| out = {} | |
| with open(f, encoding="utf-8") as fh: | |
| for line in fh: | |
| q = json.loads(line) | |
| out[q["number"]] = q.get("choices") | |
| return out | |
| def inject_missing_glyphs(pdf, path: Path): | |
| """pdfplumber ๊ฐ ๋ฒ๋ฆฌ๋ ๊ธ์(configs/ebsi_missing_glyphs.json: 2021_11 ํด์ค์ ใ ~ใฃยทใฑใดใทยทโ ฐ~โ ถยทโยทโฒยท| 99๊ฐ)๋ฅผ | |
| ํ์ด์ง ๊ธ์ ๋ชฉ๋ก์ ๋ผ์ ๋ฃ๋๋ค. ๋์ดยทํฌ๊ธฐ๋ ๊ฐ์ ์ค ์ผ์ชฝ ์ด์ ๊ธ์(๊ฐ๋ก์ ์ ์ธ)๋ฅผ ๋ฐ๋ฅด๊ณ (์ฒจ์ ์คํ ๋ฐฉ์ง), | |
| ๊ธ๊ผด์ ๊ธ ๊ธฐํธ(ใ ยทใฑยทโ ฐ ๋ฑ)๋ฉด ํ๊ธ ๋ณธ๋ฌธ ๊ธ๊ผด, ์์ ๊ธฐํธ(โฒยท|ยทโ)๋ฉด ์ด์ ๊ธ๊ผด.""" | |
| f = HERE.parent / "configs" / "ebsi_missing_glyphs.json" | |
| if not f.exists(): | |
| return 0 | |
| rows = json.loads(f.read_text(encoding="utf-8")).get(path.name, []) | |
| texty = set("ใ ใกใขใฃใคใฑใดใทในโ ฐโ ฑโ ฒโ ณโ ดโ ตโ ถ") | |
| for r in rows: | |
| p = pdf.pages[r["page"]] | |
| orig = [c for c in p.chars if not c.get("missing_glyph")] | |
| body = [c for c in orig if _hangul(c["text"])] | |
| bsize = statistics.median([c["size"] for c in body]) if body else 8.0 | |
| bfont = Counter(c["fontname"] for c in body).most_common(1)[0][0] if body else "missing" | |
| nb = [c for c in orig if c["text"].strip() and c["text"] != "๎ญ" and r["x0"] - 15 <= c["x1"] <= r["x0"] + 1 | |
| and c["top"] < r["bottom"] and c["bottom"] > r["top"]] | |
| if nb: | |
| n = max(nb, key=lambda c: c["x1"]) | |
| top, bot, size = n["top"], n["bottom"], n["size"] | |
| else: | |
| cy = (r["top"] + r["bottom"]) / 2 | |
| top, bot, size, n = cy - bsize / 2, cy + bsize / 2, bsize, None | |
| font = bfont if r["text"] in texty or n is None else n["fontname"] | |
| p.chars.append({"text": r["text"], "x0": r["x0"], "x1": r["x1"], "top": top, "bottom": bot, | |
| "size": size, "fontname": font, "matrix": (1, 0, 0, 1, 0, 0), "upright": True, | |
| "doctop": top, "missing_glyph": True}) | |
| return len(rows) | |
| def process_pdf(path: Path, latex_dir: Path | None = None): | |
| exam = path.name.replace("_solution.pdf", "") | |
| problems = [] | |
| choices = load_choices(latex_dir, exam) | |
| with pdfplumber.open(path) as pdf: | |
| LX.set_eq_fonts(pdf) # ์ต๋ช ๊ธ๊ผด(CIDFont+Fn) PDF ์ ์์ ๊ธ๊ผด ํ์ | |
| inject_missing_glyphs(pdf, path) | |
| ans, err1 = parse_answers(pdf) | |
| if len(ans) < 30: # ๊ธ์๋ฅผ ๋ชป ์ฝ์ ์นธ์ด ์์ผ๋ฉด pdfium ์ผ๋ก ํ ๋ฒ ๋ | |
| ans, err1 = parse_answers(pdf, pdfium_extra_chars(path)) | |
| select = None | |
| if exam.endswith("_hs3"): | |
| if len([k for k in ans if k <= 22]) < 22: # ํ๊ฐ์ ํ์: 1์ชฝ '01. โฃ โฆ' ๋ชฉ๋ก | |
| ans = parse_list_answers(pdf) | {k: v for k, v in ans.items() if k <= 22} | |
| hd = {h["name"]: h for h in section_headers(pdf)} | |
| if "ํํต" in hd: | |
| t = hd["ํํต"] | |
| later = [h for k, h in hd.items() if k != "ํํต" and (h["page"], h["x"], h["top"]) > (t["page"], t["x"], t["top"])] | |
| select = {"skip": t, "cut": min(later, key=lambda h: (h["page"], h["x"], h["top"])) if later else None} | |
| sel = parse_select_answers(pdf, t) | |
| ans = {k: v for k, v in ans.items() if k <= 22} | sel | |
| # pdfplumber ๊ฐ โ ~โค๋ฅผ ๋ชป ์ฝ์ ์นธ: pdfium ๊ธ์๋ก ๋ณด์ถฉ (๋น ์นธ๋ง) | |
| for rng in (range(1, 23), range(23, 31)): | |
| if any(k not in ans for k in rng): | |
| ans = pdfium_list_answers(path, rng) | ans | |
| ans = {k: v for k, v in ans.items() if 1 <= k <= 30} | |
| err1 = None if len(ans) == 30 else f"์ ๋ตํ {len(ans)}/30 (๊ณตํต+ํํต)" | |
| # ์๋ณธ ์ด๋ฏธ์ง๋ก ํ์ธํ ์ ๋ต ๋ณด์ (์ ๋ตํ ๊ธ์๊ฐ ๊นจ์ง ํ: 2021_11ยท2023_11 ๊ณ 3 ์ ์ชฝ ๊ทธ๋ฆผ + OCR ๊ธ์ ์ธต) | |
| ao = ANSWER_OVERRIDES.get(exam, {}) | |
| if ao: | |
| ans = {k: v for k, v in ans.items()} | ao | |
| err1 = None if len(ans) >= 30 else err1 | |
| sol, err2 = split_solutions(pdf, select=select) | |
| for n in range(1, 31): | |
| ctx = LX.Ctx() | |
| segs = sol.get(n, {}).get("segs", {}) | |
| text = render_solution(segs, ctx) if segs else "" | |
| # '( โ ฐ )' -> '(โ ฐ)' (2021_11 ์ ๊ดํธ์ ๋ก๋ง์ซ์ ์ฌ์ด์ ๊ณต๋ฐฑ ๊ธ์๊ฐ ์์) | |
| text = re.sub(r"\(\s*([โ ฐโ ฑโ ฒโ ณโ ดโ ตโ ถโ ทโ ธโ นโ โ กโ ขโ ฃโ คโ ฅโ ฆโ งโ จโ ฉ]+)\s*\)", r"(\1)", text) | |
| # '(โ ฐ$)n = 2$' -> '(โ ฐ) $n = 2$' (๋ซ๋ ๊ดํธ๊ฐ ์์์ ๋ถ์ด ๋ค์ด๊ฐ ๊ฒฝ์ฐ: 2020_09_hs2 ๋ฑ) | |
| text = re.sub(r"\(([โ ฐโ ฑโ ฒโ ณโ ดโ ตโ ถโ ทโ ธโ นโ โ กโ ขโ ฃโ คโ ฅโ ฆโ งโ จโ ฉ]+)\$\)", r"(\1) $", text) | |
| # ์๋ฌธ i ๋ก ์ด ๋ก๋ง์ซ์๊ฐ ์์ ๋งจ ์์ ๋ถ์ ๊ฒฝ์ฐ: '$(i)a > 0$' -> '(โ ฐ) $a > 0$', '$(i i)$' -> '(โ ฑ)' | |
| # (2022_06_hs2 ํด์ค 21ยท30๋ฒ) | |
| ROM = {"i": "โ ฐ", "i i": "โ ฑ", "i i i": "โ ฒ", "i v": "โ ณ", "v": "โ ด"} | |
| text = re.sub(r"\$\((i i i|i i|i v|i|v)\)\$", lambda g: "(" + ROM[g.group(1)] + ")", text) | |
| text = re.sub(r"\$\((i i i|i i|i v|i|v)\)(?=[^$])", lambda g: "(" + ROM[g.group(1)] + ") $", text) | |
| # '(โ ฐ) , (โ ฑ) ์' -> '(โ ฐ), (โ ฑ)์' | |
| text = re.sub(r"\(([โ ฐโ ฑโ ฒโ ณโ ดโ ตโ ถโ ทโ ธโ นโ โ กโ ขโ ฃโ คโ ฅโ ฆโ งโ จโ ฉ]+)\) (?=[,์])", r"(\1)", text) | |
| # '(โ ฐ) $\sim($โ ฒ)์' -> '(โ ฐ) $\sim$ (โ ฒ)์' (2024_09_hs2ยท2025_09_hs2) | |
| text = re.sub(r"\$\\sim\(\$([โ ฐโ ฑโ ฒโ ณโ ดโ ตโ ถโ ทโ ธโ นโ โ กโ ขโ ฃโ คโ ฅโ ฆโ งโ จโ ฉ]+)\)", r"$\\sim$ (\1)", text) | |
| # '$A \cap B$=$\{ 5,9 \}$' -> '$A \cap B = \{ 5,9 \}$' (2025_03_hs2) | |
| text = re.sub(r"(?<=[^)\s])\$=\$(?=[^(])", " = ", text) | |
| # ์์ ์ 'โฆ'ยท'โง' -> '\le'ยท'\ge' (2025_03_hs2 26๋ฒ) | |
| text = text.replace("โฆ", r" \le ").replace("โง", r" \ge ") | |
| text = TBL.convert_tables(text) # ์ฆ๊ฐํยท๋ถํธํ -> array (์นธ ์๊ฐ ๋ง์ ๋๋ง) | |
| # ๋น์นธ '(๊ฐ)' ๊ดํธ๊ฐ ์์ ๊ฒฝ๊ณ์ ์์ -> \boxed{(๊ฐ)} (12 ๋ฌธ์ ๋ณธ๋ฌธ๊ณผ ๊ฐ์ ๊ท์น; '์ฆ, (๊ฐ$)= - \frac{1}{n}$') | |
| text = re.sub(r"\(\$([๊ฐ๋๋ค๋ผ๋ง])\$\)", r"\\boxed{(\1)}", text) | |
| text = re.sub(r"\(\$([๊ฐ๋๋ค๋ผ๋ง])\)", r"\\boxed{(\1)}$", text) | |
| text = re.sub(r"\(([๊ฐ๋๋ค๋ผ๋ง])\$\)", r"$\\boxed{(\1)}", text) | |
| reasons = list(ctx.reasons) | |
| a = ans.get(n) | |
| if a is None: | |
| reasons.append("์ ๋ตํ์์ ์ ๋ต์ ๋ชป ์ฝ์") | |
| if not text: | |
| reasons.append("ํด์ค์ ๋ชป ์ฐพ์") | |
| if "$$" in text or re.search(r"\?[0-9A-F]{4}", text): | |
| reasons.append("ํด์ค ์์ ๋ณํ ์ ๊ฒ ํ์") | |
| problems.append({ | |
| "exam": exam, "number": n, | |
| "answer_kind": a["kind"] if a else None, | |
| "answer": a["value"] if a else None, | |
| "answer_source": a.get("source") if a else None, | |
| "explanation_choice": None, | |
| "explanation_choice_by": None, | |
| "explanation_latex": text, # ์ค๋ฐ๊ฟ ํ์ง ํฌํจ (finalize ์์ ์ ๋ฆฌ) | |
| "answer_check": "n/a", | |
| "needs_review": bool(reasons), | |
| "review_reasons": reasons, | |
| "_choices": choices.get(n), | |
| }) | |
| errs = [e for e in (err1, err2) if e] | |
| return problems, errs | |
| REVIEW_DIR = Path("data/ebsi/review") | |
| def _load_answer_overrides(): | |
| """data/ebsi/review/answer_overrides.jsonl: {exam, number, kind(choice|integer), value, note}. ๋์ค ์ค ์ฐ์ .""" | |
| f = Path(__file__).resolve().parents[1] / "data/ebsi/review/answer_overrides.jsonl" | |
| out: dict[str, dict] = {} | |
| if f.exists(): | |
| for line in open(f, encoding="utf-8"): | |
| if line.strip(): | |
| r = json.loads(line) | |
| out.setdefault(r["exam"], {})[r["number"]] = {"kind": r["kind"], "value": r["value"], "source": "manual"} | |
| return out | |
| ANSWER_OVERRIDES = _load_answer_overrides() | |
| def load_overrides(review_dir: Path = REVIEW_DIR): | |
| """ํ๋ฉด ๋์กฐ๋ก ์ฌ๋์ด ๊ณ ์น ํด์ค: {(exam, number): explanation_latex}. ๋์ค ์ค์ด ์ ์ค์ ๋ฎ์.""" | |
| f = review_dir / "solution_overrides.jsonl" | |
| out = {} | |
| if f.exists(): | |
| for line in open(f, encoding="utf-8"): | |
| if line.strip(): | |
| r = json.loads(line) | |
| out[(r["exam"], r["number"])] = r["explanation_latex"] | |
| return out | |
| def load_review(review_dir: Path = REVIEW_DIR): | |
| """ํ๋ฉด ๋์กฐ ๊ธฐ๋ก: {(exam, number): {"status": "verified"|"fixed", "date": ..., "note": ...}}""" | |
| f = review_dir / "solution_review.jsonl" | |
| out = {} | |
| if f.exists(): | |
| for line in open(f, encoding="utf-8"): | |
| if line.strip(): | |
| r = json.loads(line) | |
| out[(r["exam"], r["number"])] = r | |
| return out | |
| def finalize(q: dict, counts: Counter, overrides=None, review=None): | |
| """์ค๋ฐ๊ฟ ํ์ง ์ ๋ฆฌ -> (์์ผ๋ฉด) ์ฌ๋์ด ๊ณ ์น ํด์ค๋ก ๊ต์ฒด -> ๊ฐ๊ด์ ๋์กฐ.""" | |
| q["explanation_latex"] = resolve_breaks(q["explanation_latex"], counts) | |
| key = (q["exam"], q["number"]) | |
| q["explanation_source"] = "auto" | |
| if overrides and key in overrides: | |
| q["explanation_latex"] = overrides[key] | |
| q["explanation_source"] = "manual" | |
| q["review_reasons"] = [] # ์๋ ๋ณํ ๊ฒฝ๊ณ ๋ ์ฌ๋์ด ๊ณ ์น ๋ณธ๋ฌธ์๋ ํด๋น ์์ | |
| rv = (review or {}).get(key) | |
| q["visual_check"] = rv["status"] if rv else None # ์๋ณธ ํ๋ฉด ๋์กฐ ์ฌ๋ถ (None = ์์ง) | |
| if rv and rv.get("text_sha"): | |
| import hashlib | |
| now = hashlib.sha1(" ".join(q["explanation_latex"].split()).encode("utf-8")).hexdigest()[:12] | |
| if now != rv["text_sha"]: | |
| q["visual_check"] = "stale" # ๋์กฐ ๋ค ๊ท์น์ด ๋ฐ๋์ด ํ ์คํธ๊ฐ ๋ฌ๋ผ์ง -> ๋ค์ ๋์กฐํด์ผ ํจ | |
| q["needs_review"] = bool(q["review_reasons"]) or q["visual_check"] in (None, "stale") | |
| exp_choice, exp_how = explanation_choice(q["explanation_latex"], q.pop("_choices")) | |
| q["explanation_choice"], q["explanation_choice_by"] = exp_choice, exp_how | |
| if q["answer_kind"] == "choice": | |
| q["answer_check"] = ("no_choice_in_explanation" if exp_choice is None | |
| else "ok" if exp_choice == q["answer"] else "mismatch") | |
| elif q["answer_kind"] == "integer": | |
| # ๋จ๋ต: ํด์ค ๋ง์ง๋ง ์์์ ๋ง์ง๋ง '=' ๋ค ๊ฐ์ด ์ ๋ต ์ ์์ ๊ฐ์์ง | |
| v = explanation_value(q["explanation_latex"]) | |
| q["explanation_value"] = v | |
| q["answer_check"] = "no_value_in_explanation" if v is None else "ok" if v == q["answer"] else "mismatch" | |
| def explanation_value(text: str): | |
| """ํด์ค ๋์ ๊ฐ(์ ์)์ ์ฐพ๋๋ค: ๋ง์ง๋ง ์์๋ถํฐ ๊ฑฐ์ฌ๋ฌ ์ฌ๋ผ๊ฐ๋ฉฐ '= ์ ์' ๋ก ๋๋๋ ์ฒซ ์์.""" | |
| maths = re.findall(r"\$([^$]+)\$", text) | |
| for m in reversed(maths[-3:]): | |
| last = _norm(m.split("=")[-1]) | |
| if re.fullmatch(r"-?\d+", last): | |
| return int(last) | |
| return None | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--pdf", type=Path) | |
| ap.add_argument("--dir", type=Path) | |
| ap.add_argument("--out", type=Path, default=Path("data/ebsi/solutions")) | |
| ap.add_argument("--latex-dir", type=Path, default=Path("data/ebsi/latex"), | |
| help="2๋จ๊ณ ๊ฒฐ๊ณผ(๋ณด๊ธฐ ๋ด์ฉ). ํด์ค ๋ง์ง๋ง ๊ฐ๊ณผ ๋ณด๊ธฐ๋ฅผ ๋์กฐํ ๋ ์") | |
| ap.add_argument("--show", nargs="*", type=int) | |
| a = ap.parse_args() | |
| pdfs = [a.pdf] if a.pdf else sorted(a.dir.glob("*_solution.pdf")) if a.dir else [] | |
| if not pdfs: | |
| sys.exit("์ฒ๋ฆฌํ PDF ๊ฐ ์์ด์ (--pdf ๋๋ --dir)") | |
| a.out.mkdir(parents=True, exist_ok=True) | |
| tot = rev = mism = noans = vis_tot = 0 | |
| why = Counter() | |
| mismatches, nochoice = [], [] | |
| done = [(pdf, *process_pdf(pdf, a.latex_dir)) for pdf in pdfs] | |
| # ์ค๋ฐ๊ฟ ํ์ ์ฉ ์ด์ ๋น๋: ์ด๋ฒ ํด์ค ์ ๋ถ + 2๋จ๊ณ ๋ฌธ์ ๋ณธ๋ฌธ/๋ณด๊ธฐ | |
| corpus = [q["explanation_latex"] for _, items, _ in done for q in items] | |
| # ๊ฐ์ ํ๋ ๋ฌธ์ ๋ณธ๋ฌธ๋ง (๋ค๋ฅธ ํ๋ ํ์ผ์ด ๋์ด๋๋ฉด ๋น๋๊ฐ ๋ฐ๋์ด ์ด๋ฏธ ๋์กฐํ ํด์ค์ ์ค๋ฐ๊ฟ์ด ๋ฌ๋ผ์ก์: 2024_03_hs1 11๋ฒ) | |
| grades = {p.name.split("_solution")[0].rsplit("_hs", 1)[-1] for p in pdfs} | |
| for f in sorted(x for x in a.latex_dir.glob("*.jsonl") if x.stem.rsplit("_hs", 1)[-1] in grades) \ | |
| if a.latex_dir and a.latex_dir.exists() else []: | |
| for line in open(f, encoding="utf-8"): | |
| q = json.loads(line) | |
| corpus.append(q.get("stem_latex", "")) | |
| corpus.extend(q.get("choices") or []) | |
| counts = word_counts(corpus) | |
| overrides, review = load_overrides(), load_review() | |
| for pdf, items, errs in done: | |
| for q in items: | |
| finalize(q, counts, overrides, review) | |
| exam = items[0]["exam"] | |
| with open(a.out / f"{exam}.jsonl", "w", encoding="utf-8") as f: | |
| for q in items: | |
| f.write(json.dumps(q, ensure_ascii=False) + "\n") | |
| got = sum(q["answer"] is not None for q in items) | |
| mm = sum(q["answer_check"] == "mismatch" for q in items) | |
| r = sum(bool(q["review_reasons"]) for q in items) | |
| vis = sum(q["visual_check"] in ("verified", "fixed") for q in items) | |
| stale = [q["number"] for q in items if q["visual_check"] == "stale"] | |
| if stale: | |
| errs = list(errs) + [f"๋์กฐ ๋ค ํ ์คํธ ๋ฐ๋(์ฌ๋์กฐ ํ์): {stale}"] | |
| vis_tot += vis | |
| tot += len(items); rev += r; mism += mm; noans += 30 - got | |
| for q in items: | |
| why.update(q["review_reasons"]) | |
| if q["answer_check"] == "mismatch": | |
| mismatches.append(q) | |
| elif q["answer_check"] in ("no_choice_in_explanation", "no_value_in_explanation"): | |
| nochoice.append(q) | |
| print(f"{exam}: ์ ๋ต {got}/30, ์๋๊ฒฝ๊ณ {r}, ํ๋ฉด๋์กฐ {vis}/30, ๋ณด๊ธฐ๋ฒํธ ๋ถ์ผ์น {mm} {'; '.join(errs)}") | |
| if a.show: | |
| for q in items: | |
| if q["number"] in a.show: | |
| print(f"\n[{exam} {q['number']}๋ฒ] ์ ๋ต={q['answer']} ({q['answer_kind']}) ๊ฒ์ฆ={q['answer_check']}\n{q['explanation_latex'][:600]}") | |
| print(f"\n์ ์ฒด {tot}๋ฌธํญ: ์ ๋ต ๋ชป ์ฝ์ {noans}, ์๋๊ฒฝ๊ณ {rev}, ํ๋ฉด๋์กฐ ์๋ฃ {vis_tot}/{tot}, ๋ณด๊ธฐ๋ฒํธ ๋ถ์ผ์น {mism}") | |
| for k, v in why.most_common(): | |
| print(f" - {k}: {v}") | |
| okc = sum(1 for f in [a.out / f"{p.name.replace('_solution.pdf', '')}.jsonl" for p in pdfs] | |
| for line in open(f, encoding="utf-8") if json.loads(line)["answer_check"] == "ok") | |
| print(f"\n[์ ๋ตโํด์ค ๋์กฐ] ์ผ์น {okc} / ๋ถ์ผ์น {len(mismatches)} / ์๋ ๋์กฐ ๋ถ๊ฐ {len(nochoice)} (ํ๋ฉด ๋์กฐ๋ก ํ์ธ)") | |
| print(" ๊ฐ๊ด์: ํด์ค ๋ โ ~โค ๋๋ ๋ง์ง๋ง '=' ๋ค ๊ฐ์ด ๋ณด๊ธฐ ํ๋์ ๊ฐ์ ๋ / ๋จ๋ต: ๋ง์ง๋ง '= ์ ์' ๊ฐ ์ ๋ต๊ณผ ๊ฐ์ ๋") | |
| for q in mismatches: | |
| tail = q["explanation_latex"][-80:].replace("\n", " ") | |
| if q["answer_kind"] == "choice": | |
| got = f"์ ๋ตํ {CIRC[q['answer'] - 1]} / ํด์ค {CIRC[q['explanation_choice'] - 1]}({q['explanation_choice_by']})" | |
| else: | |
| got = f"์ ๋ตํ {q['answer']} / ํด์ค {q.get('explanation_value')}" | |
| print(f" ๋ถ์ผ์น {q['exam']} {q['number']:>2}๋ฒ: {got} โฆ{tail}") | |
| by_exam = Counter(q["exam"] for q in nochoice) | |
| if nochoice: | |
| print(" ์๋ ๋์กฐ ๋ถ๊ฐ(์ํ๋ณ): " + ", ".join(f"{k} {v}" for k, v in sorted(by_exam.items()))) | |
| if __name__ == "__main__": | |
| main() | |