浏览代码

Merge branch 'master' of http://120.24.5.168:8050/dujunlong/bjqa

dujunlong 2 月之前
父节点
当前提交
69086bc552

二进制
final/ByRules/__pycache__/extract_by_am.cpython-39.pyc


二进制
final/ByRules/__pycache__/similarity_answer_json.cpython-39.pyc


+ 4 - 0
final/ByRules/app.py

@@ -45,7 +45,11 @@ def process_query_route():
         # return jsonify({"error": "Query cannot be empty"}), 400
         return jsonify(error_response("Query cannot be empty"))
     try:
+        # 按照模板匹配的方法
         result = process_query(query, template_dict, TEMPLATE_FOLDER)
+        # # 按照自动机提取模板的方式进行匹配
+        # extractor = EntityExtractor()
+        # result = extractor.extract(query)
 
         #  如果没有该问题模板
         if result['play'] == '疑问':

+ 27 - 58
final/ByRules/similarity_answer_json.py

@@ -72,7 +72,23 @@ from typing import Tuple, List, Dict
 from datetime import datetime
 import re
 
+# 对时间进行预处理,例如用户输入24年则变成2024年
+def normalize_year(question: str) -> str:
+    # 匹配类似“24年”,“99年”这样的年份,排除“2014年”“1999年”这类四位数
+    def replace_func(match):
+        year = int(match.group(1))
+        # 根据需要设定规则:00-30算2000年以后,其余算1900年以后(你可改为你自己的需求)
+        if year <= 30:   # 例如30年以内默认2000+
+            return f'20{year:02d}年'
+        else:
+            return f'19{year:02d}年'
+
+    # 正则匹配两位数年份,但前面不能有数字(避免2014变20142014)
+    return re.sub(r'(?<!\d)(\d{2})年', replace_func, question)
+
+
 def extract_time_location(question: str) -> Tuple[List[Dict], List[str]]:
+    question = normalize_year(question)
     current_date = datetime.now()
     current_year = current_date.year
     current_month = current_date.month
@@ -111,7 +127,7 @@ def extract_time_location(question: str) -> Tuple[List[Dict], List[str]]:
     used_keywords = set()
 
     # 🆕 处理“起止时间段”,格式:2023年1月到2024年2月、去年1月到今年2月、2023年1月到1月等
-    range_pattern = r'(?P<start>(\d{4}|今|去|前|明)年(\d{1,2})?月?)到(?P<end>(\d{4}|今|去|前|明)年(\d{1,2})?月?)'
+    range_pattern = r'(?P<start>(\d{4}|今|去|前|明)年(\d{1,2})?月?)(|至)(?P<end>(\d{4}|今|去|前|明)年(\d{1,2})?月?)'
     for match in re.finditer(range_pattern, question):
         start_raw, end_raw = match.group('start'), match.group('end')
 
@@ -156,7 +172,7 @@ def extract_time_location(question: str) -> Tuple[List[Dict], List[str]]:
         used_keywords.add(match.group())
 
     # 🆕 新增匹配“2024年1月到2月”,结束时间没有写年份,默认与开始时间同年
-    partial_range_pattern = r'(?P<year>\d{4})年(?P<start_month>\d{1,2})月到(?P<end_month>\d{1,2})月'
+    partial_range_pattern = r'(?P<year>\d{4})年(?P<start_month>\d{1,2})月(|至)(?P<end_month>\d{1,2})月'
     for match in re.finditer(partial_range_pattern, question):
         # 避免重复匹配已经被上面时间段匹配使用过的字符串
         if match.group() in used_keywords:
@@ -600,44 +616,21 @@ def find_key_recursively(data, target_key):
 
     _search(data)
     return results
-# query = "当月省间交易完成的交易是多少?"
-# query = "2024年1月到2月累计交易电量是多少?"
-# query = "2023年省间交易电量新能源交易电量是多少??"
-# query = "但同样阿贾克斯大口径的话我可合金外壳设计文件突然发?"
-
-
-# query = "2023年省间交易电量新能源交易电量是多少??"
-# query = "今年1月到2023年2月累计交易电量是多少?"
-# query = "2024年送出电量第二名是谁?"
-# query = "2024年7月、8月、12月交易电量的平均值是多少?"
+# # query = "当月省间交易完成的交易是多少?"
+# query = "2024年1月到2月山东省累计交易电量是多少?"
+# # query = "2023年省间交易电量新能源交易电量是多少??"
+# # query = "但同样阿贾克斯大口径的话我可合金外壳设计文件突然发?"
 #
-# json_folder = "templatesJson"
 #
+# # query = "2023年省间交易电量新能源交易电量是多少??"
+# # query = "今年1月到2023年2月累计交易电量是多少?"
+# # query = "2024年送出电量第二名是谁?"
+# # query = "2024年7月、8月、12月交易电量的平均值是多少?"
 # #
-# #
-# result = process_query(query, template_dict, json_folder)
-# #
-# print(result)
-# print(result['content'])
-# print("匹配的模板 key:", result["matched_key"])
-# print("最相似的模板句:", result["matched_template"])
-# print("相似度分数:", result["similarity_score"])
-# print("类型:", result["type"])
-# print("关键词:", result["keywords"])
-# print("查询字段:", result["target"])
-# print("模型名字", result["name"])
-# print("条件", result["conditions"])
-# print("返回的内容是:", result["content"])
-# print("问句是:", result["query"])
-# print("动作是:", result["play"])
-
-# query = "当月送出均价最高的是哪个省??"
-# query = ("交易?")
-#
 # json_folder = "templatesJson"
 #
 # result = process_query(query, template_dict, json_folder)
-
+#
 # print("匹配的模板 key:", result["matched_key"])
 # print("最相似的模板句:", result["matched_template"])
 # print("相似度分数:", result["similarity_score"])
@@ -651,28 +644,4 @@ def find_key_recursively(data, target_key):
 # print("动作是:", result["play"])
 # print("描述:", result["content"])
 
-#
-# type = result["type"]
-# content = result["content"]
-#
-# json_data_folder = "..\Json\json_data"
-# if type == "query":
-#     fileName = result["dataJsonName"]
-#     result = smart_find_value(json_data_folder, fileName,result["conditions"],result["target"] )
-#     print(result)
-# elif type == "calculate":
-#     conditions = result["conditions"]
-#     start_conditions = {('年' if 'year' in k else '月'): v for k, v in conditions.items() if k.startswith('start_')}
-#     end_conditions = {('年' if 'year' in k else '月'): v for k, v in conditions.items() if k.startswith('end_')}
-#     print(start_conditions)
-#     print(end_conditions)
-#     fileName = result["dataJsonName"] + ".json"
-#     result = calculate_sum_by_time_range(json_data_folder,fileName,result["target"],start_conditions, end_conditions)
-#     print(result)
-#
-# # 最终回答的文本
-# final_content = content.replace("?", str(result))
-# # print(f"{content}{result}")
-#
-# print(final_content)