Datasets:
File size: 7,507 Bytes
3463967 | 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 | #!/usr/bin/env python3
"""
validate.py — 校验子代理产出的 v0.1 speech 数据是否符合 SPEC.md。
用法: python3 validate.py <output.jsonl> [--input <shard_input.json>]
"""
import argparse
import json
import re
import sys
from collections import Counter
META_WORDS = ["原版", "原本的", "打破原", "与原", "相比之前", "相较于原", "不同于原", "改为", "原有的"]
TYPES = {"pos", "neg_change", "neg_swap"}
MOD_TYPES = set("ABCDEF")
def norm(s):
return re.sub(r"[\s,。、,.;;::!!??\"“”'‘’()()]", "", s or "")
def main():
ap = argparse.ArgumentParser()
ap.add_argument("output")
ap.add_argument("--input")
ap.add_argument("--qmin", type=int, default=2)
ap.add_argument("--qmax", type=int, default=8)
args = ap.parse_args()
errors, warns = [], []
recs = []
with open(args.output, encoding="utf-8") as f:
for ln, line in enumerate(f, 1):
if not line.strip():
continue
try:
recs.append((ln, json.loads(line)))
except json.JSONDecodeError as e:
errors.append("行%d: JSON 解析失败 %s" % (ln, e))
inp = None
if args.input:
inp = json.load(open(args.input, encoding="utf-8"))
mod_types, mod_dims, n_mods = Counter(), Counter(), Counter()
seen = Counter()
for ln, r in recs:
tag = "行%d[%s %s]" % (ln, r.get("type"), str(r.get("uuid"))[-12:])
t = r.get("type")
if t not in TYPES:
errors.append("%s: type 非法" % tag)
continue
seen[(r.get("uuid"), t)] += 1
for k in ("uuid", "audio_path", "caption_ori", "caption", "questions", "answers"):
if not r.get(k):
errors.append("%s: 缺少字段 %s" % (tag, k))
qs, ans = r.get("questions") or [], r.get("answers") or []
if len(qs) != len(ans):
errors.append("%s: questions(%d) 与 answers(%d) 数量不一致" % (tag, len(qs), len(ans)))
if [a.get("id") for a in ans] != list(range(len(ans))):
errors.append("%s: answers id 不是 0..n-1" % tag)
if not args.qmin <= len(qs) <= args.qmax:
warns.append("%s: 题数 %d 不在 %d~%d" % (tag, len(qs), args.qmin, args.qmax))
cap_n = norm(r.get("caption"))
for i, q in enumerate(qs):
for k in ("维度", "题目", "依据"):
if not q.get(k):
errors.append("%s: 第%d题缺少 %s" % (tag, i, k))
if q.get("依据") and norm(q["依据"]) not in cap_n:
errors.append("%s: 第%d题依据不是 caption 子串: %s" % (tag, i, q["依据"][:30]))
dims = [q.get("维度") for q in qs]
dup = [d for d, c in Counter(dims).items() if c > 1]
if dup:
warns.append("%s: 维度重复 %s(确认是否为可单独成题的动态/局部特征)" % (tag, dup))
if len(set(q.get("题目") for q in qs)) < len(qs):
errors.append("%s: 有重复题目" % tag)
for a in ans:
if a.get("answer") not in ("yes", "no"):
errors.append("%s: answer 非 yes/no" % tag)
if not a.get("evidence"):
errors.append("%s: 第%s题缺少 evidence" % (tag, a.get("id")))
n_no = sum(a.get("answer") == "no" for a in ans)
if any(w in (r.get("caption") or "") for w in META_WORDS) and t == "neg_change":
errors.append("%s: caption 含元语言" % tag)
if t == "pos":
if r.get("caption") != r.get("caption_ori"):
errors.append("%s: 正样本 caption 必须与 caption_ori 一致" % tag)
if n_no:
errors.append("%s: 正样本存在 no" % tag)
elif t == "neg_change":
mods = r.get("modifications") or []
if not 1 <= len(mods) <= 2:
errors.append("%s: modifications 数量 %d 不在 1~2" % (tag, len(mods)))
n_mods[len(mods)] += 1
for m in mods:
if m.get("类型") not in MOD_TYPES:
errors.append("%s: 改动类型非法 %s" % (tag, m.get("类型")))
mod_types[m.get("类型")] += 1
mod_dims[m.get("维度")] += 1
if m.get("新描述") and norm(m["新描述"]) not in cap_n:
warns.append("%s: 新描述不是 caption 子串" % tag)
if not 1 <= n_no <= 2:
errors.append("%s: neg_change 的 no 数量为 %d,要求 1~2" % (tag, n_no))
if r.get("caption") == r.get("caption_ori"):
errors.append("%s: neg_change caption 未改动" % tag)
elif t == "neg_swap":
if not r.get("swap_uuid"):
errors.append("%s: 缺少 swap_uuid" % tag)
if n_no == 0:
warns.append("%s: 互换负样本没有 no" % tag)
elif n_no < len(ans) / 2:
warns.append("%s: 互换负样本 no 只有 %d/%d" % (tag, n_no, len(ans)))
if inp:
idx = {it["uuid"]: it for it in inp["change_items"]}
sidx = {it["uuid"]: it for it in inp["swap_items"]}
for u, it in idx.items():
for t, want in (("pos", 1), ("neg_change", it.get("n_neg", 1))):
if seen[(u, t)] != want:
errors.append("数量不对: %s %s 应为 %d 条,实际 %d 条" % (t, u, want, seen[(u, t)]))
# 同一音频的两条 neg_change 必须改不同的地方
by_u = {}
for ln, r in recs:
if r.get("type") == "neg_change":
by_u.setdefault(r.get("uuid"), []).append(r)
for u, rs in by_u.items():
if len(rs) == 2:
if rs[0].get("caption") == rs[1].get("caption"):
errors.append("同一音频的两条 neg_change caption 相同: %s" % u)
d0 = {m.get("维度") for m in rs[0].get("modifications") or []}
d1 = {m.get("维度") for m in rs[1].get("modifications") or []}
if d0 & d1:
warns.append("同一音频的两条 neg_change 改了相同维度 %s: %s" % (d0 & d1, u))
for u in sidx:
if seen[(u, "neg_swap")] != 1:
errors.append("缺失或重复: neg_swap %s(出现 %d 次)" % (u, seen[(u, "neg_swap")]))
for ln, r in recs:
src = idx.get(r.get("uuid")) if r.get("type") != "neg_swap" else sidx.get(r.get("uuid"))
if not src:
continue
if r.get("caption_ori") != src["caption"] or r.get("audio_path") != src["audio_path"]:
errors.append("行%d: caption_ori/audio_path 与输入不一致" % ln)
if r.get("type") == "neg_swap" and (r.get("caption") != src["swap_caption"] or r.get("swap_uuid") != src["swap_uuid"]):
errors.append("行%d: neg_swap caption/swap_uuid 必须与输入的 swap_caption/swap_uuid 一致" % ln)
for m in warns:
print("WARN ", m)
for m in errors:
print("ERROR", m)
tc = Counter(r.get("type") for _, r in recs)
print("\n样本数: %s" % dict(tc))
if n_mods:
print("neg_change 改动数分布: %s" % dict(n_mods))
print("改动类型分布: %s" % dict(mod_types))
print("改动维度分布: %s" % dict(mod_dims.most_common()))
print("ERROR %d, WARN %d" % (len(errors), len(warns)))
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()
|