Files
cuotibiye/tools/dump_plan_input.py
T

66 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""把评测语料里能算的题抽出来,喂给前端解析生成器做自校验。
输出 data/bench/plan_input.json:[{"stem": "...", "answer": "...", "src": "bench|db"}]
"""
import json
import sqlite3
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
BENCH = ROOT / "data" / "bench" / "results.jsonl"
DB = ROOT / "data" / "app.db"
OUT = ROOT / "data" / "bench" / "plan_input.json"
def from_bench():
out = []
if not BENCH.exists():
return out
for line in BENCH.read_text(encoding="utf-8").splitlines():
line = line.strip()
if not line:
continue
rec = json.loads(line)
if not rec.get("ok"):
continue
for b in rec.get("blocks") or []:
if b.get("type") in ("calc", "card") and b.get("answer") and b.get("stem"):
out.append({"stem": b["stem"], "answer": str(b["answer"]), "src": "bench",
"judge_type": b.get("type")})
return out
def from_db():
out = []
if not DB.exists():
return out
conn = sqlite3.connect(str(DB))
try:
rows = conn.execute("SELECT stem, answer, judge_type FROM questions").fetchall()
finally:
conn.close()
for stem, answer, jt in rows:
if answer and stem:
out.append({"stem": stem, "answer": str(answer), "src": "db", "judge_type": jt})
return out
def main():
items = from_bench() + from_db()
seen, uniq = set(), []
for it in items:
k = (it["stem"], it["answer"])
if k in seen:
continue
seen.add(k)
uniq.append(it)
OUT.parent.mkdir(parents=True, exist_ok=True)
OUT.write_text(json.dumps(uniq, ensure_ascii=False, indent=1), encoding="utf-8")
print(f"抽取 {len(uniq)} 道(bench {sum(1 for x in uniq if x['src'] == 'bench')},"
f"db {sum(1 for x in uniq if x['src'] == 'db')})→ {OUT}")
if __name__ == "__main__":
main()