|
@@ -6,9 +6,6 @@ import json
|
|
|
from datetime import datetime
|
|
|
from typing import Tuple, List, Dict
|
|
|
import re
|
|
|
-from util import *
|
|
|
-
|
|
|
-
|
|
|
def jieba_tokenizer(text):
|
|
|
return list(jieba.cut(text))
|
|
|
# 定义问题模板
|
|
@@ -214,6 +211,25 @@ def process_query(query, template_dict, json_folder, tokenizer=jieba_tokenizer):
|
|
|
# 提取条件
|
|
|
time_info, location_info = extract_time_location(query)
|
|
|
conditions = {}
|
|
|
+ # 匹配模板
|
|
|
+ matched_key, best_sentence, score = match_template(query, template_dict, tokenizer)
|
|
|
+ # 定义阈值
|
|
|
+ similarity_threshold = 0.4
|
|
|
+ # ★ 判断相似度阈值
|
|
|
+ if score < similarity_threshold:
|
|
|
+ return {
|
|
|
+ "matched_key": None,
|
|
|
+ "matched_template": None,
|
|
|
+ "similarity_score": score,
|
|
|
+ "type": None,
|
|
|
+ "keywords": None,
|
|
|
+ "target": None,
|
|
|
+ "name": None,
|
|
|
+ "conditions": conditions,
|
|
|
+ "content": "您提问的问题目前我还没有掌握",
|
|
|
+ "query": query,
|
|
|
+ "play":"疑问"
|
|
|
+ }
|
|
|
|
|
|
if time_info:
|
|
|
year = time_info[0].get('year')
|
|
@@ -226,8 +242,6 @@ def process_query(query, template_dict, json_folder, tokenizer=jieba_tokenizer):
|
|
|
unit = map_location_to_unit(location_info[0])
|
|
|
if unit and unit != '未知单位':
|
|
|
conditions['单位'] = unit
|
|
|
- # 匹配模板
|
|
|
- matched_key, best_sentence, score = match_template(query, template_dict, tokenizer)
|
|
|
# 查询模板json
|
|
|
template_info = load_template_info(matched_key, json_folder)
|
|
|
# 模板的关键词
|
|
@@ -237,15 +251,11 @@ def process_query(query, template_dict, json_folder, tokenizer=jieba_tokenizer):
|
|
|
# 模板的类型
|
|
|
type_ = template_info.get("type", "")
|
|
|
# 模板的名字
|
|
|
- dataJsonName = template_info.get("dataJsonName", "")
|
|
|
+ name = template_info.get("dataJsonName", "")
|
|
|
# content
|
|
|
content = template_info.get("content", "")
|
|
|
- # 模型需要做的动作:
|
|
|
+ # 动作类型
|
|
|
play = template_info.get("play", "")
|
|
|
- # 模块名称
|
|
|
- name = template_info.get("name", "")
|
|
|
- # 参数为模板、关键词列表、映射
|
|
|
-
|
|
|
return {
|
|
|
"matched_key": matched_key,
|
|
|
"matched_template": best_sentence,
|
|
@@ -253,74 +263,72 @@ def process_query(query, template_dict, json_folder, tokenizer=jieba_tokenizer):
|
|
|
"type": type_,
|
|
|
"keywords": keywords,
|
|
|
"target": target,
|
|
|
- "dataJsonName": dataJsonName,
|
|
|
+ "name": name,
|
|
|
"conditions": conditions,
|
|
|
"content": content,
|
|
|
- "play": play,
|
|
|
- "name": name
|
|
|
+ "query": query,
|
|
|
+ "play": play
|
|
|
}
|
|
|
-# # 查询类
|
|
|
-# def smart_find_value(folder_path, file_name, conditions: dict, target_key: str):
|
|
|
-# file_name = file_name + ".json"
|
|
|
-# file_path = os.path.join(folder_path, file_name)
|
|
|
-#
|
|
|
-# if not os.path.exists(file_path):
|
|
|
-# print(f"文件 {file_path} 不存在")
|
|
|
-# return None
|
|
|
-#
|
|
|
-# with open(file_path, 'r', encoding='utf-8') as f:
|
|
|
-# try:
|
|
|
-# data = json.load(f)
|
|
|
-# except json.JSONDecodeError as e:
|
|
|
-# print(f"JSON 解析失败:{e}")
|
|
|
-# return None
|
|
|
-#
|
|
|
-# def match_conditions(record):
|
|
|
-# return all(record.get(k) == v for k, v in conditions.items())
|
|
|
-#
|
|
|
-# # 情况一:数据是 dict
|
|
|
-# if isinstance(data, dict):
|
|
|
-# if not conditions or match_conditions(data):
|
|
|
-# values = find_key_recursively(data, target_key)
|
|
|
-# return values[0] if len(values) == 1 else values if values else None
|
|
|
-# return None
|
|
|
-#
|
|
|
-# # 情况二:数据是 list
|
|
|
-# elif isinstance(data, list):
|
|
|
-# results = []
|
|
|
-# for record in data:
|
|
|
-# if isinstance(record, dict) and match_conditions(record):
|
|
|
-# matches = find_key_recursively(record, target_key)
|
|
|
-# results.extend(matches)
|
|
|
-# if not results:
|
|
|
-# return None
|
|
|
-# elif len(results) == 1:
|
|
|
-# return results[0]
|
|
|
-# else:
|
|
|
-# return results
|
|
|
-# # 查询类的辅助函数
|
|
|
-# def find_key_recursively(data, target_key):
|
|
|
-# results = []
|
|
|
-#
|
|
|
-# def _search(obj):
|
|
|
-# if isinstance(obj, dict):
|
|
|
-# for k, v in obj.items():
|
|
|
-# if k == target_key:
|
|
|
-# results.append(v)
|
|
|
-# _search(v)
|
|
|
-# elif isinstance(obj, list):
|
|
|
-# for item in obj:
|
|
|
-# _search(item)
|
|
|
-#
|
|
|
-# _search(data)
|
|
|
-# return results
|
|
|
+# 查询类
|
|
|
+def smart_find_value(folder_path, file_name, conditions: dict, target_key: str):
|
|
|
+ file_name = file_name + ".json"
|
|
|
+ file_path = os.path.join(folder_path, file_name)
|
|
|
|
|
|
+ if not os.path.exists(file_path):
|
|
|
+ print(f"文件 {file_path} 不存在")
|
|
|
+ return None
|
|
|
|
|
|
+ with open(file_path, 'r', encoding='utf-8') as f:
|
|
|
+ try:
|
|
|
+ data = json.load(f)
|
|
|
+ except json.JSONDecodeError as e:
|
|
|
+ print(f"JSON 解析失败:{e}")
|
|
|
+ return None
|
|
|
+
|
|
|
+ def match_conditions(record):
|
|
|
+ return all(record.get(k) == v for k, v in conditions.items())
|
|
|
+
|
|
|
+ # 情况一:数据是 dict
|
|
|
+ if isinstance(data, dict):
|
|
|
+ if not conditions or match_conditions(data):
|
|
|
+ values = find_key_recursively(data, target_key)
|
|
|
+ return values[0] if len(values) == 1 else values if values else None
|
|
|
+ return None
|
|
|
+
|
|
|
+ # 情况二:数据是 list
|
|
|
+ elif isinstance(data, list):
|
|
|
+ results = []
|
|
|
+ for record in data:
|
|
|
+ if isinstance(record, dict) and match_conditions(record):
|
|
|
+ matches = find_key_recursively(record, target_key)
|
|
|
+ results.extend(matches)
|
|
|
+ if not results:
|
|
|
+ return None
|
|
|
+ elif len(results) == 1:
|
|
|
+ return results[0]
|
|
|
+ else:
|
|
|
+ return results
|
|
|
+# 查询类的辅助函数
|
|
|
+def find_key_recursively(data, target_key):
|
|
|
+ results = []
|
|
|
+
|
|
|
+ def _search(obj):
|
|
|
+ if isinstance(obj, dict):
|
|
|
+ for k, v in obj.items():
|
|
|
+ if k == target_key:
|
|
|
+ results.append(v)
|
|
|
+ _search(v)
|
|
|
+ elif isinstance(obj, list):
|
|
|
+ for item in obj:
|
|
|
+ _search(item)
|
|
|
+
|
|
|
+ _search(data)
|
|
|
+ return results
|
|
|
# query = "当月省间交易完成的交易是多少?"
|
|
|
-query = "2024年省间交易电量年度交易电量是多少??"
|
|
|
-json_folder = "templatesJson"
|
|
|
-
|
|
|
-
|
|
|
+# query = "但同样阿贾克斯大口径的话我可合金外壳设计文件突然发?"
|
|
|
+# json_folder = "templatesJson"
|
|
|
+#
|
|
|
+#
|
|
|
# result = process_query(query, template_dict, json_folder)
|
|
|
#
|
|
|
# print("匹配的模板 key:", result["matched_key"])
|
|
@@ -331,6 +339,9 @@ json_folder = "templatesJson"
|
|
|
# print("查询字段:", result["target"])
|
|
|
# print("模型名字", result["name"])
|
|
|
# print("条件", result["conditions"])
|
|
|
+# print("返回的内容是:", result["content"])
|
|
|
+# print("问句是:", result["query"])
|
|
|
+# print("动作是:", result["play"])
|
|
|
# content = result["content"]
|
|
|
#
|
|
|
# json_data_folder = "..\Json\json_data"
|