template5.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 PowerBidTemplate(AbstractTemplate):
  8. """
  9. 电力信息提取模板(适配电力行业招标表格)
  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. datas = tables[0]
  26. # 验证表头列数(目标表格为11列)
  27. if len(datas[0]) != 11:
  28. print("表头数量异常;", len(datas[0]))
  29. return False
  30. # 表头内容正则匹配(按顺序对应,泛化表达)
  31. header_patterns = [
  32. '序号', # 第1列:序号
  33. '标的(名称)?', # 第2列:标的名称
  34. '标的(预计|预估)?(采购|招标)?金额((|\\()?[万亿]?元()|\\))?', # 第3列:标的预计采购金额
  35. '(建设|招标|采购)单位(名称)?', # 第4列:建设单位(招标人)
  36. '(标段|标包)(序号|编号|号)', # 第5列:标段序号
  37. '(标段|标包)(名称)', # 第6列:标段名称
  38. '(标段|标包)?(预计|预估)?(采购|招标)?金额((|\\()?[万亿]?元()|\\))?', # 第7列:标段预计采购金额
  39. '(最高|最高投标|拦标)限价((|\\()?[万亿]?元()|\\))?', # 第8列:最高投标限价
  40. '最大中标数量', # 第9列:最大中标数量
  41. '是否特殊包', # 第10列:是否特殊包
  42. '投标保证金((|\\()?[万亿]?元()|\\))?', # 第11列:投标保证金
  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. return False
  52. self.header = datas[0]
  53. self.data = datas[1:]
  54. return True
  55. except Exception as e:
  56. print(f"表格验证错误: {e}")
  57. return False
  58. def extract(self, preprocessed_data: Dict[str, Any]) -> Dict[str, Any]:
  59. """提取表格中的招标信息、中标信息、候选人信息"""
  60. extract_result = {
  61. "招标信息": [],
  62. "中标信息": [],
  63. "候选人信息": []
  64. }
  65. for row in self.data:
  66. # 跳过列数异常的行
  67. if len(row) != 11:
  68. continue
  69. # 解析每行数据(按列顺序对应)
  70. (seq, subject, subject_budget, builder, section_no,
  71. section_name, section_budget, limit_price, _, _, _) = row
  72. # 补充金额单位(从对应表头提取)
  73. # 标的预算单位补充
  74. if re.search('([万亿]?元)', self.header[2]) and not re.search('([万亿]?元)', subject_budget):
  75. unit = re.search('([万亿]?元)', self.header[2]).group(1)
  76. subject_budget += unit
  77. # 标段预算单位补充
  78. if re.search('([万亿]?元)', self.header[6]) and not re.search('([万亿]?元)', section_budget):
  79. unit = re.search('([万亿]?元)', self.header[6]).group(1)
  80. section_budget += unit
  81. # 最高限价单位补充
  82. if re.search('([万亿]?元)', self.header[7]) and not re.search('([万亿]?元)', limit_price):
  83. unit = re.search('([万亿]?元)', self.header[7]).group(1)
  84. # 处理"/"等无数据标识
  85. limit_price = limit_price if limit_price != "/" else ""
  86. if limit_price:
  87. limit_price += unit
  88. # 整理招标信息(取标段预算作为主要预算)
  89. tender_info = {
  90. "标的": subject.strip(),
  91. "标包": section_name.strip(),
  92. "包号": section_no.strip(),
  93. "招标人": builder.strip(),
  94. "预算": section_budget.strip()
  95. }
  96. # 补充限价(非空时)
  97. if limit_price and limit_price != "/":
  98. tender_info["限价"] = limit_price.strip()
  99. # 验证招标信息有效性(包含招标人或预算)
  100. if tender_info["招标人"] or tender_info["预算"]:
  101. extract_result["招标信息"].append(tender_info)
  102. return extract_result
  103. # 使用示例
  104. if __name__ == "__main__":
  105. from lxml import etree
  106. # 读取并解析数据文件
  107. with open("data.json", "r", encoding="utf8") as f:
  108. preprocessed_data = json.load(f)
  109. # 初始化模板并提取信息
  110. template = PowerBidTemplate()
  111. if template.check_call_timing(preprocessed_data):
  112. result = template.extract(preprocessed_data)
  113. print("提取结果:", json.dumps(result, ensure_ascii=False, indent=2))
  114. else:
  115. print("当前数据不满足模板调用条件")