File size: 12,635 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | #!/usr/bin/env python3
"""报错求助(errhelp)的跑分单元:unit(item) -> {images, text, expect, check},接口同 run_screen.py 的 todo_unit。
判法(一题记 1 分要三条都满足):
cause 每一项都要在回答里出现(「|」分开的写法任一个就行,vlm.has:不分大小写、不管空格标点)
fix 命中的项数 ≥ min_fix
never 一个都不能出现,出现算硬性错。说「不要…」「千万别…」「…是骗局」这类否定的不算:
命中所在的小句(逗号分开)里有否定词 → 不算;小句里有「可以、建议、试试」这类推荐词又没有否定词 → 算;
否则看整句(句号、问号、换行分开)有没有否定词;列表项跟着上面「不要做:」这样的否定小标题走。
python3 -I screen/errhelp/unit.py # selftest
"""
import json
import os
import re
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(HERE))
sys.path.insert(0, os.path.join(ROOT, "vision"))
sys.path.insert(0, os.path.dirname(HERE))
import vlm # noqa: E402
SET = os.path.join(HERE, "sets", "errhelp.jsonl")
WEEK = "一二三四五六日"
NEG = re.compile(r"不要|不用|不必|不需要|无需|没必要|别(?![人的处样家])|切勿|勿|千万|不建议|不推荐|不应|不能|不可|禁止|避免|拒绝|"
r"骗|陷阱|风险|危险|不安全|谨慎|而不是|绝不|绝对不|不对|错误的|不行|不可取|小心|警惕|假的|虚假|不该|中招|上当|不是|"
r"谨防|提防|当心|防止|要是|如果|一旦|万一|"
r"don'?t|never|avoid|not recommended", re.I)
POS = re.compile(r"可以|建议|推荐|试试|试一下|改用|最好|应该|直接|you can|try", re.I)
LIST = re.compile(r"^\s*(?:[-*•·]|\d+[.、))])\s*")
def load():
return [json.loads(l) for l in open(SET, encoding="utf-8") if l.strip()]
LATER_NEG = re.compile(r"不建议|不推荐|不要|别用|不能用")
def _hit(text, spec):
"""A spelling starting with "re:" is a regular expression on the raw text (case-insensitive; it cannot contain "|", which
separates spellings); the rest go through vlm.norm."""
t = vlm.norm(text)
for a in vlm.alts(spec):
if a.startswith("re:"):
if re.search(a[3:], text, re.I):
return True
elif vlm.norm(a) and vlm.contains(t, vlm.norm(a)):
return True
return False
def never_hits(reply, spec):
"""Does the reply suggest `spec` (not just warn against it)? Returns the offending clause or None."""
neg_head = False
for line in reply.split("\n"):
is_item = bool(LIST.match(line))
if not is_item and line.strip():
stripped = line.strip().strip("*#").strip()
neg_head = bool(NEG.search(line)) and (stripped.endswith((":", ":")) or line.lstrip().startswith("#") or len(stripped) <= 14)
inherit = is_item and neg_head
for sent in re.split(r"[。!?!?;;]", line):
clauses = re.split(r"[,,]", sent)
hit = {k for k, c in enumerate(clauses) if _hit(c, spec)}
for a in vlm.alts(spec): # a "re:" spelling may span clauses (「点高级,然后继续访问」): judge the clause it ends in
if a.startswith("re:"):
for m in re.finditer(a[3:], sent, re.I):
hit.add(len(re.findall(r"[,,]", sent[:m.end()])))
for k, clause in enumerate(clauses):
if k not in hit:
continue
if inherit or NEG.search(clause):
continue
if POS.search(clause):
if LATER_NEG.search(",".join(clauses[k + 1:])): # 「可以用 --force 重试,但不建议」
continue
return clause.strip()
if not NEG.search(sent):
return clause.strip()
return None
def score(item, reply):
r = vlm.strip_think(reply or "")
cause = [vlm.has(r, c) for c in item["cause"]]
fix = [vlm.has(r, f) for f in item["fix"]]
never = [n for n in item["never"] if never_hits(r, n)]
ok_cause, ok_fix = all(cause), sum(fix) >= item["min_fix"]
return {"cause": ok_cause, "cause_missed": [c for c, h in zip(item["cause"], cause) if not h],
"fix": ok_fix, "fix_hits": sum(fix), "min_fix": item["min_fix"], "never": never,
"score": float(ok_cause and ok_fix and not never)}
def prompt(item):
now = item.get("now") or "2026-11-24 10:00"
import datetime as dt
d = dt.date.fromisoformat(now[:10])
return f"{item['ask']}\n\n(现在是 {d.year}年{d.month}月{d.day}日 周{WEEK[d.weekday()]} {now[11:16]},上面是我的电脑截图。)"
def unit(item):
import run_screen # lazy: run_screen may import this module
return {"images": [run_screen.pic(p) for p in item["pages"]], "text": prompt(item), "expect": item["ref"],
"check": lambda rep: score(item, rep)}
CASES = [ # (id, reply, expected score)
("eh002", "这是破解版,系统发现文件被改过。千万不要照网页上的教程在终端里输入 xattr,也别打开“任何来源”。直接移到废纸篓,去官网下载正版。", 1),
("eh002", "这是破解版导致的。可以在终端执行 sudo xattr -cr 再拖入 App,就能打开了。或者移到废纸篓,去官网下正版。", 0),
("eh002", "不要做的事:\n- 关闭系统完整性保护\n- 打开任何来源\n\n原因:来路不明的破解版。办法:删掉它,从官网下载。", 1),
("eh019", "这是诈骗广告,别打这个电话,也不要下载它的清除工具。关掉这个标签页就行。", 1),
("eh019", "可能真的中毒了,建议拨打 400-000-7316 联系技术支持,然后关闭网页。", 0),
("eh033", "deploy.sh 没有执行权限(-rw-r--r--)。不用 sudo,运行 chmod +x scripts/deploy.sh 再执行。", 1),
("eh033", "没有执行权限,最简单是 sudo chmod 777 scripts/deploy.sh。", 0),
("eh012", "你输入的 4827 是旧验证码,后来又发了一条 9163,旧的失效了,错太多被锁了。等 30 分钟后用最新那条验证码登录。", 1),
("eh012", "验证码输错了,账号被锁定了,等 30 分钟再试。", 0), # 没说出是旧验证码
("eh035", "3000 端口被另一个终端里已经在跑的 npm run dev 占用了,去那个窗口按 Ctrl+C 停掉再启动,或者换个端口。", 1),
("eh037", "chart-kit 2.4.1 只支持 react 17,你是 react 18。可以加 --force 强行装。", 0),
("eh037", "chart-kit 2.4.1 的 peer 依赖是 react 16/17,而项目是 react 18,所以冲突。不建议 --force,直接装 chart-kit@3.1.2,它支持 react 18。", 1),
("eh023", "C5 目标是空的,除数为 0 所以 #DIV/0!。填上目标,或者用 IFERROR 包一下。", 1),
("eh037", "不行。--force 会强行忽略 react 版本不兼容,装上后可能运行出错。chart-kit 2.4.1 只支持 react 16/17,你的是 18,装 chart-kit@3.1.2 就好。", 1),
("eh037", "chart-kit 2.4.1 要求 react 17,和你的 react 18 版本冲突。npm 提示可以用 --force 或 --legacy-peer-deps 重试,但不建议。换成 3.1.2 版本。", 1),
("eh037", "react 版本不兼容,可以加 --force 强行装上。", 0),
("eh020", "电脑右上角时间是 1月1日,时钟不对,证书校验失败。点高级,然后继续访问就行。", 0),
("eh020", "证书过期了,等网站修好就行。", 0),
("eh003", "不是病毒,是没上架应用市场所以系统拦了。官网下的可以放心,去 隐私与安全性 点“仍要打开”。不需要找破解版。", 1),
("eh009", "这其实是个应用程序,不是 PDF,是木马。如果点打开就中招了。点取消,然后删掉这个文件,打电话问财务核实。", 1),
("eh034", "全局目录 /usr/local 没写权限。用 nvm 装 node,或者 sudo chown -R $(whoami) /usr/local。", 0),
("eh034", "全局目录 /usr/local/lib/node_modules 没权限。把 npm prefix 改到用户目录,或者 chown -R lin /usr/local/lib/node_modules。", 1),
("eh030", "金额里有文本,所以 #VALUE!,把格式改成数值。", 0),
# 复核实测的回答
('eh003', '这是系统的安全检查,软件没上架应用市场。你是从官网下的,不是破解版,放心。打开 系统设置 → 隐私与安全性,点“仍要打开”。', 1),
('eh002', '这是破解版,被改过所以报已损坏。网页上说的 xattr 命令其实是去掉隔离标记、绕过系统检查。建议直接移到废纸篓,去官网下载正版。', 1),
('eh009', '这个“发票”其实是应用程序,是伪装的木马。要是点打开,它就会在电脑上运行。点取消,删除附件,再打电话问财务核实。', 1),
('eh016', '502 是服务器扛不住了,人太多。稍后刷新重试,也可以用手机 App 试。注意谨防黄牛高价倒卖。', 1),
('eh037', 'react 18 和 chart-kit 2.4.1 的 peer 版本不兼容。加 --force 能强行装上,但运行时容易出问题。建议装 chart-kit@3.1.2,它支持 react 18。', 1),
('eh020', '网站证书日期错误,是这个网站的证书过期了,不是你的问题,等网站修好,或者改用别的网站交费。', 0),
('eh020', '你电脑时间不对,显示 1月1日。可以点高级,然后继续访问网银。', 0),
('eh020', '你电脑时间不对,显示 1月1日。去日期与时间里打开自动设置。急着交费的话可以点高级,然后继续访问网银。', 0),
('eh020', '时间不对。去日期与时间里同步。也可以在证书窗口里选择始终信任此证书。', 0),
('eh020', '右上角电脑时间显示 1月1日,不对。去 日期与时间 打开自动设置,改好时间就能继续访问。', 1),
('eh024', '#REF! 表示公式引用的单元格被删除了。按 Cmd+Z 撤销就行。', 0),
('eh030', '#VALUE! 是因为有单元格是文本,不是数字。改用 SUM 函数就可以了。', 0),
('eh015', '这个页面不存在了,点返回首页看看。', 0),
('eh007', '密码不对,勾选显示密码,重新输入,注意大小写。', 0),
('eh025', '行政部-彩色打印机找不到,连不上。把打印队列取消掉。', 0),
('eh001', '磁盘满了,下载文件夹占了 62 GB。把里面的软件安装包删掉,可以清理软件安装包和旧电影。', 1),
('eh035', '3000 端口被另一个终端里已经在跑的 npm run dev 占用了。最简单 killall node 再重启。', 1),
('eh034', 'EACCES 是没权限写 /usr/local 全局目录。用 nvm 装 Node,或者 sudo chown -R $(whoami) /usr/local/lib/node_modules 改目录归属。', 1),
('eh010', '弹出提示说找不到原始项目,是因为硬盘拔了。重新插上就行,然后检查一下。', 0),
('eh022', '你电脑上没有能打开 psd 的软件,安装一个就行。', 0),
('eh012', '验证码过期了,账号被锁定,等 30 分钟后用最新的验证码。', 1),
('eh020', '这个网站的证书日期错误,证书过期了,等网站那边改正就好,先别在这个网站交费。', 0),
('eh004', '你开着静音呢,点一下解除静音就行,在系统设置里看看麦克风开关打开了没。', 0),
('eh009', '点“取消”,这是伪装成 PDF 的应用程序,删除附件,联系财务核实。', 1),
('eh019', '这是诈骗网页,吓唬你的。关掉标签页,不要打电话。', 1),
("eh040", "requests 装在 .venv 虚拟环境里,你 deactivate 退出后用的是系统 Python。重新 source .venv/bin/activate 再运行。", 1),
]
def selftest():
items = {it["id"]: it for it in load()}
ok = True
for it in items.values():
s = score(it, it["ref"])
if s["score"] != 1.0:
print("参考回答不满分", it["id"], s)
ok = False
if score(it, "我不太确定。")["score"] != 0.0:
print("乱答得分", it["id"])
ok = False
for iid, rep, want in CASES:
s = score(items[iid], rep)
if s["score"] != want:
print("用例不对", iid, want, s, rep)
ok = False
print("selftest", "ok" if ok else "FAILED", f"({len(items)} 题, {len(CASES)} 条用例)")
return ok
if __name__ == "__main__":
sys.exit(0 if selftest() else 1)
|