Files
cuotibiye/gen_kpt.py
T

86 lines
3.4 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.
"""把《解析模板表》灌进工作台:生成 const KPT,并让解析先查编号。
为什么要内联而不是外链一个 js:工作台是单文件,既能被 /workbench 直接返回,
也能双击离线打开;多一个文件就多一条会断的路径(之前 brand/mark.svg 就断过一次)。
"""
import json
import re
from pathlib import Path
import openpyxl
ROOT = Path(__file__).resolve().parent
XLSX = ROOT / "report" / "错题毕业-小学-解析模板表.xlsx"
HTML = ROOT / "prototype" / "workbench" / "index.html"
KPT_JSON = ROOT / "prototype" / "workbench" / "kpt.json"
BEGIN = "/* <KPT> */"
END = "/* </KPT> */"
def load_templates():
wb = openpyxl.load_workbook(XLSX)
ws = wb["解析模板表"]
out = {}
for r in ws.iter_rows(min_row=2, values_only=True):
if not r or not r[0]:
continue
code, _subject, _grade, name, _example, _judge, _v1, _how, s1, s2, s3, know, traps, _model = r[:14]
out[code] = {"n": name, "s": [s1, s2, s3], "k": know, "t": traps}
return out
def js_block(templates):
payload = json.dumps(templates, ensure_ascii=False, separators=(",", ":"))
return (
BEGIN + "\n"
"// 自动生成,别手改:改 report/build_analysis_templates.py 后重跑 gen_kpt.py\n"
f"const KPT = {payload};\n"
+ END
)
def main():
templates = load_templates()
html = HTML.read_text(encoding="utf-8")
block = js_block(templates)
if BEGIN in html and END in html:
start = html.index(BEGIN)
stop = html.index(END) + len(END)
html = html[:start] + block + html[stop:]
else:
anchor = "function buildAnalysis(q) {"
idx = html.index(anchor)
html = html[:idx] + block + "\n\n" + html[idx:]
# 解析优先级:算式按实际数字生成 → 语文按题型 → 知识点模板 → 通用
old = ''' // 语文先按题型走:写词 / 选字 / 近反义 / 标点 / 读音
const plan = (q.subject === "chinese" ? zhPlan(q) : null) || (q.judge_type === "calc" ? calcPlan(q) : null);'''
new = ''' // 第一优先:算式题按「这一道题的实际数字」生成(模板里的是示例数字,会和孩子手上这道对不上)
// 第二优先:语文按题型(写词/选字/近反义/标点/读音)
// 第三优先:知识点模板(成语、修辞、量词、句式转换这些没有通用规则可用)
let plan = q.subject === "chinese" ? zhPlan(q) : null;
if (!plan && q.judge_type === "calc") plan = calcPlan(q);
if (!plan && q.kp && KPT[q.kp]) {
const t = KPT[q.kp];
plan = { know: t.k, idea: t.n, steps: t.s, traps: t.t };
}'''
if old in html:
html = html.replace(old, new, 1)
elif "if (!plan && q.kp && KPT[q.kp])" not in html:
raise SystemExit("buildAnalysis 的优先级块没找到,无法确定插哪里")
HTML.write_text(html, encoding="utf-8", newline="\n")
# 同一份数据再落一个 json:inline 是为了 file:// 双击能跑,
# json 是为了评审时能 diff(147 条塞在 187KB 的 HTML 里没法看)。
import json as _json
KPT_JSON.write_text(_json.dumps(templates, ensure_ascii=False, indent=1, sort_keys=True),
encoding="utf-8", newline="\n")
print(f"已写入 {len(templates)} 条模板;index.html {len(html)} 字节;kpt.json {KPT_JSON.stat().st_size} 字节")
if __name__ == "__main__":
main()