parse_aidefansi_report.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. #!/usr/bin/env python3
  2. """艾德范思(Advanced Bios)肠道微生态检测报告 PDF 采集解析器。
  3. 将 PDF 解析为与既有 per-report JSON(如 501999942-某人.json)一致的结构化格式,
  4. 供后续同厂商真实报告批量复用。
  5. 依赖:PyMuPDF (fitz)。用法:
  6. python parse_aidefansi_report.py <report.pdf> [-o output.json]
  7. 输出结构(JSON):
  8. 文件名 / 格式(aidefansi) / 总页数 / 报告机构 / 个人信息 / 报告概述 /
  9. 各整体健康评估分表(名称-数值-状态三元组)/
  10. 有益菌检出 / 条件致病菌检出 / 致病菌检出 /
  11. 分项检测参考 / 肠道菌群组成(门/属/特征菌) /
  12. 指标详解(含相关介绍/食物来源/注意事项) /
  13. 过敏相关菌 / 疾病相关菌 / 疾病介绍与调理建议 / 报告声明
  14. 解析策略:按词坐标重建表格。表格区域 = [表头行 y, 下一个 ※/【/注:/检测者 标记 y),
  15. 行锚点 = 最右列状态词,行带 = 相邻锚点中点;无表头的续页按"当前章节"归属,
  16. 不依赖纯文本顺序,可跨页累积同一章节。
  17. """
  18. import json
  19. import re
  20. import sys
  21. from pathlib import Path
  22. try:
  23. import fitz
  24. except ImportError:
  25. sys.exit("需要 PyMuPDF: pip install pymupdf")
  26. # ---------------------------------------------------------------- 基础工具
  27. def page_words(page):
  28. """返回按 (y, x) 排序的词列表 [(x, y, text), ...]。"""
  29. words = [(w[0], w[1], w[4]) for w in page.get_text("words")]
  30. words.sort(key=lambda w: (round(w[1], 1), w[0]))
  31. return words
  32. def cluster_rows(words, tol=9.0):
  33. """按 y 坐标聚类成行,返回 [[(x,y,text),...], ...](行内按 x 排序)。"""
  34. rows = []
  35. for w in words:
  36. if rows and abs(w[1] - rows[-1][0][1]) <= tol:
  37. rows[-1].append(w)
  38. else:
  39. rows.append([w])
  40. for r in rows:
  41. r.sort(key=lambda w: w[0])
  42. return rows
  43. END_MARKERS = ("※", "【", "注:", "检测者:", "审核者:", "第")
  44. def next_marker_y(words, after_y, markers=END_MARKERS):
  45. """after_y 之后第一个表格结束标记的 y 坐标;找不到返回 None。"""
  46. cands = [y for x, y, t in words if y > after_y and any(t.startswith(m) for m in markers)]
  47. return min(cands) if cands else None
  48. def clean(s):
  49. return re.sub(r"\s+", "", s or "")
  50. def find_headings(words):
  51. """识别左端起编号的章节标题,返回 [(y, 编号, 名称), ...]。"""
  52. heads = []
  53. for row in cluster_rows(words, tol=4.0):
  54. if not row:
  55. continue
  56. first = row[0]
  57. if not (55 <= first[0] <= 100):
  58. continue
  59. t0 = first[2]
  60. # 支持多级编号(如 1.7.1 条件致病菌)
  61. m = re.match(r"^(\d+(?:\.\d+)*)[\.、\s]?(.*)$", t0)
  62. if not m:
  63. continue
  64. code, rest = m.group(1), m.group(2)
  65. name = rest + "".join(t for x, y, t in row[1:] if x < 320)
  66. heads.append((first[1], code, clean(name)))
  67. return heads
  68. # ---------------------------------------------------------------- 封面/机构
  69. def parse_cover(doc):
  70. text = doc[1].get_text()
  71. info = {}
  72. for key, pat in {
  73. "姓名": r"姓名[::]\s*\n?([^\n]+)",
  74. "性别": r"姓别[::]\s*\n?([^\n]+)",
  75. "出生年月": r"出生年月[::]\s*\n?([^\n]+)",
  76. "样本条码": r"样本条码[::]\s*\n?([^\n]+)",
  77. "采样日期": r"采样日期[::]\s*\n?([^\n]+)",
  78. "报告日期": r"报告日期[::]\s*\n?([^\n]+)",
  79. }.items():
  80. m = re.search(pat, text)
  81. if m:
  82. v = m.group(1).strip()
  83. info[key] = "" if v in ("年-月-日",) else v
  84. return info
  85. def parse_org(doc):
  86. text = doc[-1].get_text()
  87. org = {}
  88. for pat, key in [
  89. (r"公司官网[::]\s*(\S+)", "官网"),
  90. (r"联系电话[::]\s*(\S+)", "电话"),
  91. (r"联系邮箱[::]\s*(\S+)", "邮箱"),
  92. (r"公司地址[::]\s*(.+)$", "地址"),
  93. (r"^(.*?实验室)\s*$", "名称"),
  94. ]:
  95. m = re.search(pat, text, re.M)
  96. if m:
  97. org[key] = m.group(1).strip()
  98. return org
  99. # ---------------------------------------------------------------- 表格解析器
  100. def parse_triplet_table(words, y_start, y_end, name_x_max=300.0, val_range=(300.0, 432.0)):
  101. """名称|评估值|检测结果 三列表。锚点=检测结果列(x>=val_range[1])。"""
  102. hi = y_end if y_end is not None else 10 ** 9
  103. scope = [w for w in words if y_start < w[1] < hi]
  104. anchors = sorted([w for w in scope if w[0] >= val_range[1]], key=lambda w: w[1])
  105. if not anchors:
  106. return []
  107. bounds = [y_start] + [(anchors[i][1] + anchors[i + 1][1]) / 2 for i in range(len(anchors) - 1)] + [hi]
  108. out = []
  109. for i, anc in enumerate(anchors):
  110. cells = [w for w in scope if bounds[i] <= w[1] < bounds[i + 1]]
  111. cells.sort(key=lambda w: (w[1], w[0]))
  112. name = clean("".join(t for x, y, t in cells if x < name_x_max))
  113. value = clean("".join(t for x, y, t in cells if val_range[0] <= x < val_range[1]))
  114. if name or value:
  115. out.append({"名称": name, "数值": value, "状态": clean(anc[2])})
  116. return out
  117. def parse_abx_table(words, y_start, y_end):
  118. """类别|名称|评估值|检测结果 四列表(抗生素耐药性,类别列纵向合并,按行只取名称)。"""
  119. hi = y_end if y_end is not None else 10 ** 9
  120. scope = [w for w in words if y_start < w[1] < hi]
  121. anchors = sorted([w for w in scope if w[0] >= 432], key=lambda w: w[1])
  122. if not anchors:
  123. return []
  124. bounds = [y_start] + [(anchors[i][1] + anchors[i + 1][1]) / 2 for i in range(len(anchors) - 1)] + [hi]
  125. out = []
  126. for i, anc in enumerate(anchors):
  127. cells = sorted([w for w in scope if bounds[i] <= w[1] < bounds[i + 1]], key=lambda w: (w[1], w[0]))
  128. name = clean("".join(t for x, y, t in cells if 230 <= x < 335))
  129. value = clean("".join(t for x, y, t in cells if 335 <= x < 432))
  130. cat = ""
  131. m = re.match(r"^(.{0,8}?(?:内酰胺类|氨基糖苷类|多肽类|四环素类))(?!$)(.+)$", name)
  132. if m: # 类别列与名称列粘连时剥离
  133. raw, name = m.group(1), m.group(2)
  134. cat = next((c for c in CATEGORY_PREFIXES if c[1:] in raw or c in raw), raw)
  135. if name or value:
  136. out.append({"类别": cat, "名称": name, "数值": value, "状态": clean(anc[2])})
  137. return out
  138. def parse_quad_table(words, y_start, y_end, cols):
  139. """单行四列表(门水平/属水平/特征菌/指标详情/参考表)。cols=[(x_min,x_max),...]。"""
  140. hi = y_end if y_end is not None else 10 ** 9
  141. scope = [w for w in words if y_start < w[1] < hi]
  142. out = []
  143. for row in cluster_rows(scope, tol=9.0):
  144. cells = [clean("".join(t for x, y, t in row if lo <= x < hi2)) for lo, hi2 in cols]
  145. if any(cells):
  146. out.append(cells)
  147. return out
  148. LATIN_RE = re.compile(r"^[A-Za-z][A-Za-z0-9 _().,'\-]*$")
  149. # 结果评价列合法状态词(用于过滤"结果评价"表头折行产生的假锚点)
  150. STATUS_WORDS = {"正常", "偏低", "偏高", "未检出", "注意", "耐药", "缺乏", "超标", "检出"}
  151. CN_STOP = ",。;、!?…"
  152. SPECIES_RE = re.compile(r"(?<=[\u4e00-\u9fff])([a-z][A-Za-z0-9\-]*)")
  153. def pick_cn(text):
  154. """取中文名的合理片段:首个中文标点之前。"""
  155. text = clean(text or "")
  156. for ch in CN_STOP:
  157. if ch in text:
  158. text = text.split(ch)[0]
  159. break
  160. return text
  161. def parse_bacteria_table(words, y_start, y_end):
  162. """菌名|功能说明|检测结果(%)|结果评价 表(有益菌/条件致病菌/致病菌)。
  163. 锚点=结果评价列(x>=470)且y在表头之下、文本为合法状态词;
  164. 左列按语种拆分:纯拉丁词归拉丁名,其余归菌名/功能说明。
  165. """
  166. hi = y_end if y_end is not None else 10 ** 9
  167. scope = [w for w in words if y_start < w[1] < hi]
  168. anchors = sorted([w for w in scope if w[0] >= 470 and w[1] > y_start + 6 and w[2] in STATUS_WORDS],
  169. key=lambda w: w[1])
  170. if not anchors:
  171. return []
  172. bounds = [y_start] + [(anchors[i][1] + anchors[i + 1][1]) / 2 for i in range(len(anchors) - 1)] + [hi]
  173. out = []
  174. for i, anc in enumerate(anchors):
  175. cells = [w for w in scope if bounds[i] <= w[1] < bounds[i + 1]]
  176. left = sorted([w for w in cells if w[0] < 160], key=lambda w: w[1])
  177. cn, latin, extra_desc = "", [], []
  178. if left:
  179. first_y = left[0][1]
  180. cn = clean("".join(t for x, y, t in left if abs(y - first_y) < 6))
  181. for x, y, t in left:
  182. if abs(y - first_y) < 6:
  183. continue
  184. if LATIN_RE.match(t):
  185. latin.append(t)
  186. else:
  187. extra_desc.append(t)
  188. mid = sorted([w for w in cells if 160 <= w[0] < 390], key=lambda w: (w[1], w[0]))
  189. desc = "".join(t for x, y, t in mid)
  190. val = sorted([w for w in cells if 390 <= w[0] < 470], key=lambda w: (w[1], w[0]))
  191. # 中文名取首个标点前的片段;过长说明整段叙述溢入左列
  192. cn = pick_cn(cn)
  193. if cn and len(cn) > 24:
  194. extra_desc.insert(0, cn)
  195. cn = ""
  196. if not latin and desc:
  197. m = re.search(r"([A-Z][a-z]+ [a-z]+)", desc)
  198. if m:
  199. latin = [m.group(1)]
  200. # 描述列中被挤出的种名(如 "伤寒enterica")补回拉丁名
  201. if latin and desc:
  202. for sp in SPECIES_RE.findall(desc):
  203. if len(latin) < 3 and len(sp) >= 4 and not sp.isupper():
  204. latin.append(sp)
  205. out.append({
  206. "中文名": cn,
  207. "拉丁名": " ".join(latin).strip(),
  208. "功能说明": ("".join(extra_desc) + desc).strip(),
  209. "检测结果%": clean("".join(t for x, y, t in val)),
  210. "结果评价": clean(anc[2]),
  211. })
  212. return out
  213. DIR_RE = re.compile(r"^(正相关|负相关|补充)([\d.]*)$")
  214. DIRECTION_WORDS = {"正相关", "负相关", "补充", "相关", "性"}
  215. def parse_correlation_table(words, y_start, y_end):
  216. """菌种|相关性|检测结果|参考范围|结果评价 表(过敏/疾病相关菌)。"""
  217. hi = y_end if y_end is not None else 10 ** 9
  218. scope = [w for w in words if y_start < w[1] < hi]
  219. anchors = sorted([w for w in scope if w[0] >= 440 and w[1] > y_start + 6
  220. and w[2] not in ("结果评价", "评价", "价")], key=lambda w: w[1])
  221. if not anchors:
  222. return []
  223. bounds = [y_start] + [(anchors[i][1] + anchors[i + 1][1]) / 2 for i in range(len(anchors) - 1)] + [hi]
  224. out = []
  225. for i, anc in enumerate(anchors):
  226. cells = [w for w in scope if bounds[i] <= w[1] < bounds[i + 1]]
  227. left = sorted([w for w in cells if w[0] < 190], key=lambda w: w[1])
  228. cn, latin = [], []
  229. direction, value, rng = "", "", ""
  230. if left:
  231. first_y = left[0][1]
  232. for x, y, t in left:
  233. if t in DIRECTION_WORDS:
  234. direction = direction or t
  235. elif LATIN_RE.match(t):
  236. latin.append(t)
  237. elif abs(y - first_y) < 6:
  238. cn.append(t)
  239. cn, latin = clean("".join(cn)), " ".join(latin).strip()
  240. right = sorted([w for w in cells if 190 <= w[0] < 440], key=lambda w: w[0])
  241. nums = []
  242. for x, y, t in right:
  243. m = DIR_RE.match(t)
  244. if m:
  245. direction = m.group(1)
  246. if m.group(2):
  247. nums.append(m.group(2))
  248. elif re.match(r"^[\d.]+$", t):
  249. nums.append(t)
  250. if nums:
  251. value = nums[0]
  252. rest = nums[1:]
  253. if rest:
  254. rng = rest[0] if len(rest) == 1 else f"{rest[0]}-{rest[-1]}"
  255. out.append({
  256. "菌种": cn,
  257. "拉丁名": latin.strip(),
  258. "相关性": direction,
  259. "检测结果": value,
  260. "参考范围": rng,
  261. "结果评价": clean(anc[2]),
  262. })
  263. return out
  264. # ---------------------------------------------------------------- 文本块
  265. BLOCK_LABELS = ["相关介绍", "食物来源", "注意事项", "温馨提示", "结果提示", "疾病介绍", "调理建议", "检测结果", "检测数据"]
  266. def extract_text_blocks(words, y_start, y_end):
  267. """提取 相关介绍/食物来源/注意事项/疾病介绍/调理建议 等文本块(保留原文换行)。"""
  268. hi = y_end if y_end is not None else 10 ** 9
  269. scope = sorted([w for w in words if y_start < w[1] < hi], key=lambda w: (round(w[1], 1), w[0]))
  270. blocks, cur_label, lines, cur_y = {}, None, [], None
  271. for x, y, t in scope:
  272. line_start = x < 100
  273. stem = t.rstrip("::")
  274. if line_start and stem in BLOCK_LABELS:
  275. if cur_label and lines:
  276. blocks[cur_label] = "\n".join(lines)
  277. cur_label, lines, cur_y = stem, [], y
  278. continue
  279. if line_start and (t.startswith("※") or t.startswith("【") or re.match(r"^\d+(\.\d+)?[\.、\s]?$", t)):
  280. if cur_label and lines:
  281. blocks[cur_label] = "\n".join(lines)
  282. cur_label, lines, cur_y = None, [], None
  283. continue
  284. if cur_label is not None:
  285. if cur_y is not None and abs(y - cur_y) > 6:
  286. lines.append("\n")
  287. cur_y = y
  288. lines.append(t)
  289. if cur_label and lines:
  290. blocks[cur_label] = "".join(lines)
  291. return {k: v for k, v in blocks.items() if v}
  292. # ---------------------------------------------------------------- 章节定义
  293. SUMMARY_SECTIONS = [ # (标题关键词, 输出键, 解析器)
  294. ("肠道菌群整体健康状况", "报告概述", "triplet"),
  295. ("肠道免疫力评估", "肠道免疫力评估", "triplet"),
  296. ("营养物质代谢能力", "营养物质代谢能力", "triplet"),
  297. ("短链脂肪酸合成能力", "短链脂肪酸合成能力", "triplet"),
  298. ("维生素评估", "维生素评估", "triplet"),
  299. ("天然色素合成能力", "天然色素合成能力", "triplet"),
  300. ("氨基酸合成能力", "氨基酸合成能力", "triplet"),
  301. ("谷胱甘肽合成能力", "谷胱甘肽合成能力", "triplet"),
  302. ("胆汁酸合成能力", "胆汁酸合成能力", "triplet"),
  303. ("健康相关矿物质评估", "健康相关矿物质评估", "triplet"),
  304. ("多轴系统风险评估", "多轴系统风险评估", "triplet"),
  305. ("毒素降解能力评估", "毒素降解能力评估", "triplet"),
  306. ("抗生素耐药性评估", "抗生素耐药性评估", "abx"),
  307. ("肠道菌群过敏风险评估", "肠道菌群过敏风险评估", "triplet"),
  308. ("肠道菌群与疾病相关风险评估", "肠道菌群与疾病相关风险评估", "triplet"),
  309. ]
  310. CATEGORY_PREFIXES = ("β-内酰胺类", "氨基糖苷类", "多肽类", "四环素类") # 用于粘连剥离与归一化
  311. ABX_CATEGORY = {
  312. "青霉素": "β-内酰胺类", "头孢菌素": "β-内酰胺类",
  313. "链霉素": "氨基糖苷类", "卡那霉素": "氨基糖苷类", "庆大霉素": "氨基糖苷类", "新霉素": "氨基糖苷类",
  314. "万古霉素": "多肽类", "四环素": "四环素类",
  315. }
  316. DETAIL_TITLES = {
  317. "1.1": "肠道菌群状态", "1.2": "肠道微生物定植抗力", "1.3": "菌群多样性", "1.4": "肠型",
  318. "1.5": "厚壁菌门/拟杆菌门(F/B)", "1.8": "肠道抗炎能力", "1.9": "肠道免疫力",
  319. "2.1": "糖(碳水化合物)", "2.2": "蛋白质", "2.3": "脂肪", "2.4": "嘌呤",
  320. "3.1": "甲酸", "3.2": "乙酸", "3.3": "丙酸", "3.4": "丁酸", "3.5": "戊酸",
  321. "5.1": "类胡萝卜素", "5.2": "类黄酮", "5.3": "花青素",
  322. "7.1": "苯甲酸盐", "7.2": "苯氨基甲酸酯", "7.3": "氯代烷烃", "7.4": "氯代环己烷", "7.5": "氯苯",
  323. "7.6": "甲苯", "7.7": "二甲苯", "7.8": "硝基甲苯", "7.9": "苯乙烯", "7.10": "乙苯",
  324. "7.11": "阿特拉津", "7.12": "己内酰胺", "7.13": "双酚A", "7.14": "二噁英", "7.15": "萘",
  325. "7.16": "多环芳烃",
  326. "9.1": "胃胀", "9.2": "腹胀和排气", "9.3": "便秘", "9.4": "腹泻", "9.5": "炎症性肠病(IBD)",
  327. "9.6": "肠易激综合征(IBS)", "9.7": "克罗恩病", "9.8": "结直肠癌", "9.9": "肥胖",
  328. "9.10": "II型糖尿病", "9.11": "高脂血", "9.12": "痛风(高尿酸)", "9.13": "非酒精性脂肪肝",
  329. "9.14": "精神分裂", "9.15": "抑郁、焦虑", "9.16": "阿尔兹海默病", "9.17": "帕金森", "9.18": "痤疮",
  330. }
  331. for i, vt in enumerate(["A", "B1", "B2", "B3", "B5", "B6", "B7", "B9", "B12", "C", "K", "D", "E"]):
  332. DETAIL_TITLES[f"4.{i + 1}"] = f"维生素{vt}"
  333. AA_TITLES = ["赖氨酸", "丝氨酸", "亮氨酸", "色氨酸", "苯丙氨酸", "缬氨酸", "组氨酸", "半胱氨酸",
  334. "酪氨酸", "丙氨酸", "脯氨酸", "苏氨酸", "谷氨酸", "异亮氨酸", "精氨酸", "甲硫氨酸",
  335. "甘氨酸", "天冬氨酸"]
  336. for i, aa in enumerate(AA_TITLES):
  337. DETAIL_TITLES[f"6.{i + 1}"] = aa
  338. ALLERGY_TITLES = {"8.1": "花生过敏", "8.2": "鸡蛋过敏", "8.3": "坚果过敏", "8.4": "牛奶过敏",
  339. "8.5": "甲壳类和软体动物过敏", "8.6": "麸质敏感"}
  340. # ---------------------------------------------------------------- 主流程
  341. def parse(pdf_path):
  342. doc = fitz.open(str(pdf_path))
  343. pages = [page_words(p) for p in doc]
  344. result = {
  345. "文件名": Path(pdf_path).stem,
  346. "格式": "aidefansi",
  347. "总页数": len(doc),
  348. "报告机构": parse_org(doc),
  349. "个人信息": parse_cover(doc),
  350. }
  351. # ---- 1) 整体健康分表(PDF 第 5-19 页)----
  352. sections = {key: [] for _, key, _ in SUMMARY_SECTIONS}
  353. cur_key = None
  354. for pno in range(4, 19):
  355. page, words = doc[pno], pages[pno]
  356. # 章节起点:标题词命中(左边缘 x<100 或页面顶部 y<200)
  357. starts = []
  358. for title, key, _kind in SUMMARY_SECTIONS:
  359. for r in page.search_for(title):
  360. if r.x0 < 100 or r.y0 < 200:
  361. starts.append((r.y0, key))
  362. starts.sort()
  363. # 表头行("名称")
  364. header_ys = sorted([y for x, y, t in words if t == "名称"])
  365. # 表格区域:[表头 y, 下一个结束标记 y)
  366. regions = []
  367. for hy in header_ys:
  368. y_end = next_marker_y(words, hy)
  369. # 区域归属:本页中表头上方最近的章节起点,否则继承上一页章节
  370. owner = None
  371. for sy, key in starts:
  372. if sy < hy:
  373. owner = key
  374. regions.append((hy, y_end, owner or cur_key))
  375. if owner:
  376. cur_key = owner
  377. # 无表头且无章节起点的续页:顶部区域归属当前章节
  378. if not header_ys and not starts:
  379. y_end = next_marker_y(words, 0)
  380. if y_end and y_end > 100:
  381. regions.append((100, y_end, cur_key))
  382. for hy, y_end, key in regions:
  383. if key is None:
  384. continue
  385. kind = next(kd for t, k, kd in SUMMARY_SECTIONS if k == key)
  386. parser = parse_abx_table if kind == "abx" else parse_triplet_table
  387. rows = parser(words, hy, y_end)
  388. sections[key].extend(rows)
  389. if starts:
  390. cur_key = starts[-1][1]
  391. overview_text = doc[4].get_text()
  392. m = re.search(r"您的肠道总评分为[::]\s*\n?\s*(\d+)分", overview_text)
  393. total_score = int(m.group(1)) if m else None
  394. result["报告概述"] = {r["名称"]: {"数值": r["数值"], "状态": r["状态"]}
  395. for r in sections.pop("报告概述", [])}
  396. if total_score is not None:
  397. result["报告概述"]["肠道总评分"] = {"数值": str(total_score), "状态": ""}
  398. for key in ["肠道免疫力评估", "营养物质代谢能力", "短链脂肪酸合成能力", "维生素评估",
  399. "天然色素合成能力", "氨基酸合成能力", "谷胱甘肽合成能力", "胆汁酸合成能力",
  400. "健康相关矿物质评估", "多轴系统风险评估", "毒素降解能力评估",
  401. "肠道菌群过敏风险评估", "肠道菌群与疾病相关风险评估"]:
  402. result[key] = sections.get(key, [])
  403. result["抗生素耐药性评估"] = [
  404. {"类别": ABX_CATEGORY.get(r["名称"]) or r.get("类别", ""),
  405. "名称": r["名称"], "数值": r["数值"], "状态": r["状态"]}
  406. for r in sections.get("抗生素耐药性评估", [])
  407. ]
  408. # ---- 2) 细菌检出表(PDF 第 26-34 页:1.6 有益菌 / 1.7.1 条件致病菌 / 1.7.2 致病菌)----
  409. bact = {"有益菌检出": [], "条件致病菌检出": [], "致病菌检出": []}
  410. bact_notes = {}
  411. cur_bact = None
  412. for pno in range(25, 34):
  413. words = pages[pno]
  414. heads = find_headings(words)
  415. bact_heads = []
  416. for hy, code, name in heads:
  417. if code == "1.6":
  418. bact_heads.append((hy, "有益菌检出"))
  419. elif code == "1.7.1":
  420. bact_heads.append((hy, "条件致病菌检出"))
  421. elif code == "1.7.2":
  422. bact_heads.append((hy, "致病菌检出"))
  423. elif code == "1.7" and name.startswith("条件致病菌"):
  424. bact_heads.append((hy, "条件致病菌检出"))
  425. elif code == "1.7" and name.startswith("致病菌"):
  426. bact_heads.append((hy, "致病菌检出"))
  427. header_ys = sorted([y for x, y, t in words if t == "菌名"])
  428. if bact_heads:
  429. cur_bact = bact_heads[-1][1]
  430. if cur_bact is None:
  431. continue
  432. if header_ys:
  433. y_start = header_ys[0]
  434. y_end = next_marker_y(words, y_start)
  435. bact[cur_bact].extend(parse_bacteria_table(words, y_start, y_end))
  436. else:
  437. # 续页:顶部到第一个结束标记
  438. y_end = next_marker_y(words, 0)
  439. if y_end and y_end > 100:
  440. bact[cur_bact].extend(parse_bacteria_table(words, 100, y_end))
  441. # 结果提示/注释块
  442. blocks = extract_text_blocks(words, 0, None)
  443. for lbl in ("结果提示", "温馨提示"):
  444. if blocks.get(lbl):
  445. bact_notes.setdefault(cur_bact, {}).setdefault(lbl, blocks[lbl])
  446. result.update(bact)
  447. if bact_notes:
  448. result["细菌检出附注"] = bact_notes
  449. # ---- 3) 分项说明页(PDF 第 21-114 页)----
  450. details, ref_tables = [], {}
  451. correlations, disease_notes = {}, {}
  452. cur_sec = None # (code, name, kind)
  453. for pno in range(20, 114):
  454. words = pages[pno]
  455. heads = find_headings(words)
  456. sec_list = []
  457. for hy, code, name in heads:
  458. kind = "allergy" if code in ALLERGY_TITLES else ("disease" if code.startswith("9.") else "detail")
  459. sec_list.append((hy, code, name, kind))
  460. # 页首无归属文本块 -> 继承上一章节
  461. first_head_y = sec_list[0][0] if sec_list else None
  462. if cur_sec and first_head_y:
  463. blocks = extract_text_blocks(words, 0, first_head_y)
  464. _absorb_blocks(cur_sec, blocks, correlations, disease_notes, details, ref_tables)
  465. if not sec_list:
  466. # 整页续上一章节
  467. if cur_sec:
  468. _parse_section_page(doc, pno, words, cur_sec, 0, None,
  469. correlations, disease_notes, details, ref_tables)
  470. continue
  471. for idx, (hy, code, name, kind) in enumerate(sec_list):
  472. y_end = sec_list[idx + 1][0] if idx + 1 < len(sec_list) else None
  473. cur_sec = (code, name, kind)
  474. _parse_section_page(doc, pno, words, cur_sec, hy, y_end,
  475. correlations, disease_notes, details, ref_tables)
  476. result["指标详解"] = details
  477. if ref_tables:
  478. result["分项检测参考"] = ref_tables
  479. if correlations:
  480. result["过敏相关菌"] = {k: v for k, v in correlations.items() if k in ALLERGY_TITLES.values()}
  481. result["疾病相关菌"] = {k: v for k, v in correlations.items() if k not in ALLERGY_TITLES.values()}
  482. if disease_notes:
  483. result["疾病介绍与调理建议"] = disease_notes
  484. # ---- 4) 菌群组成(PDF 第 117-121 页)----
  485. quad_cols = [(90, 240), (240, 320), (320, 410), (410, 520)]
  486. comp = {"门水平": [], "属水平": [], "特征菌科属种": []}
  487. prev_comp = None
  488. for pno in range(116, 121):
  489. words = pages[pno]
  490. text = doc[pno].get_text()
  491. y_end = next_marker_y(words, 0, markers=("第",)) # 过滤页脚"第 N 页"
  492. rows = []
  493. if "您的肠道菌群组成(门水平)" in text:
  494. prev_comp, rows = "门水平", parse_quad_table(words, 190, y_end, cols=quad_cols)
  495. elif "您的肠道菌群组成(属水平)" in text:
  496. prev_comp, rows = "属水平", parse_quad_table(words, 180, y_end, cols=quad_cols)
  497. elif "特征菌科属种" in text:
  498. prev_comp, rows = "特征菌科属种", parse_quad_table(words, 180, y_end, cols=quad_cols)
  499. elif prev_comp: # 续页
  500. rows = parse_quad_table(words, 100, y_end, cols=quad_cols)
  501. if rows and prev_comp:
  502. rows = [r for r in rows if not (len(r) >= 2 and r[1] in ("页",) or (r[0].startswith("参考文献")))]
  503. comp[prev_comp].extend(rows)
  504. result["肠道菌群组成"] = {
  505. "门水平": [{"拉丁名": r[0], "中文名": r[1], "检测结果%": r[2], "参考范围%": r[3]}
  506. for r in comp["门水平"] if len(r) >= 4],
  507. "属水平": [{"拉丁名": r[0], "中文名": r[1], "检测结果%": r[2], "参考范围%": r[3]}
  508. for r in comp["属水平"] if len(r) >= 4],
  509. "特征菌科属种": [{"拉丁学名": r[0], "中文名称": r[1], "检测结果%": r[2], "阈值%": r[3]}
  510. for r in comp["特征菌科属种"] if len(r) >= 4],
  511. }
  512. # ---- 4.1) 组成表:先拆分粘连的"拉丁+中文",再建词典回填中文名 ----
  513. for r in result["肠道菌群组成"]["特征菌科属种"]:
  514. m = re.match(r"^([A-Za-z][A-Za-z0-9 _.()\-']*)([\u4e00-\u9fff].*)$", r["拉丁学名"])
  515. if m:
  516. r["拉丁学名"], r["中文名称"] = m.group(1).strip(), (m.group(2) + r["中文名称"]).strip()
  517. latin_cn = {}
  518. for key in ("有益菌检出", "条件致病菌检出", "致病菌检出"):
  519. for r in result.get(key, []):
  520. la, cn = r.get("拉丁名", ""), r.get("中文名", "")
  521. cn0 = re.split(r"[((]", cn)[0]
  522. # 同拉丁名取最短中文名,避免整句叙述进词典
  523. if la and cn0 and len(cn0) <= 24:
  524. if la not in latin_cn or len(cn0) < len(latin_cn[la]):
  525. latin_cn[la] = cn0
  526. # 疾病/过敏相关菌表也纳入词典(含大量干净的 拉丁名-中文名 对应)
  527. if correlations:
  528. for rows in correlations.values():
  529. for r in rows:
  530. la, cn = r.get("拉丁名", ""), r.get("菌种", "")
  531. cn0 = re.split(r"[((]", cn)[0]
  532. if la and cn0 and len(cn0) <= 24:
  533. if la not in latin_cn or len(cn0) < len(latin_cn[la]):
  534. latin_cn[la] = cn0
  535. for group, latin_key, cn_key in (("门水平", "拉丁名", "中文名"), ("属水平", "拉丁名", "中文名"),
  536. ("特征菌科属种", "拉丁学名", "中文名称")):
  537. for r in result["肠道菌群组成"][group]:
  538. la, cn = r.get(latin_key) or "", r.get(cn_key) or ""
  539. cn0 = re.split(r"[((]", cn)[0]
  540. if la and cn0 and (la not in latin_cn or len(cn0) < len(latin_cn[la])):
  541. latin_cn[la] = cn0
  542. for key in ("有益菌检出", "条件致病菌检出", "致病菌检出"):
  543. for r in result.get(key, []):
  544. la = r.get("拉丁名", "")
  545. base = la.split()[0] if la else ""
  546. if la:
  547. dict_cn = latin_cn.get(la) or latin_cn.get(base)
  548. cur = r.get("中文名", "")
  549. # 缺失或明显溢出(如整句叙述混入)时用词典标准中文名覆盖
  550. if dict_cn and (not cur or len(cur) > len(dict_cn) + 2):
  551. r["中文名"] = dict_cn
  552. # ---- 5) 报告声明(致客户)----
  553. result["报告声明"] = "\n".join(l.strip() for l in doc[3].get_text().splitlines() if l.strip())
  554. return result
  555. def _absorb_blocks(cur_sec, blocks, correlations, disease_notes, details, ref_tables):
  556. """把页首无归属文本块并入上一章节。"""
  557. code, name, kind = cur_sec
  558. if kind == "disease" and (blocks.get("疾病介绍") or blocks.get("调理建议")):
  559. sec_name = DETAIL_TITLES.get(code, name)
  560. rec = disease_notes.setdefault(sec_name, {"疾病介绍": "", "调理建议": ""})
  561. rec["疾病介绍"] += blocks.get("疾病介绍", "")
  562. rec["调理建议"] += blocks.get("调理建议", "")
  563. elif kind == "detail":
  564. sec_name = DETAIL_TITLES.get(code, name)
  565. for entry in details:
  566. if entry.get("编号") == code:
  567. for lbl in ("相关介绍", "食物来源", "注意事项", "温馨提示", "结果提示"):
  568. if blocks.get(lbl):
  569. entry[lbl] = entry.get(lbl, "") + blocks[lbl]
  570. break
  571. def _parse_section_page(doc, pno, words, cur_sec, y_start, y_end,
  572. correlations, disease_notes, details, ref_tables):
  573. """解析单个章节在其起始页/续页上的表格与文本块。"""
  574. code, name, kind = cur_sec
  575. sec_name = DETAIL_TITLES.get(code, name)
  576. hi = y_end if y_end is not None else 10 ** 9
  577. scope_words = [w for w in words if y_start < w[1] < hi] if y_start else words
  578. if kind in ("allergy", "disease"):
  579. # 相关性表:找"菌种"表头;续页无表头时从 y_start 开始
  580. header_y = None
  581. for x, y, t in scope_words:
  582. if t == "菌种":
  583. header_y = y
  584. break
  585. t_start = header_y if header_y is not None else (y_start if y_start else 100)
  586. t_end = next_marker_y(words, t_start, markers=("※", "【", "疾病介绍:", "调理建议:", "第"))
  587. rows = parse_correlation_table(words, t_start, t_end)
  588. if rows:
  589. correlations.setdefault(sec_name, []).extend(rows)
  590. blocks = extract_text_blocks(words, y_start if y_start else 0, y_end)
  591. if kind == "disease" and (blocks.get("疾病介绍") or blocks.get("调理建议")):
  592. rec = disease_notes.setdefault(sec_name, {"疾病介绍": "", "调理建议": ""})
  593. rec["疾病介绍"] += blocks.get("疾病介绍", "")
  594. rec["调理建议"] += blocks.get("调理建议", "")
  595. return
  596. # kind == detail
  597. if code in ("1.6", "1.7", "1.7.1", "1.7.2"):
  598. return # 细菌表由专门流程处理
  599. header_y, header_kind = None, None
  600. for x, y, t in scope_words:
  601. if t in ("功能", "检测项目"):
  602. header_y, header_kind = y, t
  603. break
  604. entry = {"编号": code, "名称": sec_name}
  605. if code == "1.1":
  606. # GMHI 两列表:检测项目|评估值
  607. rows = parse_quad_table(words, y_start, y_end, cols=[(90, 300), (300, 520)])
  608. if rows:
  609. entry["表格"] = [{"检测项目": r[0], "评估值": r[1]} for r in rows if len(r) >= 2 and r[0]]
  610. elif header_y is not None and header_kind in ("功能", "检测项目"):
  611. t_end = next_marker_y(words, header_y, markers=("※", "【", "相关介绍:"))
  612. if header_kind == "功能":
  613. rows = parse_quad_table(words, header_y, t_end, cols=[(90, 175), (175, 280), (280, 400), (400, 520)])
  614. if rows:
  615. entry["表格"] = [{"功能": r[0], "评估值": r[1], "参考范围": r[2], "结果评价": r[3]}
  616. for r in rows if len(r) >= 4]
  617. else:
  618. rows = parse_quad_table(words, header_y, t_end, cols=[(90, 170), (170, 250), (250, 380), (380, 520)])
  619. if rows:
  620. ref_tables.setdefault(sec_name, []).extend(
  621. [{"检测项目": r[0], "检测值": r[1], "结果评价": r[2], "参考范围": r[3]}
  622. for r in rows if len(r) >= 4])
  623. blocks = extract_text_blocks(words, y_start if y_start else 0, y_end)
  624. for lbl in ("相关介绍", "食物来源", "注意事项", "温馨提示", "结果提示"):
  625. if blocks.get(lbl):
  626. entry[lbl] = blocks[lbl]
  627. if len(entry) > 2 or entry.get("表格") or ref_tables.get(sec_name):
  628. details.append(entry)
  629. def main():
  630. if len(sys.argv) < 2:
  631. sys.exit(__doc__)
  632. pdf = Path(sys.argv[1])
  633. out = Path(sys.argv[sys.argv.index("-o") + 1]) if "-o" in sys.argv else pdf.with_suffix(".json")
  634. data = parse(pdf)
  635. out.write_text(json.dumps(data, ensure_ascii=False, indent=1), encoding="utf-8")
  636. print(f"written: {out}")
  637. for k in ["报告概述", "肠道免疫力评估", "营养物质代谢能力", "短链脂肪酸合成能力", "维生素评估",
  638. "天然色素合成能力", "氨基酸合成能力", "谷胱甘肽合成能力", "胆汁酸合成能力",
  639. "健康相关矿物质评估", "多轴系统风险评估", "毒素降解能力评估", "抗生素耐药性评估",
  640. "肠道菌群过敏风险评估", "肠道菌群与疾病相关风险评估", "有益菌检出", "条件致病菌检出",
  641. "致病菌检出", "指标详解", "疾病相关菌", "过敏相关菌", "疾病介绍与调理建议",
  642. "分项检测参考", "肠道菌群组成"]:
  643. v = data.get(k)
  644. if isinstance(v, dict) and k == "肠道菌群组成":
  645. print(f" {k}: 门{len(v['门水平'])} 属{len(v['属水平'])} 特征菌{len(v['特征菌科属种'])}")
  646. else:
  647. n = len(v) if hasattr(v, "__len__") else "-"
  648. print(f" {k}: {n}")
  649. if __name__ == "__main__":
  650. main()