Browse Source

新增疑问

zfrr 17 hours ago
parent
commit
2322fd2ca8
36 changed files with 141 additions and 130 deletions
  1. 1 1
      .idea/insertMysql.iml
  2. 1 1
      .idea/misc.xml
  3. BIN
      final/ByRules/__pycache__/app.cpython-39.pyc
  4. BIN
      final/ByRules/__pycache__/similarity_answer_json.cpython-38.pyc
  5. BIN
      final/ByRules/__pycache__/similarity_answer_json.cpython-39.pyc
  6. BIN
      final/ByRules/__pycache__/util.cpython-39.pyc
  7. 4 4
      final/ByRules/app.py
  8. 84 73
      final/ByRules/similarity_answer_json.py
  9. 2 2
      final/ByRules/templatesJson/1.json
  10. 1 1
      final/ByRules/templatesJson/19.json
  11. 2 2
      final/ByRules/templatesJson/2.json
  12. 1 1
      final/ByRules/templatesJson/20.json
  13. 1 1
      final/ByRules/templatesJson/21.json
  14. 1 1
      final/ByRules/templatesJson/22.json
  15. 1 1
      final/ByRules/templatesJson/23.json
  16. 2 2
      final/ByRules/templatesJson/8.1.json
  17. 2 2
      final/ByRules/templatesJson/8.2.json
  18. 2 2
      final/ByRules/templatesJson/8.3.json
  19. 2 2
      final/ByRules/templatesJson/8.4.json
  20. 2 2
      final/ByRules/templatesJson/9.1.json
  21. 2 2
      final/ByRules/templatesJson/9.10.json
  22. 2 2
      final/ByRules/templatesJson/9.11.json
  23. 2 2
      final/ByRules/templatesJson/9.12.json
  24. 2 2
      final/ByRules/templatesJson/9.13.json
  25. 2 2
      final/ByRules/templatesJson/9.14.json
  26. 2 2
      final/ByRules/templatesJson/9.15.json
  27. 2 2
      final/ByRules/templatesJson/9.16.json
  28. 2 2
      final/ByRules/templatesJson/9.17.json
  29. 2 2
      final/ByRules/templatesJson/9.2.json
  30. 2 2
      final/ByRules/templatesJson/9.3.json
  31. 2 2
      final/ByRules/templatesJson/9.4.json
  32. 2 2
      final/ByRules/templatesJson/9.5.json
  33. 2 2
      final/ByRules/templatesJson/9.6.json
  34. 2 2
      final/ByRules/templatesJson/9.7.json
  35. 2 2
      final/ByRules/templatesJson/9.8.json
  36. 2 2
      final/ByRules/templatesJson/9.9.json

+ 1 - 1
.idea/insertMysql.iml

@@ -2,7 +2,7 @@
 <module type="PYTHON_MODULE" version="4">
   <component name="NewModuleRootManager">
     <content url="file://$MODULE_DIR$" />
-    <orderEntry type="jdk" jdkName="VAE" jdkType="Python SDK" />
+    <orderEntry type="jdk" jdkName="Python 3.9 (BJKG)" jdkType="Python SDK" />
     <orderEntry type="sourceFolder" forTests="false" />
   </component>
 </module>

+ 1 - 1
.idea/misc.xml

@@ -3,5 +3,5 @@
   <component name="Black">
     <option name="sdkName" value="Python 3.9" />
   </component>
-  <component name="ProjectRootManager" version="2" project-jdk-name="VAE" project-jdk-type="Python SDK" />
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (BJKG)" project-jdk-type="Python SDK" />
 </project>

BIN
final/ByRules/__pycache__/app.cpython-39.pyc


BIN
final/ByRules/__pycache__/similarity_answer_json.cpython-38.pyc


BIN
final/ByRules/__pycache__/similarity_answer_json.cpython-39.pyc


BIN
final/ByRules/__pycache__/util.cpython-39.pyc


+ 4 - 4
final/ByRules/app.py

@@ -22,13 +22,13 @@ def process_query_route():
 
     try:
         result = process_query(query, template_dict, TEMPLATE_FOLDER)
-        final_value = smart_find_value(DATA_FOLDER, result["dataJsonName"], result["conditions"], result["target"])
-        final_content = result["content"].replace("&", str(final_value))
+        # final_value = smart_find_value(DATA_FOLDER, result["dataJsonName"], result["conditions"], result["target"])
+        # final_content = result["content"].replace("&", str(final_value))
 
         response = {
-            "content": final_content,
+            # "content": final_content,
             "content_text": result["content"],
-            "raw_result": final_value,
+            # "raw_result": final_value,
             # "conditions": result["conditions"],
             "name": result["name"],
             "play": result["play"]

+ 84 - 73
final/ByRules/similarity_answer_json.py

@@ -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"

+ 2 - 2
final/ByRules/templatesJson/1.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "累计省间交易电量",
   "target": "省间交易电量",
-  "content": "全年累计交易电量是&",
+  "content": "全年累计交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "省间交易电量"
 }

+ 1 - 1
final/ByRules/templatesJson/19.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "正在组织",
   "target": "正在组织",
-  "content": "省间交易正在组织的交易为&",
+  "content": "省间交易正在组织的交易为?",
   "play": "讲述文本",
   "name": "省间交易"
 }

+ 2 - 2
final/ByRules/templatesJson/2.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "月交易电量",
   "target": "交易电量",
