Browse Source

添加一个比较类问题

dujunlong 2 months ago
parent
commit
460f9281d5

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+/final/ByRules/build
+/final/ByRules/dist
+

+ 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="Python 3.9 (BJKG)" jdkType="Python SDK" />
+    <orderEntry type="jdk" jdkName="VAE" 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="Python 3.9 (BJKG)" project-jdk-type="Python SDK" />
+  <component name="ProjectRootManager" version="2" project-jdk-name="VAE" project-jdk-type="Python SDK" />
 </project>

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


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


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


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


+ 39 - 12
final/ByRules/app.py

@@ -1,4 +1,6 @@
 from flask import Flask, request, jsonify
+
+from final.ByRules.commonUtil import fill_template, fill_template_auto
 from similarity_answer_json import *
 from util import *
 import os
@@ -11,6 +13,7 @@ app = Flask(__name__)
 BASE_DIR = os.path.dirname(os.path.abspath(__file__))
 TEMPLATE_FOLDER = os.path.join(BASE_DIR, "templatesJson")
 DATA_FOLDER = os.path.join(BASE_DIR, "..", "Json", "json_data")
+MAPPING_FILE = os.path.join(BASE_DIR,"..", "Json", "省间关系映射.json")
 
 
 @app.route('/process_query', methods=['POST'])
@@ -22,6 +25,8 @@ def process_query_route():
 
     try:
         result = process_query(query, template_dict, TEMPLATE_FOLDER)
+
+        #  如果没有该问题模板
         if result['play'] == '疑问':
             response = {
                 # "content": final_content,
@@ -32,20 +37,42 @@ def process_query_route():
                 "play": result["play"]
             }
             return jsonify(response)
-        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_text": result["content"],
-            "raw_result": final_value,
-            "conditions": result["conditions"],
-            "name": result["name"],
-            "play": result["play"]
-        }
-        return jsonify(response)
+        #  查询类问题:
+        if result['type'] == 'query':
+            final_value = smart_find_value(DATA_FOLDER, result["dataJsonName"], result["conditions"], result["target"])
+            # final_content = result["content"].replace("&", str(final_value))
+            final_content = fill_template_auto(result['content'], final_value)
+            response = {
+                "content": final_content,
+                "content_text": result["content"],
+                "raw_result": final_value,
+                "conditions": result["conditions"],
+                "name": result["name"],
+                "play": result["play"]
+            }
+            return jsonify(response)
+        # 比较类问题
+        elif result['type'] == 'compare_max_min':
+            final_value = find_max_or_min_value(folder_path=DATA_FOLDER, file_name=result["dataJsonName"],
+                                                value_key=result['value_key'],
+                                                name_key=result['name_key'],
+                                                mapping_file=MAPPING_FILE,
+                                                conditions=result["conditions"],
+                                                find_max=result['find_max'])
+            keys = [result['name_key'], result['value_key']]
+            final_content = fill_template_auto(result['content'], final_value, keys)
+            response = {
+                "content": final_content,
+                "content_text": result["content"],
+                "raw_result": final_value,
+                "conditions": result["conditions"],
+                "name": result["name"],
+                "play": result["play"]
+            }
+            return jsonify(response)
     except Exception as e:
         return jsonify({"error": str(e)}), 500
 
+
 if __name__ == "__main__":
     app.run(debug=True)

+ 80 - 0
final/ByRules/commonUtil.py

@@ -0,0 +1,80 @@
+def fill_template(template: str, result: dict, keys: list) -> str:
+    """
+    将模板字符串中的每个 '&' 占位符依次替换为 result 中对应 keys 的值。
+
+    :param template: 含有占位符 `&` 的模板字符串
+    :param result: 数据字典,例如 {"单位": "新疆", "送出电量": 99475687.839}
+    :param keys: 替换顺序中使用的 key 列表,例如 ["单位", "送出电量"]
+    :return: 替换完成后的字符串
+    """
+    parts = template.split('&')
+    if len(parts) - 1 != len(keys):
+        raise ValueError("占位符 `&` 数量与 keys 长度不一致")
+
+    output = ''
+    for i in range(len(keys)):
+        output += parts[i] + str(result.get(keys[i], ''))
+    output += parts[-1]
+    return output
+
+
+def fill_template_auto(template: str, result, keys: list = None) -> str:
+    parts = template.split('&')
+    placeholder_count = len(parts) - 1
+
+    # 如果是字典类型,必须提供 keys
+    if isinstance(result, dict):
+        if not keys or len(keys) != placeholder_count:
+            raise ValueError("使用 dict 类型时,必须提供与占位符数相等的 keys")
+        values = [result.get(k, '') for k in keys]
+
+    # 如果是列表或元组类型,直接使用顺序填充
+    elif isinstance(result, (list, tuple)):
+        if len(result) != placeholder_count:
+            raise ValueError("list/tuple 长度必须与占位符数一致")
+        values = [str(v) for v in result]
+
+    # 如果是字符串,只能替换一个占位符
+    elif isinstance(result, str):
+        if placeholder_count != 1:
+            raise ValueError("字符串只能替换一个占位符 `&`")
+        values = [result]
+
+    else:
+        raise TypeError("不支持的 result 类型")
+
+    # 拼接最终字符串
+    output = ''
+    for i in range(placeholder_count):
+        output += parts[i] + str(values[i])
+    output += parts[-1]
+    return output
+
+
+# template = "送出电量最高的省份是&,送出电量是&"
+# result = {
+#     "单位": "新疆",
+#     "送出电量": 99475687.839
+# }
+# keys = ["单位", "送出电量"]
+#
+# output = fill_template(template, result, keys)
+# print(output)
+
+
+# # 字典
+# template = "送出电量最高的省份是&,送出电量是&"
+# result = {"单位": "新疆", "送出电量": 99475687.839}
+# keys = ["单位", "送出电量"]
+#
+# print(fill_template_auto(template, result, keys))
+#
+# # 列表
+# print(fill_template_auto(template, ["新疆", 99475687.839]))
+#
+#
+# # 单个字符串
+# template = "最高的省份是&"
+# print(fill_template_auto(template, "新疆"))
+
+

