From 82c5cc4cef3e3ee091b28da2b2d7ac936c5489a5 Mon Sep 17 00:00:00 2001 From: SXP-Simon Date: Wed, 25 Mar 2026 16:15:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20JSON=20=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E4=B8=AD=E6=AD=A3=E5=88=99=E8=B4=AA=E5=A9=AA=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=E5=AF=BC=E8=87=B4=E7=9A=84=E6=95=B0=E7=BB=84=E6=88=AA?= =?UTF-8?q?=E6=96=AD=E9=97=AE=E9=A2=98=20(#126)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将解析 JSON 数组的正则模式从非贪婪 `.*?` 改为贪婪匹配 `.*`,防止内容中包含方括号(如 [UID])时导致 JSON 截断。 - 优化话题、称号、金句等分析逻辑中的正则表达式,使用更严谨的模式处理转义双引号和特殊格式。 --- src/infrastructure/analysis/utils/json_utils.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/infrastructure/analysis/utils/json_utils.py b/src/infrastructure/analysis/utils/json_utils.py index c27b563..d6fb2f9 100644 --- a/src/infrastructure/analysis/utils/json_utils.py +++ b/src/infrastructure/analysis/utils/json_utils.py @@ -97,7 +97,7 @@ def parse_json_response( fixed_json_text = None try: # 1. 提取JSON部分 - json_match = re.search(r"\[.*?\]", result_text, re.DOTALL) + json_match = re.search(r"\[.*\]", result_text, re.DOTALL) if not json_match: error_msg = f"{data_type}响应中未找到JSON格式" logger.warning(error_msg) @@ -220,12 +220,12 @@ def extract_topics_with_regex(result_text: str, max_topics: int) -> list[dict]: try: # 更强的正则表达式提取话题信息,处理转义字符 # 匹配每个完整的话题对象 - topic_pattern = r'\{\s*"topic":\s*"([^"]+)"\s*,\s*"contributors":\s*\[([^\]]+)\]\s*,\s*"detail":\s*"([^"]*(?:\\.[^"]*)*)"\s*\}' + topic_pattern = r'\{\s*"topic":\s*"([^"]*(?:\\.[^"]*)*)"\s*,\s*"contributors":\s*\[(.*?)\],?\s*"detail":\s*"([^"]*(?:\\.[^"]*)*)"\s*\}' matches = re.findall(topic_pattern, result_text, re.DOTALL) if not matches: # 尝试更宽松的匹配 - topic_pattern = r'"topic":\s*"([^"]+)"[^}]*"contributors":\s*\[([^\]]+)\][^}]*"detail":\s*"([^"]*(?:\\.[^"]*)*)"' + topic_pattern = r'"topic":\s*"([^"]*(?:\\.[^"]*)*)"[^}]*"contributors":\s*\[(.*?)\][^}]*"detail":\s*"([^"]*(?:\\.[^"]*)*)"' matches = re.findall(topic_pattern, result_text, re.DOTALL) topics = [] @@ -274,12 +274,12 @@ def extract_user_titles_with_regex(result_text: str, max_count: int) -> list[dic titles = [] # 正则模式:匹配完整的用户称号对象 - pattern = r'\{\s*"name":\s*"([^"]+)"\s*,\s*"user_id":\s*"([^"]+)"\s*,\s*"title":\s*"([^"]+)"\s*,\s*"mbti":\s*"([^"]+)"\s*,\s*"reason":\s*"([^"]*(?:\\.[^"]*)*)"\s*\}' + pattern = r'\{\s*"name":\s*"([^"]*(?:\\.[^"]*)*)"\s*,\s*"user_id":\s*"([^"]+)"\s*,\s*"title":\s*"([^"]*(?:\\.[^"]*)*)"\s*,\s*"mbti":\s*"([^"]+)"\s*,\s*"reason":\s*"([^"]*(?:\\.[^"]*)*)"\s*\}' matches = re.findall(pattern, result_text, re.DOTALL) if not matches: # 尝试更宽松的匹配(字段顺序可变) - pattern = r'"name":\s*"([^"]+)"[^}]*"user_id":\s*"([^"]+)"[^}]*"title":\s*"([^"]+)"[^}]*"mbti":\s*"([^"]+)"[^}]*"reason":\s*"([^"]*(?:\\.[^"]*)*)"' + pattern = r'"name":\s*"([^"]*(?:\\.[^"]*)*)"[^}]*"user_id":\s*"([^"]+)"[^}]*"title":\s*"([^"]*(?:\\.[^"]*)*)"[^}]*"mbti":\s*"([^"]+)"[^}]*"reason":\s*"([^"]*(?:\\.[^"]*)*)"' matches = re.findall(pattern, result_text, re.DOTALL) for match in matches[:max_count]: @@ -325,12 +325,12 @@ def extract_golden_quotes_with_regex(result_text: str, max_count: int) -> list[d quotes = [] # 正则模式:匹配完整的金句对象 - pattern = r'\{\s*"content":\s*"([^"]*(?:\\.[^"]*)*)"\s*,\s*"sender":\s*"([^"]+)"\s*,\s*"reason":\s*"([^"]*(?:\\.[^"]*)*)"\s*\}' + pattern = r'\{\s*"content":\s*"([^"]*(?:\\.[^"]*)*)"\s*,\s*"sender":\s*"([^"]*(?:\\.[^"]*)*)"\s*,\s*"reason":\s*"([^"]*(?:\\.[^"]*)*)"\s*\}' matches = re.findall(pattern, result_text, re.DOTALL) if not matches: # 尝试更宽松的匹配(字段顺序可变) - pattern = r'"content":\s*"([^"]*(?:\\.[^"]*)*)"[^}]*"sender":\s*"([^"]+)"[^}]*"reason":\s*"([^"]*(?:\\.[^"]*)*)"' + pattern = r'"content":\s*"([^"]*(?:\\.[^"]*)*)"[^}]*"sender":\s*"([^"]*(?:\\.[^"]*)*)"[^}]*"reason":\s*"([^"]*(?:\\.[^"]*)*)"' matches = re.findall(pattern, result_text, re.DOTALL) for match in matches[:max_count]: @@ -372,7 +372,7 @@ def extract_quality_with_regex(result_text: str) -> dict | None: summary_m = re.search(r'"summary"\s*:\s*"([^"]*(?:\\.[^"]*)*)"', result_text) # Extract dimensions array - dims_match = re.search(r'"dimensions"\s*:\s*\[(.*?)\]', result_text, re.DOTALL) + dims_match = re.search(r'"dimensions"\s*:\s*\[(.*)\]', result_text, re.DOTALL) dims = [] if dims_match: dim_objects = re.findall(