63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
"""把历史题目里空着的知识点补上(一次性维护脚本,可重复跑,幂等)。
|
||
|
||
python tools/backfill_kp.py # 只看会补哪些,不写库
|
||
python tools/backfill_kp.py --apply # 真写
|
||
|
||
判据用的是运行时同一套规则(`backend.kp_match`),所以补出来的结果和
|
||
新入库的题完全一致。认不准的仍然留空——不硬猜。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import os
|
||
import sqlite3
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
ROOT = Path(__file__).resolve().parent.parent
|
||
sys.path.insert(0, str(ROOT))
|
||
|
||
from backend import kp_match # noqa: E402
|
||
|
||
|
||
def db_path() -> Path:
|
||
return Path(os.environ.get("QINGLING_DB") or ROOT / "data" / "app.db")
|
||
|
||
|
||
def main(argv: list[str] | None = None) -> int:
|
||
ap = argparse.ArgumentParser(description="补空知识点")
|
||
ap.add_argument("--apply", action="store_true", help="真的写库(默认只看)")
|
||
ap.add_argument("--db", default=None, help="数据库路径(默认 data/app.db 或 $QINGLING_DB)")
|
||
args = ap.parse_args(argv)
|
||
|
||
path = Path(args.db) if args.db else db_path()
|
||
if not path.exists():
|
||
print(f"没有这个库:{path}")
|
||
return 1
|
||
conn = sqlite3.connect(str(path))
|
||
rows = conn.execute(
|
||
"SELECT id, subject, stem FROM questions WHERE kp IS NULL OR kp=''").fetchall()
|
||
todo = []
|
||
for qid, subject, stem in rows:
|
||
kp = kp_match.classify(subject, stem)
|
||
if kp:
|
||
todo.append((qid, kp, (stem or "").replace("\n", " ")[:48]))
|
||
print(f"空知识点 {len(rows)} 道,能认出 {len(todo)} 道:")
|
||
for _qid, kp, stem in todo:
|
||
print(f" {stem} -> {kp} {kp_match.name_of(kp)}")
|
||
if not args.apply:
|
||
print("\n(只看不写;加 --apply 才写)")
|
||
return 0
|
||
with conn:
|
||
for qid, kp, _stem in todo:
|
||
conn.execute("UPDATE questions SET kp=? WHERE id=?", (kp, qid))
|
||
left = conn.execute(
|
||
"SELECT COUNT(*) FROM questions WHERE kp IS NULL OR kp=''").fetchone()[0]
|
||
print(f"\n写好了 {len(todo)} 道,还剩 {left} 道空着(认不准,留空)")
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|