| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- # -*- coding: utf-8 -*-
- """回归测试:修复后的 v5 提取器 vs 目录内 26 份旧 JSON"""
- import os, sys, json, subprocess, tempfile
- SRC = r'E:/cfc/docs/参考资料'
- SCRIPTS = r'C:/Users/liaoxg.DESKTOP-DGLN79N/.workbuddy/skills/bun-report-parser/scripts'
- OUT = tempfile.mkdtemp(prefix='regress_')
- pdfs = sorted(f for f in os.listdir(SRC) if f.endswith('.pdf'))
- print(f'共 {len(pdfs)} 份 PDF,输出到 {OUT}')
- # 用调度器同款逻辑逐份跑 v5 主提取(输出到临时目录,不覆盖旧 JSON)
- import importlib
- sys.path.insert(0, SCRIPTS)
- import extract_full_report_v5 as v5
- from extract_relations import _norm_cn
- def deep_norm(o):
- """复刻调度器的 _deep_normalize(NFKC+部首归一),保证与旧 JSON 同口径比较"""
- if isinstance(o, str):
- return _norm_cn(o)
- if isinstance(o, list):
- return [deep_norm(x) for x in o]
- if isinstance(o, dict):
- return {_norm_cn(k): deep_norm(v) for k, v in o.items()}
- return o
- SECTIONS = ['疾病风险评估', '主要营养评估', '氨基酸评估', '维生素评估', '微量元素评估',
- '主要消化道致病菌', '抗生素风险评估', '肠道屏障及代谢物', '短链脂肪酸',
- '神经递质及激素', '个体化食物推荐表']
- def sig(rows):
- """行签名:排序后的 (名称,数值/评估值,状态/健康状况) 元组列表"""
- out = []
- for r in rows:
- if not isinstance(r, dict):
- continue
- name = r.get('名称', '')
- val = r.get('数值', r.get('评估值', ''))
- st = r.get('状态', r.get('健康状况', ''))
- out.append((str(name), str(val), str(st)))
- return sorted(out)
- total_diff = 0
- for pdf in pdfs:
- base = os.path.splitext(pdf)[0]
- old_path = os.path.join(SRC, base + '.json')
- if not os.path.exists(old_path):
- print(f'[跳过] {pdf} 无旧 JSON')
- continue
- old = json.load(open(old_path, encoding='utf-8'))
- # 新提取(直接调函数,避免写盘;过一遍调度器同款归一化)
- try:
- new = deep_norm(v5.extract_pdf_to_json(os.path.join(SRC, pdf)))
- except Exception as e:
- print(f'[失败] {pdf}: {e}')
- total_diff += 1
- continue
- diffs = []
- for sec in SECTIONS:
- o = sig(old.get(sec, []) if isinstance(old.get(sec), list) else
- (old.get(sec, {}).get('数据', []) if isinstance(old.get(sec), dict) else []))
- n = sig(new.get(sec, []) if isinstance(new.get(sec), list) else
- (new.get(sec, {}).get('数据', []) if isinstance(new.get(sec), dict) else []))
- if o != n:
- only_old = [x for x in o if x not in n]
- only_new = [x for x in n if x not in o]
- diffs.append((sec, len(o), len(n), only_old, only_new))
- if diffs:
- total_diff += 1
- print(f'[差异] {pdf}')
- for sec, no, nn, only_old, only_new in diffs:
- print(f' {sec}: 旧{no} → 新{nn}')
- for x in only_old[:5]:
- print(f' - 旧有: {x}')
- for x in only_new[:5]:
- print(f' + 新增: {x}')
- else:
- print(f'[一致] {pdf}')
- print()
- print(f'汇总:{len(pdfs)} 份中 {total_diff} 份有差异')
|