regress_test_v5.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. """回归测试:修复后的 v5 提取器 vs 目录内 26 份旧 JSON"""
  3. import os, sys, json, subprocess, tempfile
  4. SRC = r'E:/cfc/docs/参考资料'
  5. SCRIPTS = r'C:/Users/liaoxg.DESKTOP-DGLN79N/.workbuddy/skills/bun-report-parser/scripts'
  6. OUT = tempfile.mkdtemp(prefix='regress_')
  7. pdfs = sorted(f for f in os.listdir(SRC) if f.endswith('.pdf'))
  8. print(f'共 {len(pdfs)} 份 PDF,输出到 {OUT}')
  9. # 用调度器同款逻辑逐份跑 v5 主提取(输出到临时目录,不覆盖旧 JSON)
  10. import importlib
  11. sys.path.insert(0, SCRIPTS)
  12. import extract_full_report_v5 as v5
  13. from extract_relations import _norm_cn
  14. def deep_norm(o):
  15. """复刻调度器的 _deep_normalize(NFKC+部首归一),保证与旧 JSON 同口径比较"""
  16. if isinstance(o, str):
  17. return _norm_cn(o)
  18. if isinstance(o, list):
  19. return [deep_norm(x) for x in o]
  20. if isinstance(o, dict):
  21. return {_norm_cn(k): deep_norm(v) for k, v in o.items()}
  22. return o
  23. SECTIONS = ['疾病风险评估', '主要营养评估', '氨基酸评估', '维生素评估', '微量元素评估',
  24. '主要消化道致病菌', '抗生素风险评估', '肠道屏障及代谢物', '短链脂肪酸',
  25. '神经递质及激素', '个体化食物推荐表']
  26. def sig(rows):
  27. """行签名:排序后的 (名称,数值/评估值,状态/健康状况) 元组列表"""
  28. out = []
  29. for r in rows:
  30. if not isinstance(r, dict):
  31. continue
  32. name = r.get('名称', '')
  33. val = r.get('数值', r.get('评估值', ''))
  34. st = r.get('状态', r.get('健康状况', ''))
  35. out.append((str(name), str(val), str(st)))
  36. return sorted(out)
  37. total_diff = 0
  38. for pdf in pdfs:
  39. base = os.path.splitext(pdf)[0]
  40. old_path = os.path.join(SRC, base + '.json')
  41. if not os.path.exists(old_path):
  42. print(f'[跳过] {pdf} 无旧 JSON')
  43. continue
  44. old = json.load(open(old_path, encoding='utf-8'))
  45. # 新提取(直接调函数,避免写盘;过一遍调度器同款归一化)
  46. try:
  47. new = deep_norm(v5.extract_pdf_to_json(os.path.join(SRC, pdf)))
  48. except Exception as e:
  49. print(f'[失败] {pdf}: {e}')
  50. total_diff += 1
  51. continue
  52. diffs = []
  53. for sec in SECTIONS:
  54. o = sig(old.get(sec, []) if isinstance(old.get(sec), list) else
  55. (old.get(sec, {}).get('数据', []) if isinstance(old.get(sec), dict) else []))
  56. n = sig(new.get(sec, []) if isinstance(new.get(sec), list) else
  57. (new.get(sec, {}).get('数据', []) if isinstance(new.get(sec), dict) else []))
  58. if o != n:
  59. only_old = [x for x in o if x not in n]
  60. only_new = [x for x in n if x not in o]
  61. diffs.append((sec, len(o), len(n), only_old, only_new))
  62. if diffs:
  63. total_diff += 1
  64. print(f'[差异] {pdf}')
  65. for sec, no, nn, only_old, only_new in diffs:
  66. print(f' {sec}: 旧{no} → 新{nn}')
  67. for x in only_old[:5]:
  68. print(f' - 旧有: {x}')
  69. for x in only_new[:5]:
  70. print(f' + 新增: {x}')
  71. else:
  72. print(f'[一致] {pdf}')
  73. print()
  74. print(f'汇总:{len(pdfs)} 份中 {total_diff} 份有差异')