"""在公开测试题上测工具库。用法:python benchmark/run.py(只查表和错别字规则,不用向量模型) 报每种题:题数;给代码的比例;给代码时的正确率;排第一的候选(best)的正确率。 答案是 ABSTAIN 的题报:错给代码的比例(越低越好)。 """ import csv, json, os, sys, collections HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.dirname(HERE)) from geoalign import Gazetteer # noqa: E402 def main(): g = Gazetteer() Q = list(csv.DictReader(open(os.path.join(HERE, "questions.csv"), encoding="utf-8"))) S = collections.defaultdict(collections.Counter) wrong = [] for q in Q: m = g.align(q["text"], parent=q["parent"] or None, year=int(q["year"]) if q["year"] else None) s = S[q["kind"] + ("(不应给代码)" if q["expect"] == "ABSTAIN" else "")] s["n"] += 1 if q["expect"] == "ABSTAIN": s["wrong_code"] += m.code is not None if m.code is not None: wrong.append((q["id"], q["kind"], q["text"], q["year"], q["expect"], m.code, m.method)) else: s["coded"] += m.code is not None s["coded_right"] += m.code == q["expect"] s["best_right"] += (m.code or m.best) == q["expect"] if m.code is not None and m.code != q["expect"]: wrong.append((q["id"], q["kind"], q["text"], q["year"], q["expect"], m.code, m.method)) rows = [] tot = collections.Counter() for k in sorted(S): s = S[k] if "wrong_code" in s or "(不应给代码)" in k: rows.append((k, s["n"], "", "", "", f"{s['wrong_code'] / s['n']:.1%}")) else: tot.update(s) rows.append((k, s["n"], f"{s['coded'] / s['n']:.1%}", f"{s['coded_right'] / max(s['coded'], 1):.1%}", f"{s['best_right'] / s['n']:.1%}", "")) rows.append(("有答案的题合计", tot["n"], f"{tot['coded'] / tot['n']:.1%}", f"{tot['coded_right'] / max(tot['coded'], 1):.1%}", f"{tot['best_right'] / tot['n']:.1%}", "")) print("| 题的种类 | 题数 | 给代码的比例 | 给代码时的正确率 | 排第一的候选正确率 | 错给代码的比例 |") print("|---|---|---|---|---|---|") for r in rows: print("| " + " | ".join(str(x) for x in r) + " |") print(f"\n给错代码的题 {len(wrong)} 道:") for w in wrong: print(" ", *w) if __name__ == "__main__": main()