zbparse/flask_app/货物标/投标人须知正文提取指定内容货物标版.py
2024-12-10 09:02:39 +08:00

137 lines
5.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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:
merged_baseinfo_path (str): 合并后的基础信息路径。
clause_path (str): 包含条款的JSON文件路径。
type (int): 提取的类型。
1 - ["投标文件","响应文件","响应性文件"]
2 - ["开标", "评标", "定标","磋商程序","中标","程序","步骤"]
3 - ["重新招标、不再招标和终止招标", "重新招标", "不再招标", "终止招标"]
4 - ["评标"] # 测试
Returns:
dict 或 str: 提取并处理后的数据,或在 `clause_path` 为空或发生错误时返回空字符串 `""`。
"""
# 定义默认的返回结果
DEFAULT_RESULT = ""
# 映射 type 到 target_values
type_target_map = {
1: ["投标文件", "响应文件", "响应性文件"],
2: ["开标", "评标", "定标", "磋商程序", "中标", "程序", "步骤"],
3: ["重新招标、不再招标和终止招标", "重新招标", "重新采购", "不再招标", "不再采购", "终止招标", "终止采购"],
4: ["评标"] # 测试
}
target_values = type_target_map.get(type)
if not target_values:
print(f"Error: Invalid type specified: {type}. Use 1, 2, 3, or 4.")
return DEFAULT_RESULT
try:
# 如果 clause_path 不为空,尝试从 JSON 文件中提取数据
if clause_path:
with open(clause_path, 'r', encoding='utf-8') as file:
data = json.load(file)
# 提取目标部分
extracted_data = extract_between_sections(data, target_values) # 读取json截取大标题之间的内容
# 如果成功提取到数据,进行后续处理并返回
if extracted_data:
# 合并键值对,启用结构化
extracted_data_concatenated = {
section: concatenate_keys_values(content)
for section, content in extracted_data.items()
}
# transformed_data = process_with_outer_key(extracted_data) #取消注释这三行
# final_result = process_nested_data(transformed_data)
# return final_result
print(json.dumps(extracted_data_concatenated,ensure_ascii=False,indent=4))
return extracted_data_concatenated
# 如果 clause_path 为空或提取数据失败,调用回退函数
final_result = get_requirements_with_gpt(merged_baseinfo_path, type)
return final_result
except Exception as e:
print(f"Error occurred: {e}")
return DEFAULT_RESULT
#TODO:可以通过判断格式来看是否需要调用GPT 1.1 2.1....
if __name__ == "__main__":
clause_path = r'C:\Users\Administrator\Desktop\招标文件\output4\tmp\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, 2) # 可以改变此处的 type 参数测试不同的场景
res2=json.dumps(res,ensure_ascii=False,indent=4)
print(res2)
except ValueError as e:
print(e)