-  "content": "月交易电量是&",
+  "content": "月交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "省间交易电量"
 }

+ 1 - 1
final/ByRules/templatesJson/20.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "当月完成",
   "target": "当月完成",
-  "content": "省间交易当月完成的交易为&",
+  "content": "省间交易当月完成的交易为?",
   "play": "讲述文本",
   "name": "省间交易"
 }

+ 1 - 1
final/ByRules/templatesJson/21.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "当年完成",
   "target": "当年完成",
-  "content": "省间交易当年完成的交易为&",
+  "content": "省间交易当年完成的交易为?",
   "play": "讲述文本",
   "name": "省间交易"
 }

+ 1 - 1
final/ByRules/templatesJson/22.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "当年达成",
   "target": "当年达成电量",
-  "content": "省间交易当年达成的电量为&",
+  "content": "省间交易当年达成的电量为?",
   "play": "讲述文本",
   "name": "省间交易"
 }

+ 1 - 1
final/ByRules/templatesJson/23.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "参与交易",
   "target": "参与交易家次",
-  "content": "省间交易当年参与交易的家次为&",
+  "content": "省间交易当年参与交易的家次为?",
   "play": "讲述文本",
   "name": "省间交易"
 }

+ 2 - 2
final/ByRules/templatesJson/8.1.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "交易周期",
   "target": "按交易周期划分",
-  "content": "省间交易电量按交易周期划分的电量是&",
+  "content": "省间交易电量按交易周期划分的电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易周期/交易类型/发电类型/交易方式 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/8.2.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "交易类型",
   "target": "按交易类型划分",
-  "content": "省间交易电量按交易类型划分的电量是&",
+  "content": "省间交易电量按交易类型划分的电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易周期/交易类型/发电类型/交易方式 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/8.3.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "发电类型",
   "target": "按发电类型划分",
-  "content": "省间交易电量按发电类型划分的电量是&",
+  "content": "省间交易电量按发电类型划分的电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易周期/交易类型/发电类型/交易方式 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/8.4.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "交易方式",
   "target": "按交易方式划分",
-  "content": "省间交易电量按交易方式划分的电量是&",
+  "content": "省间交易电量按交易方式划分的电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易周期/交易类型/发电类型/交易方式 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.1.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "年度交易",
   "target": "年度交易",
-  "content": "省间交易电量年度交易电量是&",
+  "content": "省间交易电量年度交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易周期 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.10.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "非市场化交易",
   "target": "非市场化",
-  "content": "省间交易电量非市场化交易电量是&",
+  "content": "省间交易电量非市场化交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易类型 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.11.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "新能源交易",
   "target": "新能源",
-  "content": "省间交易电量新能源交易电量是&",
+  "content": "省间交易电量新能源交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 发电类型 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.12.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "火电交易",
   "target": "火电",
-  "content": "省间交易电量火电交易电量是&",
+  "content": "省间交易电量火电交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 发电类型 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.13.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "水电交易",
   "target": "水电",
-  "content": "省间交易电量水电交易电量是&",
+  "content": "省间交易电量水电交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 发电类型 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.14.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "核电交易",
   "target": "核电",
-  "content": "省间交易电量核电交易电量是&",
+  "content": "省间交易电量核电交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 发电类型 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.15.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "双边交易",
   "target": "双边",
-  "content": "省间交易电量双边交易电量是&",
+  "content": "省间交易电量双边交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易方式 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.16.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "集中交易",
   "target": "集中",
-  "content": "省间交易电量集中交易电量是&",
+  "content": "省间交易电量集中交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易方式 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.17.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "挂牌交易",
   "target": "挂牌",
-  "content": "省间交易电量挂牌交易电量是&",
+  "content": "省间交易电量挂牌交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易方式 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.2.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "月度交易",
   "target": "月度交易",
-  "content": "省间交易电量月度交易电量是&",
+  "content": "省间交易电量月度交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易周期 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.3.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "现货交易",
   "target": "现货交易",
-  "content": "省间交易电量现货交易电量是&",
+  "content": "省间交易电量现货交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易周期 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.4.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "应急交易",
   "target": "应急交易",
-  "content": "省间交易电量应急交易电量是&",
+  "content": "省间交易电量应急交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易周期 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.5.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "月内交易",
   "target": "月内交易",
-  "content": "省间交易电量月内交易电量是&",
+  "content": "省间交易电量月内交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易周期 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.6.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "省间外送交易",
   "target": "省间外送",
-  "content": "省间交易电量省间外送交易电量是&",
+  "content": "省间交易电量省间外送交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易类型 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.7.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "电力直接交易",
   "target": "电力直接交易",
-  "content": "省间交易电量电力直接交易电量是&",
+  "content": "省间交易电量电力直接交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易类型 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.8.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "合同交易",
   "target": "合同交易",
-  "content": "省间交易电量合同交易电量是&",
+  "content": "省间交易电量合同交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易类型 划分"
 }

+ 2 - 2
final/ByRules/templatesJson/9.9.json

@@ -3,7 +3,7 @@
   "type": "query",
   "keyword": "绿电交易",
   "target": "绿电交易",
-  "content": "省间交易电量绿电交易电量是&",
+  "content": "省间交易电量绿电交易电量是?",
   "play": "讲述文本",
-  "name": "省间交易"
+  "name": "按 交易类型 划分"
 }