stem-md-corpus / scripts /validate_md.py
孟祥瑞
上传语料:stem-md-corpus
eadaa58 verified
Raw History Blame Contribute Delete
3.39 kB
#!/usr/bin/env python3
"""validate_md.py -- 校验 out/md/ 产物质量(community.md §8 待办第 2 条)。
检查项:
A. md 与 review.json 成对存在;
B. md 正文 >= 300 字符;
C. 无三级标题(### );
D. 二级标题(## )<= 4;
E. review.json 可解析且 issue type 在 7 种白名单内;
F. md 首行非空(应为中文标题)。
区分新旧产物:review 含 record_match 字段视为新 schema,否则为子 agent 时代旧产物。
只报告,不删改任何文件。
"""
from __future__ import annotations
import glob
import json
import os
import re
import sys
MD_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "out", "md")
ALLOWED_TYPES = {
"metadata_identity_mismatch", "ocr_text_error", "formula_symbol_loss",
"table_structure_damage", "reading_order_damage", "text_truncated_or_missing",
"other_extraction_error",
}
def main() -> int:
mds = {os.path.basename(p)[:-3] for p in glob.glob(os.path.join(MD_DIR, "*.md"))}
revs = {os.path.basename(p)[:-len(".review.json")]
for p in glob.glob(os.path.join(MD_DIR, "*.review.json"))}
problems: dict[str, list[str]] = {}
old_schema = set()
def flag(sha: str, msg: str) -> None:
problems.setdefault(sha, []).append(msg)
for sha in sorted(mds - revs):
flag(sha, "缺 review.json")
for sha in sorted(revs - mds):
flag(sha, "缺 md(review 孤儿)")
for sha in sorted(mds & revs):
p_md = os.path.join(MD_DIR, sha + ".md")
p_rev = os.path.join(MD_DIR, sha + ".review.json")
try:
text = open(p_md, encoding="utf-8").read()
except Exception as e:
flag(sha, "md 读取失败: %s" % e)
continue
body = text.strip()
if len(body) < 300:
flag(sha, "正文过短(%d 字符)" % len(body))
h3 = re.findall(r"^###\s", text, re.M)
if h3:
flag(sha, "含 %d 个三级标题" % len(h3))
h2 = re.findall(r"^##\s", text, re.M)
if len(h2) > 4:
flag(sha, "二级标题 %d 个(>4)" % len(h2))
first = text.splitlines()[0].strip() if text.splitlines() else ""
if not first:
flag(sha, "首行为空(应为主题)")
try:
rev = json.load(open(p_rev, encoding="utf-8"))
except Exception as e:
flag(sha, "review.json 解析失败: %s" % e)
continue
if not isinstance(rev, dict):
flag(sha, "review.json 非 dict")
continue
if "record_match" in rev:
for iss in rev.get("extraction_issues") or []:
t = iss.get("type") if isinstance(iss, dict) else None
if t not in ALLOWED_TYPES:
flag(sha, "issue type 越界: %r" % t)
else:
old_schema.add(sha)
print("md 总数: %d | review 总数: %d | 旧 schema(子agent时代): %d"
% (len(mds), len(revs), len(old_schema)))
if problems:
print("问题文件 %d 个:" % len(problems))
for sha, msgs in sorted(problems.items()):
print(" %s: %s" % (sha[:12], "; ".join(msgs)))
else:
print("全部通过。")
return 0
if __name__ == "__main__":
sys.exit(main())