2024-09-30 16:23:39 +08:00
|
|
|
|
import json
|
|
|
|
|
import re
|
2024-10-22 21:02:54 +08:00
|
|
|
|
from functools import cmp_to_key
|
|
|
|
|
|
2024-12-11 17:42:51 +08:00
|
|
|
|
from flask_app.general.投标人须知正文提取指定内容 import get_requirements_with_gpt, concatenate_keys_values, extract_between_sections
|
2024-09-30 16:23:39 +08:00
|
|
|
|
|
2024-11-07 16:22:52 +08:00
|
|
|
|
# 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
|
2024-09-30 16:23:39 +08:00
|
|
|
|
|
|
|
|
|
|
2024-10-19 12:53:25 +08:00
|
|
|
|
"""
|
|
|
|
|
递归处理嵌套的数据结构(字典和列表)。
|
|
|
|
|
对最内层的字符串值应用 post_process 函数。
|
|
|
|
|
post_process 函数尝试将长字符串按特定模式分割成块,每块至少包含50个中英文字符。
|
|
|
|
|
如果字典中所有值都是 ""、"/" 或空列表,则返回'键'的列表。
|
|
|
|
|
"""
|
2024-09-30 16:23:39 +08:00
|
|
|
|
|
|
|
|
|
# 读取JSON数据,提取内容,转换结构,并打印结果
|
2024-11-07 17:01:14 +08:00
|
|
|
|
def extract_from_notice(merged_baseinfo_path, clause_path, type):
|
2024-10-17 15:33:58 +08:00
|
|
|
|
"""
|
|
|
|
|
从公告中提取特定类型的内容。
|
2024-11-07 17:01:14 +08:00
|
|
|
|
|
2024-10-17 15:33:58 +08:00
|
|
|
|
Args:
|
2024-11-07 17:01:14 +08:00
|
|
|
|
merged_baseinfo_path (str): 合并后的基础信息路径。
|
2024-10-17 15:33:58 +08:00
|
|
|
|
clause_path (str): 包含条款的JSON文件路径。
|
|
|
|
|
type (int): 提取的类型。
|
|
|
|
|
1 - ["投标文件","响应文件","响应性文件"]
|
|
|
|
|
2 - ["开标", "评标", "定标","磋商程序","中标","程序","步骤"]
|
|
|
|
|
3 - ["重新招标、不再招标和终止招标", "重新招标", "不再招标", "终止招标"]
|
|
|
|
|
4 - ["评标"] # 测试
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
dict 或 str: 提取并处理后的数据,或在 `clause_path` 为空或发生错误时返回空字符串 `""`。
|
|
|
|
|
"""
|
|
|
|
|
# 定义默认的返回结果
|
|
|
|
|
DEFAULT_RESULT = ""
|
|
|
|
|
|
2024-11-07 17:01:14 +08:00
|
|
|
|
# 映射 type 到 target_values
|
|
|
|
|
type_target_map = {
|
|
|
|
|
1: ["投标文件", "响应文件", "响应性文件"],
|
2024-12-25 14:35:52 +08:00
|
|
|
|
2: ["开标", "评标", "定标","评审","成交","合同","磋商","谈判", "中标", "程序", "步骤"],
|
2024-11-07 17:01:14 +08:00
|
|
|
|
3: ["重新招标、不再招标和终止招标", "重新招标", "重新采购", "不再招标", "不再采购", "终止招标", "终止采购"],
|
|
|
|
|
4: ["评标"] # 测试
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
target_values = type_target_map.get(type)
|
|
|
|
|
if not target_values:
|
2024-10-17 15:33:58 +08:00
|
|
|
|
print(f"Error: Invalid type specified: {type}. Use 1, 2, 3, or 4.")
|
|
|
|
|
return DEFAULT_RESULT
|
2024-11-06 17:29:45 +08:00
|
|
|
|
|
2024-11-07 17:01:14 +08:00
|
|
|
|
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
|
|
|
|
|
return extracted_data_concatenated
|
|
|
|
|
|
|
|
|
|
# 如果 clause_path 为空或提取数据失败,调用回退函数
|
|
|
|
|
final_result = get_requirements_with_gpt(merged_baseinfo_path, type)
|
|
|
|
|
return final_result
|
2024-09-30 16:23:39 +08:00
|
|
|
|
|
2024-10-17 15:33:58 +08:00
|
|
|
|
except Exception as e:
|
2024-11-07 17:01:14 +08:00
|
|
|
|
print(f"Error occurred: {e}")
|
2024-10-17 15:33:58 +08:00
|
|
|
|
return DEFAULT_RESULT
|
|
|
|
|
|
2024-12-10 09:02:39 +08:00
|
|
|
|
#TODO:可以通过判断格式来看是否需要调用GPT 1.1 2.1....
|
|
|
|
|
|
2024-09-30 16:23:39 +08:00
|
|
|
|
if __name__ == "__main__":
|
2024-12-26 17:20:27 +08:00
|
|
|
|
clause_path = r'C:\Users\Administrator\Desktop\文件解析问题\文件解析问题\910f6c0c-8a71-438d-a25c-7a6b34aa689c\tmp\clause1.json'
|
2024-12-12 16:06:20 +08:00
|
|
|
|
merged_baseinfo_path=r"D:\flask_project\flask_app\static\output\output1\ce279982-aeeb-4f08-ab39-df6ee2732eae\ztbfile.pdf"
|
2024-10-22 21:02:54 +08:00
|
|
|
|
# file_path = 'D:\\flask_project\\flask_app\\static\\output\\fee18877-0c60-4c28-911f-9a5f7d1325a7\\clause1.json'
|
2024-09-30 16:23:39 +08:00
|
|
|
|
try:
|
2024-12-25 14:35:52 +08:00
|
|
|
|
res = extract_from_notice(merged_baseinfo_path,clause_path, 2) # 可以改变此处的 type 参数测试不同的场景
|
2024-09-30 16:23:39 +08:00
|
|
|
|
res2=json.dumps(res,ensure_ascii=False,indent=4)
|
|
|
|
|
print(res2)
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
print(e)
|