template23.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 CustomDealInfoTemplate(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. # 验证表头列数(目标表格为6列)
  28. if len(datas[0]) != 6:
  29. print(f"表头数量异常:{len(datas[0])},预期6列")
  30. return False
  31. # 表头内容正则匹配(按实际位置)
  32. header_patterns = [
  33. '序号', # 第1列:序号
  34. '品类(名称)?', # 第2列:品类名称(泛化匹配)
  35. '标的(名称)?', # 第3列:标的名称(泛化匹配)
  36. '标包|标段||包名称', # 第4列:标包(泛化匹配)
  37. '(中标|成交|中选)(人|单位|供应商)(名称)?', # 第5列:中标人名称(泛化匹配)
  38. '(中标|成交|中选)比例((%))?' # 第6列:中标比例(带百分比)
  39. ]
  40. # 逐列验证表头
  41. for i, pattern in enumerate(header_patterns):
  42. if not re.fullmatch(pattern, datas[0][i]):
  43. print(f'表头不匹配:模式[{pattern}] 实际[{datas[0][i]}]')
  44. return False
  45. # 确保表格有数据行
  46. if len(datas) < 2:
  47. print("表格无数据行")
  48. return False
  49. self.header = datas[0]
  50. self.data = datas[1:]
  51. return True
  52. except Exception as e:
  53. print(f"表格验证错误: {e}")
  54. return False
  55. def extract(self, preprocessed_data: Dict[str, Any]) -> Dict[str, Any]:
  56. """提取表格中的相关信息"""
  57. extract_result = {
  58. "项目编号": "",
  59. "项目名称": "",
  60. "招标信息": [],
  61. "中标信息": [],
  62. "候选人信息": [],
  63. "产品信息": []
  64. }
  65. # 招标信息:因无招标人或预算信息,保持空列表
  66. # 候选人信息:因无候选人及排名信息,保持空列表
  67. for row in self.data:
  68. # 跳过列数异常的行
  69. if len(row) != 6:
  70. continue
  71. # 解析每行数据(按实际列位置)
  72. seq, category, subject, package, winner, winning_ratio = row
  73. package = subject + package if category != subject else package # 675385849 品类、标的相同;676248419 品类、标的不同
  74. # 中标信息:必须包含中标人
  75. if winner:
  76. winner_info = {
  77. "标的": category,
  78. "标包": package,
  79. "中标人": winner,
  80. # "中标价": f"{winning_ratio}%" # 补充单位%
  81. }
  82. # 过滤空值字段
  83. winner_info = {k: v for k, v in winner_info.items() if v}
  84. extract_result["中标信息"].append(winner_info)
  85. return extract_result
  86. # 使用示例
  87. if __name__ == "__main__":
  88. # 读取并解析数据文件
  89. with open("data.json", "r", encoding="utf8") as f:
  90. preprocessed_data = json.load(f)
  91. # 初始化模板并提取信息
  92. template = CustomDealInfoTemplate()
  93. if template.check_call_timing(preprocessed_data):
  94. result = template.extract(preprocessed_data)
  95. print("提取结果:", json.dumps(result, ensure_ascii=False, indent=2))
  96. else:
  97. print("当前数据不满足模板调用条件")