| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- # coding:utf8
- import re
- from typing import Dict, Any
- from BiddingKG.dl.template_extract.abstract_template import AbstractTemplate, table2list
- import os
- import json
- class AdaptedBudgetTemplate(AbstractTemplate):
- """
- 招标信息提取模板(适配data.json中第一个表格结构)
- 适配包含标的、预算金额、标包信息等列的招标表格提取
- """
- def __init__(self):
- super().__init__(
- template_id=os.path.abspath(__file__),
- priority=3
- )
- def check_call_timing(self, preprocessed_data: Dict[str, Any]) -> bool:
- """检查是否符合模板调用条件"""
- if "表格列表" not in preprocessed_data:
- return False
- tables = preprocessed_data["表格列表"]
- if len(tables) < 1:
- return False
- try:
- # 取第一个表格进行验证
- datas = tables[0]
- # 验证表头列数(目标表格为10列)
- if len(datas[0]) != 10:
- print(f"表头数量异常,期望10列,实际{len(datas[0])}列")
- return False
- # 表头内容正则匹配(泛化类似表达)
- header_patterns = [
- '序号', # 第1列:序号
- '标的(名称)?', # 第2列:标的
- '(预算|概算|招标|标包)?金额(([万亿美欧日]?元))?', # 第3列:金额相关
- '最大中标数量', # 第4列:中标数量
- '标包号|包号|标段号|标包名称', # 第5列:标包号/名称
- '(标包|招标|概算)?金额(([万亿美欧日]?元))?', # 第6列:金额相关
- '(最高|最高限|拦标)价(([万亿美欧日]?元))?', # 第7列:限价相关
- '是否特殊包', # 第8列:特殊包标识
- '招标文件(收取)?费用(([万亿美欧日]?元))?', # 第9列:文件费用
- '投标保证金(([万亿美欧日]?元))?' # 第10列:保证金
- ]
- # 逐列验证表头
- for i, pattern in enumerate(header_patterns):
- if not re.fullmatch(pattern, datas[0][i]):
- print(f'表头第{i+1}列未匹配:{pattern} vs {datas[0][i]}')
- return False
- # 确保表格有数据行
- if len(datas) < 2:
- print("表格无数据行")
- return False
- self.header = datas[0]
- self.data = datas[1:]
- return True
- except Exception as e:
- print(f"表格验证错误: {e}")
- return False
- def extract(self, preprocessed_data: Dict[str, Any]) -> Dict[str, Any]:
- """提取表格中的招标信息、中标信息、候选人信息"""
- extract_result = {
- "招标信息": [],
- "中标信息": [],
- "候选人信息": []
- }
- for row in self.data:
- # 跳过列数异常的行
- if len(row) != 10:
- continue
- # 解析每行数据(10列结构)
- seq, subject, budget, quantity, package_num, package_budget, limited_price, is_special, doc_fee, deposit = row
- # 补充金额单位
- # 处理预算金额单位
- if re.search('[万亿美欧日]?元', self.header[2]) and not re.search('[万亿美欧日]?元', budget):
- budget += re.search('[万亿美欧日]?元', self.header[2]).group(0)
- # 处理标包金额单位
- if re.search('[万亿美欧日]?元', self.header[5]) and not re.search('[万亿美欧日]?元', package_budget):
- package_budget += re.search('[万亿美欧日]?元', self.header[5]).group(0)
- # 处理最高限价单位
- if re.search('[万亿美欧日]?元', self.header[6]) and not re.search('[万亿美欧日]?元', limited_price):
- limited_price += re.search('[万亿美欧日]?元', self.header[6]).group(0)
- # 整理招标信息(必须包含预算,否则置空)
- tender_info = {
- "标的": subject,
- "标包": package_num, # 此处标包名称对应第5列
- "包号": package_num,
- "预算": package_budget or budget, # 优先取第6列标包金额,无则用第3列预算
- "限价": limited_price
- }
- # 验证招标信息必填项
- if tender_info.get("预算"):
- extract_result["招标信息"].append(tender_info)
- else:
- print("招标信息缺少预算,不添加")
- # 中标信息和候选人信息无对应数据,不添加
- return extract_result
- # 使用示例
- if __name__ == "__main__":
- # 读取并解析数据文件
- with open("data.json", "r", encoding="utf8") as f:
- preprocessed_data = json.load(f)
- # 初始化模板并提取信息
- template = AdaptedBudgetTemplate()
- if template.check_call_timing(preprocessed_data):
- result = template.extract(preprocessed_data)
- print("提取结果:", json.dumps(result, ensure_ascii=False, indent=2))
- else:
- print("当前数据不满足模板调用条件")
|