"""EBSi(h1·h2·h3) + AI Hub 단원별 문항 수를 합친 표를 만든다. python scripts\\20_coverage_all.py 입력 data/aihub_normalized/coverage_after.csv AI Hub (unit_id, unit_name, A_low, A_mid, A_high, usable_low, usable_mid, usable_high) data/ebsi/coverage_ebsi_h1.csv / _h2 / _h3 EBSi 학년별 (같은 열) — scripts/16_ebsi_units.py build 결과 출력 data/coverage_all.csv unit_id, unit_name, aihub_A, ebsi_h1_A, ebsi_h2_A, ebsi_h3_A, (출처별 A 합) aihub_usable, ebsi_h1_usable, ebsi_h2_usable, ebsi_h3_usable, (출처별 usable 합 = low+mid+high) A_low, A_mid, A_high, usable_low, usable_mid, usable_high, (전체 합) usable_total A = 글자로 완비, usable = A + B (그림·상자·이미지 함께 표시). C(자동 경고)는 빠짐. 비용·외부 쓰기 없음. """ import csv from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SOURCES = [ ("aihub", ROOT / "data/aihub_normalized/coverage_after.csv"), ("ebsi_h1", ROOT / "data/ebsi/coverage_ebsi_h1.csv"), ("ebsi_h2", ROOT / "data/ebsi/coverage_ebsi_h2.csv"), ("ebsi_h3", ROOT / "data/ebsi/coverage_ebsi_h3.csv"), ] COLS = ["A_low", "A_mid", "A_high", "usable_low", "usable_mid", "usable_high"] OUT = ROOT / "data/coverage_all.csv" def read(p): with open(p, encoding="utf-8-sig", newline="") as f: return [r for r in csv.DictReader(f) if r.get("unit_id")] def main(): order, names, tot, per, perA = [], {}, {}, {}, {} for src, p in SOURCES: if not p.exists(): raise SystemExit(f"없음: {p}") for r in read(p): u = r["unit_id"] if u not in names: order.append(u) names[u] = r["unit_name"] tot[u] = {c: 0 for c in COLS} for c in COLS: tot[u][c] += int(r[c] or 0) per[(src, u)] = sum(int(r[c] or 0) for c in ("usable_low", "usable_mid", "usable_high")) perA[(src, u)] = sum(int(r[c] or 0) for c in ("A_low", "A_mid", "A_high")) head = (["unit_id", "unit_name"] + [f"{s}_A" for s, _ in SOURCES] + [f"{s}_usable" for s, _ in SOURCES] + COLS + ["usable_total"]) with open(OUT, "w", encoding="utf-8-sig", newline="") as f: w = csv.writer(f) w.writerow(head) for u in order: t = tot[u] w.writerow([u, names[u]] + [perA.get((s, u), 0) for s, _ in SOURCES] + [per.get((s, u), 0) for s, _ in SOURCES] + [t[c] for c in COLS] + [t["usable_low"] + t["usable_mid"] + t["usable_high"]]) srcsum = {s: sum(per.get((s, u), 0) for u in order) for s, _ in SOURCES} srcA = {s: sum(perA.get((s, u), 0) for u in order) for s, _ in SOURCES} print(f"단원 {len(order)}개 -> {OUT}") print("출처별 A:", srcA, "| 합", sum(srcA.values())) print("출처별 usable(A+B):", srcsum, "| 합", sum(srcsum.values())) print("참고: AI Hub 는 DB 에 A 만 적재됨(B 미적재). EBSi 는 A+B 모두 적재(B 는 문항 이미지 함께 표시).") empty = [u for u in order if tot[u]["usable_low"] + tot[u]["usable_mid"] + tot[u]["usable_high"] == 0] print(f"문항 0개 단원 {len(empty)}개:", " ".join(empty)) if __name__ == "__main__": main()