Files
cuotibiye/_tag_kp.py
T

165 lines
8.1 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.
"""把知识点编号(kp)打通:题库标注 → 表结构 → 入库 → 接口 → 解析查表。
分三步:
1. content.py 末尾给 72 道题标上 M/C 编号(显式列表,不改题干);
2. db.py 的 questions / mistakes 加 kp 列,老库用 _migrate 补;
3. services.py 的 4 处 INSERT 带上 kp,attempt 返回 kp,前端按 kp 查模板。
"""
import re
from pathlib import Path
ROOT = Path(__file__).resolve().parent
CONTENT = ROOT / "backend" / "content.py"
DB = ROOT / "backend" / "db.py"
SERVICES = ROOT / "backend" / "services.py"
# ── 72 道题的知识点编号(按 grade 顺序,对应 content.py 里的列表)────────────
MATH_KP = {
1: ["M003", "M004", "M003", "M004", "M007", "M004"],
2: ["M019", "M020", "M022", "M023", "M022", "M020"],
3: ["M037", "M038", "M039", "M040", "M051", "M027"],
4: ["M039", "M060", "M062", "M055", "", "M038"],
5: ["M077", "M078", "M082", "M081", "M096", "M064"],
6: ["M087", "M110", "M114", "M105", "M110", "M103"],
}
CHINESE_KP = {
1: ["C011", "C011", "C014", "C015", "C008", ""],
2: ["C030", "C027", "C032", "C030", "C038", "C030"],
3: ["C046", "C052", "C049", "C046", "", "C046"],
4: ["C070", "C070", "C075", "C073", "", "C038"],
5: ["C090", "C090", "C093", "C073", "C059", "C038"],
6: ["C108", "C108", "C111", "C073", "C113", "C119"],
}
BLOCK_HEAD = "\n# ---------------------------------------------------------------- 知识点编号\n"
def patch_content():
s = CONTENT.read_text(encoding="utf-8")
if "MATH_KP" in s:
print("content.py 已标注,跳过")
return
lines = [BLOCK_HEAD]
lines.append("# 每道题对应《知识点对照表》里的一个编号;空字符串表示这个概念还没进表,\n")
lines.append("# 解析时退回通用三步(宁可讲得浅,也不套错模板)。\n")
lines.append("MATH_KP = {\n")
for g, codes in MATH_KP.items():
lines.append(f" {g}: {codes!r},\n")
lines.append("}\nCHINESE_KP = {\n")
for g, codes in CHINESE_KP.items():
lines.append(f" {g}: {codes!r},\n")
lines.append("}\n\n")
lines.append("for _g, _codes in MATH_KP.items():\n")
lines.append(" for _q, _kp in zip(MATH[_g], _codes):\n")
lines.append(' _q["kp"] = _kp or None\n')
lines.append("for _g, _codes in CHINESE_KP.items():\n")
lines.append(" for _q, _kp in zip(CHINESE[_g], _codes):\n")
lines.append(' _q["kp"] = _kp or None\n')
s = s.rstrip("\n") + "\n" + "".join(lines)
CONTENT.write_text(s, encoding="utf-8", newline="\n")
print("content.py 已加 72 道题的 kp")
def patch_db():
s = DB.read_text(encoding="utf-8")
if '"kp"' in s:
print("db.py 已有 kp,跳过")
return
# 1) 建表语句加列
s = s.replace(''' options TEXT,
judge_type TEXT NOT NULL DEFAULT 'calc'
)''', ''' options TEXT,
kp TEXT,
judge_type TEXT NOT NULL DEFAULT 'calc'
)''', 1)
# 2) 迁移:老库补列
s = s.replace(''' want = {"questions": ["options"], "mistakes": ["options"]}''',
''' want = {"questions": ["options", "kp"], "mistakes": ["options", "kp"]}''', 1)
DB.write_text(s, encoding="utf-8", newline="\n")
print("db.py 已加 kp 列与迁移")
def patch_services():
s = SERVICES.read_text(encoding="utf-8")
n = 0
# ① create_paper
old = ''' "INSERT INTO questions (id, paper_id, child_id, subject, seq, stem, answer, options,"
" judge_type, created_at) VALUES (?,?,?,?,?,?,?,?,?,?)",
(new_id("q"), pid, cid, subject, i, q["stem"], q["answer"],
json.dumps(q.get("options"), ensure_ascii=False) if q.get("options") else None,
q.get("judge_type") or judging.infer_judge_type(q["stem"], subject), now()))'''
new = ''' "INSERT INTO questions (id, paper_id, child_id, subject, seq, stem, answer, options,"
" kp, judge_type, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,?)",
(new_id("q"), pid, cid, subject, i, q["stem"], q["answer"],
json.dumps(q.get("options"), ensure_ascii=False) if q.get("options") else None,
q.get("kp"),
q.get("judge_type") or judging.infer_judge_type(q["stem"], subject), now()))'''
assert old in s, "create_paper insert 未匹配"
s = s.replace(old, new, 1)
n += 1
# ② create_paper_from_image
old = ''' "INSERT INTO questions (id, paper_id, child_id, subject, seq, stem, answer, options,"
" judge_type, created_at) VALUES (?,?,?,?,?,?,?,?,?,?)",
(new_id("q"), pid, cid, q.get("subject") or subject, i, q["stem"], q.get("answer"),
json.dumps(q.get("options"), ensure_ascii=False) if q.get("options") else None,
q.get("judge_type") or "manual", now()))'''
new = ''' "INSERT INTO questions (id, paper_id, child_id, subject, seq, stem, answer, options,"
" kp, judge_type, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,?)",
(new_id("q"), pid, cid, q.get("subject") or subject, i, q["stem"], q.get("answer"),
json.dumps(q.get("options"), ensure_ascii=False) if q.get("options") else None,
q.get("kp"),
q.get("judge_type") or "manual", now()))'''
assert old in s, "create_paper_from_image insert 未匹配"
s = s.replace(old, new, 1)
n += 1
# ③ add_manual_mistake
old = ''' "INSERT INTO questions (id, paper_id, child_id, subject, seq, stem, answer, options,"
" judge_type, verdict, wrong_code, created_at) VALUES (?,?,?,?,1,?,?,?,?,'wrong',?,?)",
(qid, pid, cid, subject, stem, ans,
json.dumps(opts, ensure_ascii=False) if opts else None, jt, wrong_code, now()))'''
new = ''' "INSERT INTO questions (id, paper_id, child_id, subject, seq, stem, answer, options,"
" kp, judge_type, verdict, wrong_code, created_at)"
" VALUES (?,?,?,?,1,?,?,?,?,?,'wrong',?,?)",
(qid, pid, cid, subject, stem, ans,
json.dumps(opts, ensure_ascii=False) if opts else None, kp, jt, wrong_code, now()))'''
assert old in s, "add_manual_mistake insert 未匹配"
s = s.replace(old, new, 1)
n += 1
# ④ mistakes 入库
old = ''' "INSERT INTO mistakes (id, child_id, question_id, subject, stem, answer, options, judge_type,"
" wrong_count, wrong_code, status, stage, interval_days, next_due, created_at)"
" VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
(mid, cid, qid, q["subject"], q["stem"], q["answer"], q["options"], q["judge_type"],'''
new = ''' "INSERT INTO mistakes (id, child_id, question_id, subject, stem, answer, options, kp,"
" judge_type, wrong_count, wrong_code, status, stage, interval_days, next_due, created_at)"
" VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
(mid, cid, qid, q["subject"], q["stem"], q["answer"], q["options"], q["kp"], q["judge_type"],'''
assert old in s, "mistakes insert 未匹配"
s = s.replace(old, new, 1)
n += 1
# ⑤ 手输题允许带 kp
s = s.replace(''' options: list[str] | None = None, wrong_code: str = "E4",
source: str = "manual") -> dict:''',
''' options: list[str] | None = None, wrong_code: str = "E4",
source: str = "manual", kp: str | None = None) -> dict:''', 1)
# ⑥ attempt 把 kp 一起下发(前端据此查解析模板)
s = s.replace(''' "judge_type": mdict.get("judge_type"),''',
''' "judge_type": mdict.get("judge_type"),
"kp": mdict.get("kp"),''', 1)
SERVICES.write_text(s, encoding="utf-8", newline="\n")
print(f"services.py 已改 {n} 处 INSERT + attempt 返回 kp")
if __name__ == "__main__":
patch_content()
patch_db()
patch_services()