template6.py 4.4 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 CustomTemplate(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. # 验证表头列数(data.json中为11列)
  28. if len(datas[0]) != 11:
  29. print(f"表头数量异常;实际{len(datas[0])}列,预期11列")
  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. '备注', # 第11列:备注
  44. ]
  45. # 逐列验证表头
  46. for i, pattern in enumerate(header_patterns):
  47. if not re.fullmatch(pattern, datas[0][i]):
  48. print(f'表头未验证:位置{i+1},预期{pattern},实际{datas[0][i]}')
  49. return False
  50. # 确保表格有数据行
  51. if len(datas) < 2:
  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. if self.data:
  67. extract_result["项目名称"] = self.data[0][1]
  68. # 提取招标信息
  69. for row in self.data:
  70. # 跳过列数异常的行
  71. if len(row) != 11:
  72. continue
  73. # 解析每行数据(按实际列位置)
  74. seq, subject, max_win_num, package_count, package_num, package_name, \
  75. budget, limited_price, is_special, deposit, remark = row
  76. # 补充金额单位(表头已明确万元)
  77. if budget and not re.search('万元', budget):
  78. budget += '万元'
  79. if limited_price and not re.search('万元', limited_price):
  80. limited_price += '万元'
  81. # 构建招标信息条目
  82. tender_info = {
  83. "标的": subject,
  84. "标包": package_name,
  85. "预算": budget,
  86. "限价": limited_price
  87. }
  88. # 检查招标信息有效性(必须包含预算)
  89. if "预算" in tender_info:
  90. extract_result["招标信息"].append(tender_info)
  91. # 处理无有效招标信息的情况
  92. if not extract_result["招标信息"]:
  93. extract_result["招标信息"] = []
  94. return extract_result
  95. # 使用示例
  96. if __name__ == "__main__":
  97. # 读取数据文件
  98. with open("data.json", "r", encoding="utf8") as f:
  99. preprocessed_data = json.load(f)
  100. # 初始化模板并提取信息
  101. template = CustomTemplate()
  102. if template.check_call_timing(preprocessed_data):
  103. result = template.extract(preprocessed_data)
  104. print("提取结果:", json.dumps(result, ensure_ascii=False, indent=2))
  105. else:
  106. print("当前数据不满足模板调用条件")