template1.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # coding:utf8
  2. import re
  3. from typing import Dict, Any
  4. from BiddingKG.dl.template_extract.abstract_template import AbstractTemplate, table2list
  5. import os
  6. import json
  7. class AdaptedPurchaseTemplate(AbstractTemplate):
  8. """
  9. 适配data.json表格结构的信息提取模板
  10. 提取项目信息、招标信息、中标信息、候选人信息及产品信息
  11. """
  12. def __init__(self):
  13. super().__init__(
  14. template_id=os.path.abspath(__file__),
  15. priority=3
  16. )
  17. def check_call_timing(self, preprocessed_data: Dict[str, Any]) -> bool:
  18. """检查是否符合模板调用条件"""
  19. if "表格列表" not in preprocessed_data:
  20. return False
  21. tables = preprocessed_data["表格列表"]
  22. if len(tables) < 1:
  23. print("无表格数据")
  24. return False
  25. try:
  26. # 验证供应商表格表头(主要数据表格)
  27. target_header_patterns = [
  28. r'序号', # 第1列:序号
  29. r'供应商名称|供应商', # 第2列:供应商名称
  30. r'候选供应商|中标候选人|投标人类型', # 第3列:候选状态
  31. r'已报物料|投标内容|标的', # 第4列:已报物料
  32. r'交货时间|投标时间|有效期' # 第5列:时间信息
  33. ]
  34. # 查找符合条件的主要表格
  35. self.main_tables = []
  36. for table in tables:
  37. if len(table) < 1:
  38. continue
  39. header = table[0]
  40. if len(header) != 5:
  41. continue
  42. match = True
  43. for i, pattern in enumerate(target_header_patterns):
  44. if not re.search(pattern, header[i]): # 使用search增强匹配灵活性
  45. match = False
  46. break
  47. if match and len(table) >= 2: # 确保有数据行
  48. self.main_tables.append(table)
  49. # 至少需要一个主要表格
  50. return len(self.main_tables) > 0
  51. except Exception as e:
  52. print(f"表格验证错误: {e}")
  53. return False
  54. def extract(self, preprocessed_data: Dict[str, Any]) -> Dict[str, Any]:
  55. """提取表格中的相关信息及字段"""
  56. extract_result = {
  57. "招标信息": [],
  58. "中标信息": [],
  59. "候选人信息": [],
  60. "产品信息": []
  61. }
  62. # 处理主要表格数据
  63. for table in self.main_tables:
  64. header = table[0]
  65. for row in table[1:]: # 跳过表头行
  66. if len(row) != 5:
  67. continue # 跳过列数异常的行
  68. 序号, 供应商名称, 候选状态, 已报物料, 交货时间 = [cell.strip() for cell in row]
  69. # 处理中标信息(仅预成交供应商作为中标人)
  70. if re.fullmatch(r'预成交供应商', 候选状态):
  71. if 供应商名称:
  72. extract_result["中标信息"].append({"中标人": 供应商名称})
  73. # 处理候选人信息(明确排除备选供应商,此处逻辑可根据实际需求扩展其他候选人类型)
  74. # 按需求:备选供应商不作为候选人,故不添加相关逻辑
  75. # 过滤不符合条件的字段
  76. # 招标信息必须包含招标人或预算
  77. if not extract_result["招标信息"] or all(
  78. not (info.get("招标人") or info.get("预算"))
  79. for info in extract_result["招标信息"]
  80. ):
  81. extract_result["招标信息"] = []
  82. # 中标信息必须包含中标人
  83. extract_result["中标信息"] = [
  84. info for info in extract_result["中标信息"]
  85. if info.get("中标人")
  86. ]
  87. # 候选人信息必须包含候选人和排名(当前逻辑下候选人信息应为空)
  88. extract_result["候选人信息"] = [
  89. info for info in extract_result["候选人信息"]
  90. if info.get("候选人") and info.get("排名")
  91. ]
  92. # 有候选人信息则清空中标信息(当前逻辑下不影响)
  93. if extract_result["候选人信息"]:
  94. extract_result["中标信息"] = []
  95. # 移除无数据的字段
  96. if not extract_result["招标信息"]:
  97. del extract_result["招标信息"]
  98. if not extract_result["中标信息"]:
  99. del extract_result["中标信息"]
  100. if not extract_result["候选人信息"]:
  101. del extract_result["候选人信息"]
  102. if not extract_result["产品信息"]:
  103. del extract_result["产品信息"]
  104. return extract_result
  105. # 使用示例
  106. if __name__ == "__main__":
  107. # 读取数据文件
  108. with open("data.json", "r", encoding="utf8") as f:
  109. preprocessed_data = json.load(f)
  110. # 初始化模板并提取信息
  111. template = AdaptedPurchaseTemplate()
  112. if template.check_call_timing(preprocessed_data):
  113. result = template.extract(preprocessed_data)
  114. print("提取结果:", json.dumps(result, ensure_ascii=False, indent=2))
  115. else:
  116. print("当前数据不满足模板调用条件")