template2.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 CustomBudgetTemplate(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. datas = tables[0]
  26. # 验证表头列数(目标表格为5列)
  27. if len(datas[0]) != 5:
  28. print("表头数量异常;", len(datas[0]))
  29. return False
  30. # 表头内容正则匹配
  31. header_patterns = [
  32. '序号', # 第1列:序号
  33. '标的名称', # 第2列:标的名称
  34. '(概算|预算)金额([万亿美欧日]?元)', # 第3列:概算金额
  35. '最高限价([万亿美欧日]?元)', # 第4列:最高限价
  36. '服务期/工期|工期/服务期|工期/交货期|交货期/工期|服务期|工期|交货期' # 第5列:工期
  37. ]
  38. # 逐列验证表头
  39. for i, pattern in enumerate(header_patterns):
  40. if not re.fullmatch(pattern, datas[0][i]):
  41. print('表头未验证: ', pattern, datas[0][i])
  42. return False
  43. # 确保表格有数据行
  44. if len(datas) < 2:
  45. return False
  46. self.header = datas[0]
  47. self.data = datas[1:]
  48. return True
  49. except Exception as e:
  50. print(f"表格验证错误: {e}")
  51. return False
  52. def extract(self, preprocessed_data: Dict[str, Any]) -> Dict[str, Any]:
  53. """提取表格中的招标信息、中标信息、候选人信息"""
  54. extract_result = {
  55. "招标信息": [],
  56. "中标信息": [],
  57. "候选人信息": []
  58. }
  59. for row in self.data:
  60. # 跳过列数异常的行
  61. if len(row) != 5:
  62. continue
  63. # 解析每行数据
  64. seq, subject_name, budget, limited_price, _ = row
  65. # 补充报价单位(如果表头有单位而内容没有)
  66. if re.search('[万亿美欧日]?元', self.header[2]) and not re.search('[万亿美欧日]?元', budget):
  67. budget += re.search('[万亿美欧日]?元', self.header[2]).group(0)
  68. if re.search('[万亿美欧日]?元', self.header[3]) and not re.search('[万亿美欧日]?元', limited_price):
  69. limited_price += re.search('[万亿美欧日]?元', self.header[3]).group(0)
  70. # 提取招标信息(包含预算,符合必填条件)
  71. tender_info = {
  72. "标的": subject_name,
  73. "预算": budget,
  74. "限价": limited_price
  75. }
  76. # 过滤空值字段
  77. tender_info = {k: v for k, v in tender_info.items() if v}
  78. extract_result["招标信息"].append(tender_info)
  79. return extract_result
  80. # 使用示例
  81. if __name__ == "__main__":
  82. # 读取数据文件
  83. with open("data.json", "r", encoding="utf8") as f:
  84. preprocessed_data = json.load(f)
  85. # 初始化模板并提取信息
  86. template = CustomBudgetTemplate()
  87. if template.check_call_timing(preprocessed_data):
  88. result = template.extract(preprocessed_data)
  89. print("提取结果:", json.dumps(result, ensure_ascii=False, indent=2))
  90. else:
  91. print("当前数据不满足模板调用条件")