#!/usr/bin/env python3 """换样子(small / dark / english)的打分接口:unit(item) -> {images, text, expect, check};selftest() -> bool。 python3 -I screen/variants/unit.py # 跑 selftest 变体题就是原题换了图:按原题所属题库的判法(run_screen 的 webqa_unit / todo_unit / profile_unit), 题表里的 page / pages 已经换成变体图的名字。english 的 answers 已换成英文写法(中文原答案也在里面)。 """ import json import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.dirname(HERE)) import run_screen # noqa: E402 SETS = ("small", "dark", "english") def load(name): return [json.loads(l) for l in open(os.path.join(HERE, "sets", name + ".jsonl"), encoding="utf-8") if l.strip()] def unit(item): if item["part"] == "webqa": return run_screen.webqa_unit(item["set"], item) if item["part"] == "todo": return run_screen.todo_unit(item) if item["part"] == "profile": return run_screen.profile_unit(item) raise KeyError(item["part"]) def selftest(): ok = True def bad(msg): nonlocal ok ok = False print("FAIL", msg) n = 0 for name in SETS: rows = load(name) for it in rows: u = unit(it) if run_screen.as_score(u["check"](u["expect"])) < 0.999: bad(f"{it['id']} 参考答案当回答不满分") free = it.get("type") == "none" or (it["part"] == "todo" and not it["should_add"]) or \ (it["part"] == "profile" and not it["keep"]) if not free and run_screen.as_score(u["check"]("我不太确定。")) > 0.05: bad(f"{it['id']} 乱答得分") if not all(os.path.exists(p) for p in u["images"]): bad(f"{it['id']} 没有变体图 {u['images']}") if any(not os.path.basename(p).split(".")[0].endswith(("_s50", "_dark", "_en")) for p in u["images"]): bad(f"{it['id']} 用的不是变体图") n += 1 print(f"{name}: {len(rows)} 题") # english: 英文写法和中文原写法都算对;数字题不变 en = {it["src"]: it for it in load("english")} chk = lambda sid, rep: unit(en[sid])["check"](rep) # noqa: E731 for sid, rep, want in [("wq040", "Shangu Mushroom Broth Hotpot", 1.0), ("wq040", "山谷菌菇汤锅", 1.0), ("wq040", "Shuweiyuan Hotpot", 0.0), ("wq053", "Desktop PC", 1.0), ("wq052", "13.6 GB", 1.0), ("wq004", "不能,夜场 20:30 停止入场", 1.0), ("wq046", "UI Designer", 0.0), ("wq016", "42 hours", 1.0)]: if chk(sid, rep) != want: bad(f"english {sid} 答「{rep}」应得 {want}") print("selftest", "ok" if ok else "FAILED", f"({n} items)") return ok if __name__ == "__main__": sys.exit(0 if selftest() else 1)