File size: 2,532 Bytes
ab1e9bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""在公开测试题上测工具库。用法: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()