template3.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 AdaptedBudgetTemplate(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. return False
  24. try:
  25. # 取第一个表格进行验证
  26. datas = tables[0]
  27. # 验证表头列数(目标表格为10列)
  28. if len(datas[0]) != 10:
  29. print(f"表头数量异常,期望10列,实际{len(datas[0])}列")
  30. return False
  31. # 表头内容正则匹配(泛化类似表达)
  32. header_patterns = [
  33. '序号', # 第1列:序号
  34. '标的(名称)?', # 第2列:标的
  35. '(预算|概算|招标|标包)?金额(([万亿美欧日]?元))?', # 第3列:金额相关
  36. '最大中标数量', # 第4列:中标数量
  37. '标包号|包号|标段号|标包名称', # 第5列:标包号/名称
  38. '(标包|招标|概算)?金额(([万亿美欧日]?元))?', # 第6列:金额相关
  39. '(最高|最高限|拦标)价(([万亿美欧日]?元))?', # 第7列:限价相关
  40. '是否特殊包', # 第8列:特殊包标识
  41. '招标文件(收取)?费用(([万亿美欧日]?元))?', # 第9列:文件费用
  42. '投标保证金(([万亿美欧日]?元))?' # 第10列:保证金
  43. ]
  44. # 逐列验证表头
  45. for i, pattern in enumerate(header_patterns):
  46. if not re.fullmatch(pattern, datas[0][i]):
  47. print(f'表头第{i+1}列未匹配:{pattern} vs {datas[0][i]}')
  48. return False
  49. # 确保表格有数据行
  50. if len(datas) < 2:
  51. print("表格无数据行")
  52. return False
  53. self.header = datas[0]
  54. self.data = datas[1:]
  55. return True
  56. except Exception as e:
  57. print(f"表格验证错误: {e}")
  58. return False
  59. def extract(self, preprocessed_data: Dict[str, Any]) -> Dict[str, Any]:
  60. """提取表格中的招标信息、中标信息、候选人信息"""
  61. extract_result = {
  62. "招标信息": [],
  63. "中标信息": [],
  64. "候选人信息": []
  65. }
  66. for row in self.data:
  67. # 跳过列数异常的行
  68. if len(row) != 10:
  69. continue
  70. # 解析每行数据(10列结构)
  71. seq, subject, budget, quantity, package_num, package_budget, limited_price, is_special, doc_fee, deposit = row
  72. # 补充金额单位
  73. # 处理预算金额单位
  74. if re.search('[万亿美欧日]?元', self.header[2]) and not re.search('[万亿美欧日]?元', budget):
  75. budget += re.search('[万亿美欧日]?元', self.header[2]).group(0)
  76. # 处理标包金额单位
  77. if re.search('[万亿美欧日]?元', self.header[5]) and not re.search('[万亿美欧日]?元', package_budget):
  78. package_budget += re.search('[万亿美欧日]?元', self.header[5]).group(0)
  79. # 处理最高限价单位
  80. if re.search('[万亿美欧日]?元', self.header[6]) and not re.search('[万亿美欧日]?元', limited_price):
  81. limited_price += re.search('[万亿美欧日]?元', self.header[6]).group(0)
  82. # 整理招标信息(必须包含预算,否则置空)
  83. tender_info = {
  84. "标的": subject,
  85. "标包": package_num, # 此处标包名称对应第5列
  86. "包号": package_num,
  87. "预算": package_budget or budget, # 优先取第6列标包金额,无则用第3列预算
  88. "限价": limited_price
  89. }
  90. # 验证招标信息必填项
  91. if tender_info.get("预算"):
  92. extract_result["招标信息"].append(tender_info)
  93. else:
  94. print("招标信息缺少预算,不添加")
  95. # 中标信息和候选人信息无对应数据,不添加
  96. return extract_result
  97. # 使用示例
  98. if __name__ == "__main__":
  99. # 读取并解析数据文件
  100. with open("data.json", "r", encoding="utf8") as f:
  101. preprocessed_data = json.load(f)
  102. # 初始化模板并提取信息
  103. template = AdaptedBudgetTemplate()
  104. if template.check_call_timing(preprocessed_data):
  105. result = template.extract(preprocessed_data)
  106. print("提取结果:", json.dumps(result, ensure_ascii=False, indent=2))
  107. else:
  108. print("当前数据不满足模板调用条件")