File size: 4,156 Bytes
e39a6b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
"""Checks sets/errhelp.jsonl against the pages (standard library + vision/vlm.py):
fields present, ids unique, pages exist, `now` well-formed, every `evidence` string is in the page text (HTML without
tags, entities decoded), every keyword spelling survives normalisation (a spelling that is all punctuation would be
skipped, one that normalises to a very short string would match almost anything), min_fix within range, the reference
answer scores 1 and "我不太确定。" scores 0, and the page has enough text. Cause/fix keywords are explanations, not
strings copied from the page, so they are not looked up in the page (the evidence strings are).

    python3 -I screen/errhelp/check.py
"""
import html
import os
import re
import sys
from collections import Counter

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import unit  # noqa: E402
from unit import vlm  # noqa: E402

FIELDS = ["id", "pages", "cat", "who", "now", "ask", "evidence", "cause", "fix", "min_fix", "never", "ref", "design"]
CATS = {"mac", "web", "office", "dev"}


def page_text(pid):
    h = open(os.path.join(HERE, "pages", pid + ".html"), encoding="utf-8").read()
    h = re.sub(r"<style.*?</style>|<title>.*?</title>", " ", h, flags=re.S)
    return html.unescape(re.sub(r"<[^>]+>", " ", h))


def main():
    rows = unit.load()
    bad = []
    ids = Counter(r.get("id") for r in rows)
    bad += [f"重复 id {i}" for i, n in ids.items() if n > 1]
    img = os.path.expanduser("~/.cache/luoke-bench/screen/img")
    missing_png = []
    lengths = []
    for r in rows:
        rid = r.get("id")
        for f in FIELDS:
            if f not in r:
                bad.append(f"{rid}: 缺字段 {f}")
        if r.get("cat") not in CATS:
            bad.append(f"{rid}: cat 不对 {r.get('cat')}")
        if not re.fullmatch(r"\d{4}-\d\d-\d\d \d\d:\d\d", r.get("now", "")):
            bad.append(f"{rid}: now 格式不对")
        text = ""
        for p in r.get("pages", []):
            if not os.path.exists(os.path.join(HERE, "pages", p + ".html")):
                bad.append(f"{rid}: 页面不存在 {p}")
                continue
            text += page_text(p)
            if not os.path.exists(os.path.join(img, p + ".png")):
                missing_png.append(p)
        flat = re.sub(r"\s+", "", text)
        lengths.append((len(flat), rid))
        for e in r.get("evidence", []):
            if re.sub(r"\s+", "", e) not in flat:
                bad.append(f"{rid}: 页面里找不到 evidence「{e}」")
        for key in ("cause", "fix", "never"):
            for spec in r.get(key, []):
                for a in vlm.alts(spec):
                    if a.startswith("re:"):
                        continue
                    n = vlm.norm(a)
                    if len(n) < 2:
                        bad.append(f"{rid}: {key} 的写法「{a}」归一化后太短({n!r})")
        if not r.get("cause"):
            bad.append(f"{rid}: cause 为空")
        if not (1 <= r.get("min_fix", 0) <= len(r.get("fix", []))):
            bad.append(f"{rid}: min_fix 超出 1..{len(r.get('fix', []))}")
        s = unit.score(r, r.get("ref", ""))
        if s["score"] != 1.0:
            bad.append(f"{rid}: 参考回答不满分 {s}")
        if unit.score(r, "我不太确定。")["score"] != 0.0:
            bad.append(f"{rid}: 乱答也得分")
    print(f"{len(rows)} 题;类别 {dict(Counter(r['cat'] for r in rows))};开发者 {sum(r['who'] == '开发者' for r in rows)} 题;"
          f"never 条数 {sum(len(r['never']) for r in rows)}({sum(not r['never'] for r in rows)} 题没有)")
    lengths.sort()
    print("页面文字(去标签、含被挡住的)最少:", ", ".join(f"{rid} {n}" for n, rid in lengths[:5]), f";中位 {lengths[len(lengths) // 2][0]}")
    if missing_png:
        print(f"还没渲染的 PNG {len(missing_png)} 张(先跑 render.py):", " ".join(missing_png[:10]))
    for b in bad:
        print("  ✗", b)
    print("OK" if not bad else f"{len(bad)} 处问题")
    return not bad


if __name__ == "__main__":
    sys.exit(0 if main() else 1)