test_completeness_integration.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. 完整性审查集成测试
  5. 测试流程:
  6. 1. 加载现有的 final_result.json 文件
  7. 2. 模拟三级分类结果(添加 tertiary_classification_details)
  8. 3. 运行完整性审查
  9. 4. 验证结果
  10. """
  11. import asyncio
  12. import json
  13. import sys
  14. from pathlib import Path
  15. # 添加项目路径
  16. project_root = Path(__file__).parent.parent.parent
  17. sys.path.insert(0, str(project_root))
  18. from core.construction_review.component.reviewers.completeness_reviewer import (
  19. LightweightCompletenessChecker,
  20. TertiarySpecLoader
  21. )
  22. def load_final_result(file_path: str) -> dict:
  23. """加载最终结果文件"""
  24. with open(file_path, 'r', encoding='utf-8') as f:
  25. return json.load(f)
  26. def simulate_tertiary_classification(chunks: list) -> list:
  27. """
  28. 模拟三级分类结果
  29. 为每个二级分类下的 chunk 添加 tertiary_classification_details
  30. """
  31. # 三级分类标准映射(基于 StandardCategoryTable.csv)
  32. tertiary_mapping = {
  33. # 编制依据
  34. "LawsAndRegulations": [
  35. {"third_category_code": "NationalLawsAndRegulations", "third_category_name": "国家政府发布的法律法规与规章制度"},
  36. {"third_category_code": "ProvincialLawsAndRegulationsOfProjectLocation", "third_category_name": "工程所在地省级政府发布的法律法规与规章制度"}
  37. ],
  38. "StandardsAndSpecifications": [
  39. {"third_category_code": "IndustryStandards", "third_category_name": "行业标准"},
  40. {"third_category_code": "TechnicalRegulations", "third_category_name": "技术规程"}
  41. ],
  42. "DocumentSystems": [
  43. {"third_category_code": "SichuanRoadAndBridgeDocumentSystemsAndmanagementProcedures", "third_category_name": "四川路桥下发的文件制度和管理程序文件"},
  44. {"third_category_code": "RoadAndBridgeGroupDocumentSystemsAndmanagementProcedures", "third_category_name": "路桥集团下发的文件制度和管理程序文件"},
  45. {"third_category_code": "BridgeCompanyDocumentSystemsAndmanagementProcedures", "third_category_name": "桥梁公司下发的文件制度和管理程序文件"},
  46. {"third_category_code": "ConstructionUnitDocumentSystemsAndmanagementProcedures", "third_category_name": "建设单位下发的文件制度和管理程序文件"}
  47. ],
  48. "CompilationPrinciples": [
  49. {"third_category_code": "NationalPoliciesStandardsAndDesignDocument", "third_category_name": "国家方针、政策、标准和设计文件"},
  50. {"third_category_code": "BasicConstructionProcedures", "third_category_name": "基本建设程序"},
  51. {"third_category_code": "ProjectFunctionImplementation", "third_category_name": "工程项目功能实现"},
  52. {"third_category_code": "ContractPerformance", "third_category_name": "合同履约"},
  53. {"third_category_code": "ConstructionForceConcentration", "third_category_name": "施工力量集中"},
  54. {"third_category_code": "ProcessControl", "third_category_name": "工序控制"}
  55. ],
  56. "CompilationScope": [
  57. {"third_category_code": "ProjectCoverage", "third_category_name": "填写完整涵盖本方案包含的所有工程"},
  58. {"third_category_code": "ConstructionTechnology", "third_category_name": "部分工程可简要说明采取的施工工艺"}
  59. ],
  60. # 工程概况
  61. "DesignSummary": [
  62. {"third_category_code": "ProjectIntroduction", "third_category_name": "工程简介"},
  63. {"third_category_code": "MainTechnicalStandards", "third_category_name": "主要技术标准"}
  64. ],
  65. "GeologyWeather": [
  66. {"third_category_code": "HydrologicalConditions", "third_category_name": "水文状况"},
  67. {"third_category_code": "ClimaticConditions", "third_category_name": "气候条件"}
  68. ],
  69. "Surroundings": [
  70. {"third_category_code": "PositionalRelationship", "third_category_name": "位置关系"},
  71. {"third_category_code": "StructuralDimensions", "third_category_name": "结构尺寸"}
  72. ],
  73. "LayoutPlan": [
  74. {"third_category_code": "TemporaryFacilityLocation", "third_category_name": "临时设施位置"},
  75. {"third_category_code": "ConstructionWorkPlatform", "third_category_name": "施工作业平台与便道参数"},
  76. {"third_category_code": "TemporaryWaterAndElectricityArrangement", "third_category_name": "临时水电布置"}
  77. ],
  78. "RequirementsTech": [
  79. {"third_category_code": "DurationTarget", "third_category_name": "工期目标"},
  80. {"third_category_code": "QualityTarget", "third_category_name": "质量目标"},
  81. {"third_category_code": "SecurityGoals", "third_category_name": "安全目标"},
  82. {"third_category_code": "EnvironmentalGoals", "third_category_name": "环境目标"}
  83. ],
  84. "RiskLevel": [
  85. {"third_category_code": "DangerSource", "third_category_name": "危险源"},
  86. {"third_category_code": "ClassificationAndResponseMeasures", "third_category_name": "分级与应对措施"}
  87. ],
  88. "Stakeholders": [
  89. {"third_category_code": "UnitType", "third_category_name": "单位类型"}
  90. ],
  91. # 施工计划
  92. "Schedule": [
  93. {"third_category_code": "ProcessOperationTimeAnalysis", "third_category_name": "工序作业时间分析"},
  94. {"third_category_code": "KeyProjectNodeArrangement", "third_category_name": "关键工程(工序)节点安排"},
  95. {"third_category_code": "ConstructionScheduleGanttChart", "third_category_name": "施工进度计划横道图等"}
  96. ],
  97. "Materials": [
  98. {"third_category_code": "ListOfConstructionMeasuresAndMaterials", "third_category_name": "施工措施材料清单"}
  99. ],
  100. "Equipment": [
  101. {"third_category_code": "MainConstructionMachineryAndEquipment", "third_category_name": "主要施工机械设备"}
  102. ],
  103. "Workforce": [
  104. {"third_category_code": "WorkforceAllocationPlan", "third_category_name": "劳动力配置计划"},
  105. {"third_category_code": "StageLaborDemand", "third_category_name": "阶段劳动力需求"}
  106. ],
  107. "SafetyCost": [
  108. {"third_category_code": "CategoryOfSafetyProductionExpenses", "third_category_name": "安全生产费用类别"},
  109. {"third_category_code": "SecurityFeeName", "third_category_name": "安全费用名称"},
  110. {"third_category_code": "SingleInvestmentAmount", "third_category_name": "单项投入金额"},
  111. {"third_category_code": "TotalSafetyProductionExpenses", "third_category_name": "安全生产费用总额"}
  112. ],
  113. # 施工工艺技术
  114. "MethodsOverview": [
  115. {"third_category_code": "ConstructionTechnologySelection", "third_category_name": "施工工艺选择"},
  116. {"third_category_code": "MainConstructionMethods", "third_category_name": "主要施工方法"},
  117. {"third_category_code": "TemplateConfigurationQuantity", "third_category_name": "模板配置数量"}
  118. ],
  119. "TechParams": [
  120. {"third_category_code": "MaterialType", "third_category_name": "材料类型"},
  121. {"third_category_code": "MaterialSpecifications", "third_category_name": "材料规格"},
  122. {"third_category_code": "DeviceName", "third_category_name": "设备名称"},
  123. {"third_category_code": "DeviceModel", "third_category_name": "设备型号"},
  124. {"third_category_code": "EquipmentManufacturingTime", "third_category_name": "设备出厂时间"},
  125. {"third_category_code": "EquipmentPerformanceParameters", "third_category_name": "设备性能参数"},
  126. {"third_category_code": "EquipmentWeight", "third_category_name": "设备自重"}
  127. ],
  128. "Process": [
  129. {"third_category_code": "ConstructionProcess", "third_category_name": "施工工序"},
  130. {"third_category_code": "ProcessSequence", "third_category_name": "工艺顺序"},
  131. {"third_category_code": "ProcessFlowDiagram", "third_category_name": "工艺流程框图"}
  132. ],
  133. "PrepWork": [
  134. {"third_category_code": "MeasurementAndStakeout", "third_category_name": "测量放样"},
  135. {"third_category_code": "TemporaryWaterAndElectricityConsumption", "third_category_name": "临时水电用量"},
  136. {"third_category_code": "TheSiteIsFlat", "third_category_name": "场地平整"},
  137. {"third_category_code": "Staffing", "third_category_name": "人员配置"},
  138. {"third_category_code": "EquipmentEntry", "third_category_name": "设备进场"},
  139. {"third_category_code": "SafetyProtectionFacilities", "third_category_name": "安全防护措施"},
  140. {"third_category_code": "PersonnelAccess", "third_category_name": "人员上下通道"}
  141. ],
  142. "Operations": [
  143. {"third_category_code": "ConstructionProcessOperations", "third_category_name": "施工工序描述操作"},
  144. {"third_category_code": "ConstructionPoints", "third_category_name": "施工要点"},
  145. {"third_category_code": "FAQPrevention", "third_category_name": "常见问题及预防"},
  146. {"third_category_code": "ProblemSolvingMeasures", "third_category_name": "问题处理措施"}
  147. ],
  148. "Inspection": [
  149. {"third_category_code": "MaterialInspectionUponArrival", "third_category_name": "材料进场质量检验"},
  150. {"third_category_code": "RandomInspectionOfIncomingComponents", "third_category_name": "构配件进场质量抽查"},
  151. {"third_category_code": "ProcessInspectionContent", "third_category_name": "工序检查内容"},
  152. {"third_category_code": "ProcessInspectionStandards", "third_category_name": "工序检查标准"}
  153. ]
  154. }
  155. # 按二级分类分组
  156. secondary_groups = {}
  157. for chunk in chunks:
  158. sec_code = chunk.get("secondary_category_code", "")
  159. if sec_code and sec_code not in ("none", "None", ""):
  160. if sec_code not in secondary_groups:
  161. secondary_groups[sec_code] = []
  162. secondary_groups[sec_code].append(chunk)
  163. # 为每个二级分类添加三级分类详情
  164. updated_chunks = []
  165. for sec_code, group_chunks in secondary_groups.items():
  166. # 获取该二级分类对应的三级分类列表
  167. tertiary_list = tertiary_mapping.get(sec_code, [])
  168. for chunk in group_chunks:
  169. # 模拟三级分类结果
  170. chunk["tertiary_classification_details"] = tertiary_list
  171. # 设置第一个三级分类为主分类(向后兼容)
  172. if tertiary_list:
  173. chunk["tertiary_category_code"] = tertiary_list[0]["third_category_code"]
  174. chunk["tertiary_category_cn"] = tertiary_list[0]["third_category_name"]
  175. updated_chunks.append(chunk)
  176. return updated_chunks
  177. async def test_completeness_check():
  178. """测试完整性审查"""
  179. print("=" * 60)
  180. print("完整性审查集成测试")
  181. print("=" * 60)
  182. # 1. 加载测试数据
  183. test_file = project_root / "temp/construction_review/final_result/4148f6019f89e061b15679666f646893-1773993108.json"
  184. if not test_file.exists():
  185. print(f"错误: 测试文件不存在: {test_file}")
  186. return
  187. print(f"\n1. 加载测试数据: {test_file.name}")
  188. data = load_final_result(str(test_file))
  189. chunks = data.get('document_result', {}).get('structured_content', {}).get('chunks', [])
  190. print(f" 原始 chunks 数量: {len(chunks)}")
  191. # 2. 检查原始数据
  192. print("\n2. 检查原始数据结构:")
  193. sample_chunk = chunks[0] if chunks else {}
  194. print(f" chunk keys: {list(sample_chunk.keys())}")
  195. print(f" 有 tertiary_classification_details: {'tertiary_classification_details' in sample_chunk}")
  196. # 3. 模拟三级分类结果
  197. print("\n3. 模拟三级分类结果...")
  198. chunks_with_tertiary = simulate_tertiary_classification(chunks)
  199. # 统计三级分类情况
  200. tertiary_counts = {}
  201. for chunk in chunks_with_tertiary:
  202. sec_code = chunk.get("secondary_category_code", "")
  203. details = chunk.get("tertiary_classification_details", [])
  204. if sec_code:
  205. tertiary_counts[sec_code] = len(details)
  206. print(f" 已添加三级分类详情的 chunks: {len(chunks_with_tertiary)}")
  207. print(f"\n 各二级分类的三级分类数量:")
  208. for sec_code, count in sorted(tertiary_counts.items()):
  209. print(f" {sec_code}: {count} 个三级分类")
  210. # 4. 运行完整性审查
  211. print("\n4. 运行完整性审查...")
  212. csv_path = str(project_root / "core/construction_review/component/doc_worker/config/StandardCategoryTable.csv")
  213. checker = LightweightCompletenessChecker(csv_path)
  214. result = await checker.check(
  215. chunks=chunks_with_tertiary,
  216. outline=None,
  217. chapter_classification="basis" # 只测试编制依据章节
  218. )
  219. # 5. 输出结果
  220. print("\n5. 审查结果:")
  221. print(f" 总体状态: {result.overall_status}")
  222. # 检查三级完整性
  223. tertiary_result = result.tertiary_completeness
  224. print(f"\n 三级完整性:")
  225. print(f" 总数: {tertiary_result.get('total', 0)}")
  226. print(f" 已有: {tertiary_result.get('present', 0)}")
  227. print(f" 缺失: {tertiary_result.get('missing', 0)}")
  228. print(f" 完整率: {tertiary_result.get('completeness_rate', '0%')}")
  229. # 显示缺失详情
  230. missing_details = tertiary_result.get('missing_details', [])
  231. if missing_details:
  232. print(f"\n 缺失的三级分类 ({len(missing_details)} 个):")
  233. for item in missing_details[:10]: # 只显示前10个
  234. print(f" - {item.get('secondary_name', '')} > {item.get('tertiary_name', '')}")
  235. else:
  236. print(f"\n [OK] 无缺失的三级分类!")
  237. # 6. 验证编制原则
  238. print("\n6. 验证 '编制原则' 二级分类:")
  239. # 查找编制原则相关的 chunks
  240. principle_chunks = [c for c in chunks_with_tertiary
  241. if "CompilationPrinciples" in c.get("secondary_category_code", "")]
  242. if principle_chunks:
  243. chunk = principle_chunks[0]
  244. details = chunk.get("tertiary_classification_details", [])
  245. print(f" 找到编制原则 chunk")
  246. print(f" 三级分类详情数量: {len(details)}")
  247. print(f" 三级分类列表:")
  248. for d in details:
  249. print(f" - {d.get('third_category_code')}: {d.get('third_category_name')}")
  250. # 检查完整性审查是否识别到这些三级分类
  251. actual_tertiary = set()
  252. for item in tertiary_result.get('secondary_stats', []):
  253. if item.get('secondary_code') == 'CompilationPrinciples':
  254. print(f"\n 完整性审查统计:")
  255. print(f" 总数: {item.get('total_tertiary', 0)}")
  256. print(f" 已有: {item.get('present', 0)}")
  257. print(f" 缺失: {item.get('missing', 0)}")
  258. else:
  259. print(" 未找到编制原则相关的 chunks")
  260. print("\n" + "=" * 60)
  261. print("测试完成")
  262. print("=" * 60)
  263. if __name__ == "__main__":
  264. asyncio.run(test_completeness_check())