+ 12 - 3
final/ByRules/similarity_answer_json.py

@@ -33,6 +33,7 @@ template_dict = {
     "9.15": ["某年省间交易电量双边交易电量是多少?"],
     "9.16": ["某年省间交易电量集中交易电量是多少?"],
     "9.17": ["某年省间交易电量挂牌交易电量是多少?"],
+    "17.1": ["那个省送出电量最高?是多少?"],
     "19": ["省间交易正在组织的交易有多少?"],
     "20": ["省间交易当月完成的交易有多少?"],
     "21": ["省间交易当年完成的交易有多少?"],
@@ -252,9 +253,13 @@ def process_query(query, template_dict, json_folder, tokenizer=jieba_tokenizer):
     type_ = template_info.get("type", "")
     # 模板的名字
     dataJsonName = template_info.get("dataJsonName", "")
-
+    # ---------------- 比较类 -----------------
+    value_key = template_info.get("value_key", "")
+    name_key = template_info.get("name_key", "")
+    find_max = template_info.get("find_max")
+    # block名称
     name = template_info.get("name", "")
-    # content
+    # 输出内容
     content = template_info.get("content", "")
     # 动作类型
     play = template_info.get("play", "")
@@ -270,7 +275,11 @@ def process_query(query, template_dict, json_folder, tokenizer=jieba_tokenizer):
         "conditions": conditions,
         "content": content,
         "query": query,
-        "play": play
+        "play": play,
+        "find_max": find_max,
+        "value_key": value_key,
+        "name_key": name_key
+
     }
 # 查询类
 def smart_find_value(folder_path, file_name, conditions: dict, target_key: str):

+ 11 - 0
final/ByRules/templatesJson/17.1.json

@@ -0,0 +1,11 @@
+{
+  "dataJsonName": "sjjy1_B04_output",
+  "type": "compare_max_min",
+  "value_key": "送出电量",
+  "name_key": "单位",
+  "find_max": "True",
+  "content": "送出电量最高的省份是&,送出电量是&",
+  "play": "讲述文本",
+  "name": "省间交易",
+  "qcode": "17.1"
+}

+ 12 - 10
final/ByRules/util.py

@@ -64,6 +64,7 @@ def smart_find_value(folder_path, file_name, conditions: dict, target_key: str):
 # 找最值
 def find_max_or_min_value(folder_path, file_name, value_key: str, name_key: str, mapping_file=None, find_max=True,
                           conditions=None):
+    file_name = file_name + ".json"
     file_path = os.path.join(folder_path, file_name)
 
     if not os.path.exists(file_path):
@@ -111,8 +112,8 @@ def find_max_or_min_value(folder_path, file_name, value_key: str, name_key: str,
     unit_code = target_record.get(name_key)
     province = name_mapping.get(unit_code, unit_code)  # 优先用映射名
     return {
-        "单位编码": unit_code,
-        "省份": province,
+        # "单位编码": unit_code,
+        name_key: province,
         value_key: target_value
     }
 # 找TopN
@@ -289,21 +290,22 @@ if __name__ == '__main__':
     # value = smart_find_value(folder, filename, conditions, target_field)
     # print("查询结果:", value)
     #
-    folder = "Json/json_data"
-    filename = "2.json"
-    mapping_file = "Json/省间关系映射.json"
-    conditions = {"年": 2025}
+    folder = "../Json/json_data"
+    filename = "sjjy1_B04_output.json"
+    mapping_file = "../Json/省间关系映射.json"
+    # conditions = {"年": 2025}
+    conditions = {}
     result = find_max_or_min_value(
         folder_path=folder,
         file_name=filename,
-        value_key="交易电量",   # 要比较的数值字段
-        name_key="",  # 待寻找的字段(例如:年、月、单位)(哪一“年”、“月”,哪个“省”)
+        value_key="送出电量",   # 要比较的数值字段
+        name_key="单位",  # 待寻找的字段(例如:年、月、单位)(哪一“年”、“月”,哪个“省”)
         mapping_file=mapping_file,
         find_max=True,  # 找最大值,
         conditions=conditions
     )
-    #
-    # print("送出电量最高的省份:", result)
+
+    print("送出电量最高的省份:", result)
     #
     # folder = "Json/json_data"
     # filename = "2.json"