json_converter.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. # Copyright (c) Opendatalab. All rights reserved.
  2. """JSON转换模块 v2 - 独立版本,不依赖v1"""
  3. from typing import Dict, Any, Optional, List
  4. import re
  5. import os
  6. from copy import deepcopy
  7. from PIL import Image
  8. from ..utils.logging_config import get_logger
  9. from ..utils.paddleocr_fallback import fallback_parse_with_paddleocr, call_paddleocr
  10. from .document_type import detect_document_type
  11. from .noise_parser import parse_noise_detection_record
  12. from .electromagnetic_parser import parse_electromagnetic_detection_record
  13. from .investment_parser import parse_investment_record
  14. from .table_parser import parse_operational_conditions, parse_operational_conditions_v2, parse_operational_conditions_opstatus, parse_operational_conditions_format3_5
  15. logger = get_logger("pdf_converter_v2.parser.json")
  16. NOISE_HEADER_FIELDS = [
  17. "project",
  18. "standardReferences",
  19. "soundLevelMeterMode",
  20. "soundCalibratorMode",
  21. "calibrationValueBefore",
  22. "calibrationValueAfter",
  23. ]
  24. WEATHER_VALUE_FIELDS = ["weather", "temp", "humidity", "windSpeed", "windDirection"]
  25. def _normalize_date(date: Optional[str]) -> str:
  26. if not date:
  27. return ""
  28. return date.strip().rstrip(".")
  29. def _merge_weather_lists(
  30. primary: Optional[List[Dict[str, Any]]],
  31. secondary: Optional[List[Dict[str, Any]]]
  32. ) -> List[Dict[str, Any]]:
  33. if not primary and not secondary:
  34. return []
  35. if not primary:
  36. return deepcopy(secondary or [])
  37. merged = deepcopy(primary)
  38. if not secondary:
  39. return merged
  40. date_to_index: Dict[str, int] = {}
  41. empty_indices: List[int] = []
  42. for idx, item in enumerate(merged):
  43. norm_date = _normalize_date(item.get("monitorAt"))
  44. if norm_date:
  45. date_to_index.setdefault(norm_date, idx)
  46. else:
  47. empty_indices.append(idx)
  48. for src in secondary:
  49. norm_date = _normalize_date(src.get("monitorAt"))
  50. target = None
  51. if norm_date and norm_date in date_to_index:
  52. target = merged[date_to_index[norm_date]]
  53. elif empty_indices:
  54. target = merged[empty_indices.pop(0)]
  55. if target:
  56. if not _normalize_date(target.get("monitorAt")) and src.get("monitorAt"):
  57. target["monitorAt"] = src["monitorAt"]
  58. for field in WEATHER_VALUE_FIELDS:
  59. if (not target.get(field) or not str(target.get(field)).strip()) and src.get(field):
  60. target[field] = src[field]
  61. else:
  62. merged.append(deepcopy(src))
  63. return merged
  64. def _merge_noise_records(
  65. primary: Optional[Dict[str, Any]],
  66. secondary: Optional[Dict[str, Any]],
  67. preserve_primary_noise: bool = True
  68. ) -> Dict[str, Any]:
  69. if not primary and not secondary:
  70. return {}
  71. merged = deepcopy(primary) if primary else {}
  72. secondary = secondary or {}
  73. for field in NOISE_HEADER_FIELDS:
  74. primary_value = merged.get(field) if merged else ""
  75. secondary_value = secondary.get(field)
  76. if (not primary_value or not str(primary_value).strip()) and secondary_value:
  77. merged[field] = secondary_value
  78. merged["weather"] = _merge_weather_lists(merged.get("weather"), secondary.get("weather"))
  79. if not merged.get("operationalConditions") and secondary.get("operationalConditions"):
  80. merged["operationalConditions"] = deepcopy(secondary["operationalConditions"])
  81. if preserve_primary_noise and primary and primary.get("noise"):
  82. merged["noise"] = deepcopy(primary["noise"])
  83. elif not merged.get("noise") and secondary.get("noise"):
  84. merged["noise"] = deepcopy(secondary["noise"])
  85. return merged
  86. def _merge_electromagnetic_records(
  87. primary: Optional[Dict[str, Any]],
  88. secondary: Optional[Dict[str, Any]],
  89. preserve_primary_electric_magnetic: bool = True
  90. ) -> Dict[str, Any]:
  91. """合并电磁检测记录的原始和fallback解析结果
  92. Args:
  93. primary: 原始解析结果
  94. secondary: fallback解析结果
  95. preserve_primary_electric_magnetic: 是否保留原始的电测数据(默认True)
  96. Returns:
  97. 合并后的数据
  98. """
  99. if not primary and not secondary:
  100. return {}
  101. merged = deepcopy(primary) if primary else {}
  102. secondary = secondary or {}
  103. logger.info(f"[合并数据] 开始合并,primary project: {repr(merged.get('project'))}, secondary project: {repr(secondary.get('project'))}")
  104. # 合并头部字段(如果原始结果中字段为空,使用fallback结果)
  105. header_fields = ["project", "standardReferences", "deviceName", "deviceMode", "deviceCode", "monitorHeight"]
  106. for field in header_fields:
  107. primary_value = merged.get(field) if merged else ""
  108. secondary_value = secondary.get(field)
  109. logger.info(f"[合并数据] 检查字段 {field}: primary={repr(primary_value)}, secondary={repr(secondary_value)}")
  110. if (not primary_value or not str(primary_value).strip()) and secondary_value:
  111. merged[field] = secondary_value
  112. logger.info(f"[合并数据] 从fallback结果补充头部字段: {field} = {secondary_value}")
  113. else:
  114. logger.info(f"[合并数据] 字段 {field} 不满足合并条件,跳过")
  115. # 合并天气信息
  116. primary_weather = merged.get("weather", {}) if merged else {}
  117. secondary_weather = secondary.get("weather", {}) or {}
  118. for field in ["weather", "temp", "humidity", "windSpeed", "windDirection"]:
  119. primary_value = primary_weather.get(field) if primary_weather else ""
  120. secondary_value = secondary_weather.get(field)
  121. if (not primary_value or not str(primary_value).strip()) and secondary_value:
  122. if "weather" not in merged:
  123. merged["weather"] = {}
  124. merged["weather"][field] = secondary_value
  125. # 合并电测数据:优先保留原始数据,如果原始数据为空则使用fallback数据
  126. # 但是需要合并每个数据项的address字段(如果原始数据中address为空,使用fallback数据)
  127. if preserve_primary_electric_magnetic and primary and primary.get("electricMagnetic"):
  128. merged["electricMagnetic"] = deepcopy(primary["electricMagnetic"])
  129. # 合并每个数据项的address字段
  130. secondary_electric_magnetic = secondary.get("electricMagnetic", [])
  131. if secondary_electric_magnetic:
  132. # 建立编号到数据项的映射
  133. code_to_em = {em.get("code", "").upper(): em for em in merged["electricMagnetic"]}
  134. # 从secondary中提取address并填充到merged中
  135. for sec_em in secondary_electric_magnetic:
  136. sec_code = sec_em.get("code", "").upper()
  137. sec_address = sec_em.get("address", "")
  138. if sec_code in code_to_em and sec_address:
  139. # 如果merged中对应数据项的address为空,使用secondary的address
  140. if not code_to_em[sec_code].get("address") or not str(code_to_em[sec_code].get("address")).strip():
  141. code_to_em[sec_code]["address"] = sec_address
  142. logger.info(f"[合并数据] 从fallback结果补充地址: {sec_code} -> {sec_address}")
  143. elif not merged.get("electricMagnetic") and secondary.get("electricMagnetic"):
  144. merged["electricMagnetic"] = deepcopy(secondary["electricMagnetic"])
  145. return merged
  146. def parse_markdown_to_json(markdown_content: str, first_page_image: Optional[Image.Image] = None, output_dir: Optional[str] = None, forced_document_type: Optional[str] = None, enable_paddleocr_fallback: bool = True, input_file: Optional[str] = None) -> Dict[str, Any]:
  147. """将Markdown内容转换为JSON - v2独立版本,不依赖v1和OCR
  148. 如果提供 forced_document_type(正式全称),则优先按指定类型解析。
  149. 支持映射:
  150. - noiseMonitoringRecord -> 使用噪声解析
  151. - electromagneticTestRecord -> 使用电磁解析
  152. - 其他类型:返回空数据占位
  153. Args:
  154. markdown_content: markdown内容
  155. first_page_image: 第一页图片(v2版本不使用)
  156. output_dir: 输出目录(用于查找图片进行备用解析)
  157. forced_document_type: 强制文档类型
  158. enable_paddleocr_fallback: 是否启用PaddleOCR备用解析(默认True)
  159. input_file: 原始输入文件路径(PDF或图片),用于从PDF提取第一页
  160. """
  161. original_markdown = markdown_content
  162. logger.info(f"[JSON转换] 开始解析,forced_document_type={forced_document_type}")
  163. if forced_document_type:
  164. auto_weather_default = False
  165. if forced_document_type == "noiseMonitoringRecord":
  166. noise_record = parse_noise_detection_record(markdown_content, first_page_image=None, output_dir=output_dir)
  167. auto_weather_default = getattr(noise_record, "_auto_weather_default_used", False)
  168. data = noise_record.to_dict()
  169. result = {"document_type": forced_document_type, "data": data}
  170. elif forced_document_type == "electromagneticTestRecord":
  171. data = parse_electromagnetic_detection_record(markdown_content).to_dict()
  172. result = {"document_type": forced_document_type, "data": data}
  173. elif forced_document_type == "operatingConditionInfo":
  174. # 仅解析工况信息
  175. # 优先级:表1检测工况格式 > 格式3/5 > opStatus格式 > 旧格式
  176. # 1. 检查是否为"表1检测工况"格式(使用正则表达式,允许中间有空格)
  177. # 支持:表1检测工况、表 1 检测工况、表 1检测工况、表1 检测工况 等变体
  178. pattern = r'表\s*1\s*检测工况'
  179. if re.search(pattern, markdown_content):
  180. logger.info("[JSON转换] 检测到'表1检测工况'标识(包括空格变体),使用新格式解析")
  181. op_list = parse_operational_conditions_v2(markdown_content)
  182. serialized = [oc.to_dict() if hasattr(oc, "to_dict") else oc for oc in (op_list or [])]
  183. return {"document_type": forced_document_type, "data": {"operationalConditions": serialized}}
  184. # 2. 检查是否为格式3/5(附件 2 工况信息 或 附件 2 工况及工程信息,电压列第一列存储时间段)
  185. # 更精确的判断:必须包含"附件"和"2",且包含"工况信息"或"工况及工程信息"
  186. # 排除格式4("附件 工况及工程信息"没有"2")
  187. has_attachment_2 = re.search(r'附件\s*2', markdown_content) or ("附件2" in markdown_content)
  188. has_condition_info = "工况信息" in markdown_content or "工况及工程信息" in markdown_content
  189. if has_attachment_2 and has_condition_info:
  190. logger.info("[JSON转换] 检测到'附件 2 工况信息'或'附件 2 工况及工程信息'格式,尝试使用格式3/5解析")
  191. op_list = parse_operational_conditions_format3_5(markdown_content)
  192. if op_list:
  193. # 格式3/5返回OperationalConditionV2格式
  194. serialized = [oc.to_dict() if hasattr(oc, "to_dict") else oc for oc in op_list]
  195. logger.info(f"[JSON转换] 格式3/5解析成功,共解析到 {len(serialized)} 条记录")
  196. # 检查是否有缺失字段(如minReactivePower为空)
  197. has_missing_fields = False
  198. required_fields = ["maxVoltage", "minVoltage", "maxCurrent", "minCurrent",
  199. "maxActivePower", "minActivePower", "maxReactivePower", "minReactivePower"]
  200. for record in serialized:
  201. for field in required_fields:
  202. if not record.get(field) or record.get(field) == "":
  203. has_missing_fields = True
  204. logger.warning(f"[JSON转换] 检测到缺失字段: {field} 在记录 {record.get('name', 'unknown')} 中为空")
  205. break
  206. if has_missing_fields:
  207. break
  208. # 如果有缺失字段,调用paddle ocr获取JSON来补充缺失字段
  209. if has_missing_fields and enable_paddleocr_fallback and (output_dir or input_file):
  210. logger.info("[JSON转换] 检测到缺失字段,调用PaddleOCR OCR获取JSON来补充")
  211. try:
  212. # 查找图片路径
  213. image_path = None
  214. if output_dir:
  215. from ..utils.paddleocr_fallback import extract_image_from_markdown
  216. image_path = extract_image_from_markdown(markdown_content, output_dir)
  217. # 如果从markdown中找不到图片,尝试从input_file提取
  218. if not image_path and input_file:
  219. from ..utils.paddleocr_fallback import extract_first_page_from_pdf, detect_file_type
  220. file_type = detect_file_type(input_file)
  221. if file_type == 'pdf':
  222. image_path = extract_first_page_from_pdf(input_file, output_dir)
  223. elif file_type in ['png', 'jpeg', 'jpg']:
  224. image_path = input_file
  225. if image_path and os.path.exists(image_path):
  226. logger.info(f"[JSON转换] 使用PaddleOCR OCR解析图片: {image_path}")
  227. from ..utils.paddleocr_fallback import call_paddleocr_ocr, supplement_missing_fields_from_ocr_json
  228. # 调用OCR获取JSON
  229. ocr_save_path = os.path.dirname(image_path) if image_path else output_dir
  230. ocr_texts, ocr_json_path = call_paddleocr_ocr(image_path, ocr_save_path)
  231. if ocr_json_path and os.path.exists(ocr_json_path):
  232. logger.info(f"[JSON转换] 从OCR JSON文件补充缺失字段: {ocr_json_path}")
  233. # 使用OCR JSON补充缺失字段
  234. serialized = supplement_missing_fields_from_ocr_json(serialized, ocr_json_path)
  235. logger.info("[JSON转换] OCR字段补充完成")
  236. else:
  237. logger.warning("[JSON转换] 未找到OCR JSON文件,无法补充缺失字段")
  238. else:
  239. logger.warning("[JSON转换] 未找到可用的图片文件,无法使用PaddleOCR OCR补充")
  240. except Exception as e:
  241. logger.exception(f"[JSON转换] PaddleOCR OCR补充过程出错: {e}")
  242. return {"document_type": forced_document_type, "data": {"operationalConditions": serialized}}
  243. else:
  244. logger.debug("[JSON转换] 格式3/5解析未找到结果,继续尝试其他格式")
  245. # 3. 检查是否为opStatus格式(附件 工况及工程信息,没有"2",表格结构是U/I/P/Q)
  246. # 格式4:附件 工况及工程信息(没有"2"),且表格结构是U/I/P/Q(不是"检测时间 项目"格式)
  247. # 先检查表格结构,避免误判包含"检测时间"和"项目"列的格式2
  248. is_opstatus_format = False
  249. if "附件" in markdown_content and "工况" in markdown_content and not has_attachment_2:
  250. # 检查表格结构:opStatus格式的表头应该是"名称 时间 U (kV) I (A) P (MW) Q (Mvar)"
  251. # 而不是"检测时间 项目 电压 电流 有功功率 无功功率"
  252. from ..parser.table_parser import extract_table_with_rowspan_colspan
  253. tables = extract_table_with_rowspan_colspan(markdown_content)
  254. for table in tables:
  255. if table and len(table) > 0:
  256. first_row = table[0]
  257. first_row_text = " ".join(first_row).lower()
  258. # 如果包含"检测时间"和"项目",则不是opStatus格式(可能是格式2)
  259. if "检测时间" in first_row_text and "项目" in first_row_text:
  260. logger.debug("[JSON转换] 表格包含'检测时间'和'项目'列,不是opStatus格式,跳过")
  261. is_opstatus_format = False
  262. break
  263. # 如果包含"运行工况"或"U (kV)"、"I (A)"等,则是opStatus格式
  264. if "运行工况" in first_row_text or ("u" in first_row_text and "kv" in first_row_text):
  265. is_opstatus_format = True
  266. break
  267. if is_opstatus_format:
  268. logger.info("[JSON转换] 检测到'附件 工况及工程信息'格式(格式4),使用opStatus格式解析,返回OperationalCondition格式")
  269. op_list = parse_operational_conditions_opstatus(markdown_content)
  270. # 格式4直接返回OperationalCondition格式(旧格式),不转换为V2格式
  271. serialized = [oc.to_dict() if hasattr(oc, "to_dict") else oc for oc in (op_list or [])]
  272. return {"document_type": forced_document_type, "data": {"operationalConditions": serialized}}
  273. # 3. 使用旧格式解析(先尝试有标题模式,如果失败则尝试无标题模式)
  274. logger.info("[JSON转换] 未检测到特殊格式标识,使用旧格式解析")
  275. op_list = parse_operational_conditions(markdown_content, require_title=True)
  276. # 如果没有找到结果,尝试无标题模式(仅根据表格结构判断)
  277. if not op_list:
  278. logger.info("[JSON转换] 有标题模式未找到结果,尝试无标题模式解析")
  279. op_list = parse_operational_conditions(markdown_content, require_title=False)
  280. serialized = [oc.to_dict() if hasattr(oc, "to_dict") else oc for oc in (op_list or [])]
  281. result = {"document_type": forced_document_type, "data": {"operationalConditions": serialized}}
  282. elif forced_document_type in ["fsApproval", "fsReview", "pdApproval", "safetyFsApproval"]:
  283. # 投资估算类型处理(包括安评类)
  284. logger.info(f"[JSON转换] 处理投资估算类型: {forced_document_type}")
  285. logger.debug(f"[JSON转换] Markdown内容长度: {len(markdown_content)} 字符")
  286. investment_record = parse_investment_record(markdown_content, forced_document_type)
  287. if investment_record:
  288. data = investment_record.to_dict()
  289. # 检查返回的数据格式:可能是列表(旧格式)或字典(包含projectInfo的新格式)
  290. if isinstance(data, dict) and "data" in data:
  291. # 新格式:包含 projectInfo 和 data
  292. logger.info(f"[JSON转换] 投资估算解析成功,共 {len(data['data'])} 条记录")
  293. if data.get("projectInfo"):
  294. logger.info(f"[JSON转换] 项目信息: {data['projectInfo'].get('projectName', '')}")
  295. # 输出前3条记录的摘要
  296. if data["data"]:
  297. for idx, item in enumerate(data["data"][:3]):
  298. logger.debug(f"[JSON转换] 记录 {idx+1}: No={item.get('No', '')}, Name={item.get('name', '')}, Level={item.get('Level', '')}")
  299. else:
  300. # 旧格式:直接是数据列表
  301. logger.info(f"[JSON转换] 投资估算解析成功,共 {len(data)} 条记录")
  302. # 输出前3条记录的摘要
  303. if data:
  304. for idx, item in enumerate(data[:3]):
  305. logger.debug(f"[JSON转换] 记录 {idx+1}: No={item.get('No', '')}, Name={item.get('name', '')}, Level={item.get('Level', '')}")
  306. result = {"document_type": forced_document_type, "data": data}
  307. else:
  308. logger.error("[JSON转换] 投资估算解析失败:parse_investment_record 返回 None")
  309. result = {"document_type": forced_document_type, "data": [], "error": "投资估算解析失败"}
  310. elif forced_document_type == "finalAccount":
  311. # 决算报告类型处理
  312. logger.info(f"[JSON转换] 处理决算报告类型: {forced_document_type}")
  313. logger.debug(f"[JSON转换] Markdown内容长度: {len(markdown_content)} 字符")
  314. from .investment_parser import parse_final_account_record
  315. final_account_record = parse_final_account_record(markdown_content)
  316. if final_account_record:
  317. data = final_account_record.to_dict()
  318. logger.info(f"[JSON转换] 决算报告解析成功,共 {len(data)} 条记录")
  319. # 输出前3条记录的摘要
  320. if data:
  321. for idx, item in enumerate(data[:3]):
  322. logger.debug(f"[JSON转换] 记录 {idx+1}: No={item.get('No', '')}, Name={item.get('name', '')}, feeName={item.get('feeName', '')}")
  323. result = {"document_type": forced_document_type, "data": data}
  324. else:
  325. logger.error("[JSON转换] 决算报告解析失败:parse_final_account_record 返回 None")
  326. result = {"document_type": forced_document_type, "data": [], "error": "决算报告解析失败"}
  327. else:
  328. result = {"document_type": forced_document_type, "data": {}}
  329. # 对于forced_document_type,也检查数据完整性
  330. if enable_paddleocr_fallback and result.get("document_type") in ["noiseMonitoringRecord", "electromagneticTestRecord"]:
  331. try:
  332. from ..utils.paddleocr_fallback import check_json_data_completeness
  333. is_complete = check_json_data_completeness(result, result.get("document_type"))
  334. if auto_weather_default and result.get("document_type") == "noiseMonitoringRecord":
  335. logger.warning("[JSON转换] 检测到天气字段使用默认值,尝试使用PaddleOCR备用解析")
  336. is_complete = False
  337. if not is_complete:
  338. logger.warning(f"[JSON转换] 检测到数据缺失,尝试使用PaddleOCR备用解析")
  339. fallback_markdown = fallback_parse_with_paddleocr(
  340. result,
  341. original_markdown,
  342. output_dir=output_dir,
  343. document_type=result.get("document_type"),
  344. input_file=input_file
  345. )
  346. if fallback_markdown:
  347. logger.info("[JSON转换] PaddleOCR备用解析成功,重新解析JSON")
  348. if result.get("document_type") == "noiseMonitoringRecord":
  349. original_data = result.get("data", {}) or {}
  350. fallback_data = parse_noise_detection_record(fallback_markdown, first_page_image=None, output_dir=output_dir).to_dict()
  351. merged_data = _merge_noise_records(
  352. primary=original_data,
  353. secondary=fallback_data,
  354. preserve_primary_noise=True
  355. )
  356. result = {"document_type": "noiseMonitoringRecord", "data": merged_data}
  357. elif result.get("document_type") == "electromagneticTestRecord":
  358. original_data = result.get("data", {}) or {}
  359. fallback_data = parse_electromagnetic_detection_record(fallback_markdown).to_dict()
  360. logger.info(f"[JSON转换] fallback_data project: {repr(fallback_data.get('project'))}, EB1 address: {repr(fallback_data.get('electricMagnetic', [{}])[0].get('address') if fallback_data.get('electricMagnetic') else '')}")
  361. merged_data = _merge_electromagnetic_records(
  362. primary=original_data,
  363. secondary=fallback_data,
  364. preserve_primary_electric_magnetic=True
  365. )
  366. logger.info(f"[JSON转换] merged_data project: {repr(merged_data.get('project'))}, EB1 address: {repr(merged_data.get('electricMagnetic', [{}])[0].get('address') if merged_data.get('electricMagnetic') else '')}")
  367. result = {"document_type": "electromagneticTestRecord", "data": merged_data}
  368. logger.info("[JSON转换] 使用PaddleOCR结果重新解析完成")
  369. except Exception as e:
  370. logger.exception(f"[JSON转换] PaddleOCR备用解析过程出错: {e}")
  371. return result
  372. auto_weather_default = False
  373. doc_type = detect_document_type(markdown_content)
  374. if doc_type == "noiseRec":
  375. # v2版本不依赖OCR,first_page_image参数会被忽略
  376. noise_record = parse_noise_detection_record(markdown_content, first_page_image=None, output_dir=output_dir)
  377. auto_weather_default = getattr(noise_record, "_auto_weather_default_used", False)
  378. data = noise_record.to_dict()
  379. result = {"document_type": doc_type, "data": data}
  380. elif doc_type == "emRec":
  381. data = parse_electromagnetic_detection_record(markdown_content).to_dict()
  382. result = {"document_type": doc_type, "data": data}
  383. elif doc_type in ["fsApproval", "fsReview", "pdApproval", "safetyFsApproval"]:
  384. # 新增:投资估算类型(包括安评类)
  385. logger.info(f"[JSON转换] 检测到投资估算类型: {doc_type}")
  386. logger.debug(f"[JSON转换] Markdown内容长度: {len(markdown_content)} 字符")
  387. investment_record = parse_investment_record(markdown_content, doc_type)
  388. if investment_record:
  389. data = investment_record.to_dict()
  390. logger.info(f"[JSON转换] 投资估算解析成功,共 {len(data)} 条记录")
  391. # 输出前3条记录的摘要
  392. if data:
  393. for idx, item in enumerate(data[:3]):
  394. logger.debug(f"[JSON转换] 记录 {idx+1}: No={item.get('No', '')}, Name={item.get('name', '')}, Level={item.get('Level', '')}")
  395. result = {"document_type": doc_type, "data": data}
  396. else:
  397. logger.error("[JSON转换] 投资估算解析失败:parse_investment_record 返回 None")
  398. result = {"document_type": doc_type, "data": [], "error": "投资估算解析失败"}
  399. else:
  400. result = {"document_type": "unknown", "data": {}, "error": "无法识别的文档类型"}
  401. # 检查数据完整性,如果缺失则使用PaddleOCR备用解析
  402. if enable_paddleocr_fallback and result.get("document_type") != "unknown":
  403. try:
  404. # 检查是否需要备用解析
  405. from ..utils.paddleocr_fallback import check_json_data_completeness
  406. is_complete = check_json_data_completeness(result, result.get("document_type"))
  407. if auto_weather_default and result.get("document_type") in ["noiseMonitoringRecord", "noise_detection"]:
  408. logger.warning("[JSON转换] 检测到天气字段使用默认值,尝试使用PaddleOCR备用解析")
  409. is_complete = False
  410. if not is_complete:
  411. logger.warning(f"[JSON转换] 检测到数据缺失,尝试使用PaddleOCR备用解析")
  412. # 尝试使用PaddleOCR补充
  413. fallback_markdown = fallback_parse_with_paddleocr(
  414. result,
  415. original_markdown,
  416. output_dir=output_dir,
  417. document_type=result.get("document_type"),
  418. input_file=input_file
  419. )
  420. if fallback_markdown:
  421. logger.info("[JSON转换] PaddleOCR备用解析成功,重新解析JSON")
  422. # 使用PaddleOCR的结果重新解析
  423. if result.get("document_type") == "noiseMonitoringRecord" or doc_type == "noise_detection":
  424. original_data = result.get("data", {}) or {}
  425. fallback_data = parse_noise_detection_record(fallback_markdown, first_page_image=None, output_dir=output_dir).to_dict()
  426. merged_data = _merge_noise_records(
  427. primary=original_data,
  428. secondary=fallback_data,
  429. preserve_primary_noise=True
  430. )
  431. result = {"document_type": "noiseMonitoringRecord", "data": merged_data}
  432. elif result.get("document_type") == "electromagneticTestRecord" or doc_type == "electromagnetic_detection":
  433. original_data = result.get("data", {}) or {}
  434. fallback_data = parse_electromagnetic_detection_record(fallback_markdown).to_dict()
  435. merged_data = _merge_electromagnetic_records(
  436. primary=original_data,
  437. secondary=fallback_data,
  438. preserve_primary_electric_magnetic=True
  439. )
  440. result = {"document_type": "electromagneticTestRecord", "data": merged_data}
  441. logger.info("[JSON转换] 使用PaddleOCR结果重新解析完成")
  442. except Exception as e:
  443. logger.exception(f"[JSON转换] PaddleOCR备用解析过程出错: {e}")
  444. # 即使备用解析失败,也返回原始结果
  445. return result