import json import re from functools import cmp_to_key from flask_app.general.投标人须知正文提取指定内容 import process_nested_data, transform_json, get_requirements_with_gpt,concatenate_keys_values #提取两个大标题之间的内容 def extract_between_sections(data, target_values): target_found = False extracted_data = {} current_section_title = "" section_pattern = re.compile(r'^[一二三四五六七八九十]+$') # 匹配 "一", "二", "三" 等大标题 current_block = {} # 遍历所有键值对 for key, value in data.items(): # 只匹配形如 "一": "竞争性磋商响应文件" 的章节标题 if section_pattern.match(key): if target_found: # 如果已经找到了符合的章节,并且遇到了另一个章节 # 保存当前块并重置 if current_block: extracted_data[current_section_title] = current_block current_block = {} target_found = False # 检查当前标题是否包含 target_values 中的任意关键词 if any(tv in value for tv in target_values): target_found = True # 找到了目标章节,开始捕获后续内容 current_section_title = value # 保存章节标题内容 elif target_found: # 只捕获目标值之后的内容 current_block[key] = value # 保存最后一个块(如果有的话) if current_block: extracted_data[current_section_title] = current_block return extracted_data def process_with_outer_key(data): processed_data = {} # 遍历外层的键值对 for outer_key, inner_data in data.items(): # 调用 transform_json 函数对内层数据进行处理 processed_inner_data = transform_json(inner_data) # 将处理后的数据保留在外层键下 processed_data[outer_key] = processed_inner_data return processed_data """ 递归处理嵌套的数据结构(字典和列表)。 对最内层的字符串值应用 post_process 函数。 post_process 函数尝试将长字符串按特定模式分割成块,每块至少包含50个中英文字符。 如果字典中所有值都是 ""、"/" 或空列表,则返回'键'的列表。 """ # 读取JSON数据,提取内容,转换结构,并打印结果 def extract_from_notice(merged_baseinfo_path,clause_path, type): """ 从公告中提取特定类型的内容。 Args: clause_path (str): 包含条款的JSON文件路径。 type (int): 提取的类型。 1 - ["投标文件","响应文件","响应性文件"] 2 - ["开标", "评标", "定标","磋商程序","中标","程序","步骤"] 3 - ["重新招标、不再招标和终止招标", "重新招标", "不再招标", "终止招标"] 4 - ["评标"] # 测试 Returns: dict 或 str: 提取并处理后的数据,或在 `clause_path` 为空或发生错误时返回空字符串 `""`。 """ # 定义默认的返回结果 DEFAULT_RESULT = "" # 判断 clause_path 是否为空 if not clause_path: print("Warning: clause_path is empty.") return DEFAULT_RESULT # 根据 type 设置 target_values if type == 1: target_values = ["投标文件", "响应文件", "响应性文件"] elif type == 2: target_values = ["开标", "评标", "定标", "磋商程序", "中标", "程序", "步骤"] elif type == 3: target_values = ["重新招标、不再招标和终止招标","重新招标","重新采购", "不再招标", "不再采购","终止招标","终止采购"] elif type == 4: target_values = ["评标"] # 测试 else: print(f"Error: Invalid type specified: {type}. Use 1, 2, 3, or 4.") return DEFAULT_RESULT try: # 打开并读取JSON文件 with open(clause_path, 'r', encoding='utf-8') as file: data = json.load(file) # 提取目标部分 extracted_data = extract_between_sections(data, target_values) # 读取json,截取大标题之间的内容 if not extracted_data: final_result = get_requirements_with_gpt(merged_baseinfo_path, type) #万一没用正则匹配到,那就调用大模型 return final_result # print(json.dumps(extracted_data,ensure_ascii=False,indent=4)) extracted_data_concatenated = {section: concatenate_keys_values(content) #启用结构化就注释这三行 for section, content in extracted_data.items()} return extracted_data_concatenated # transformed_data = process_with_outer_key(extracted_data) #取消注释这三行 # final_result = process_nested_data(transformed_data) # return final_result except Exception as e: print(f"Error in extract_from_notice: {e}") return DEFAULT_RESULT if __name__ == "__main__": clause_path = r'C:\Users\Administrator\Desktop\fsdownload\a110ed59-00e8-47ec-873a-bd4579a6e628\\clause1.json' merged_baseinfo_path=r"C:\Users\Administrator\Desktop\fsdownload\a110ed59-00e8-47ec-873a-bd4579a6e628\ztbfile_merged_baseinfo.pdf" # file_path = 'D:\\flask_project\\flask_app\\static\\output\\fee18877-0c60-4c28-911f-9a5f7d1325a7\\clause1.json' try: res = extract_from_notice(merged_baseinfo_path,clause_path, 1) # 可以改变此处的 type 参数测试不同的场景 res2=json.dumps(res,ensure_ascii=False,indent=4) print(res2) except ValueError as e: print(e)