From 8048e4416e9c6636ef05b36c36ec560be0ceaee1 Mon Sep 17 00:00:00 2001 From: SXP-Simon Date: Thu, 9 Oct 2025 16:58:06 +0800 Subject: [PATCH] [fix] (code review) --- src/analysis/analyzers/base_analyzer.py | 4 ++-- src/analysis/analyzers/golden_quote_analyzer.py | 2 +- src/analysis/analyzers/topic_analyzer.py | 4 ++-- src/analysis/llm_analyzer.py | 17 +---------------- src/analysis/utils/json_utils.py | 10 ++++++++-- 5 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/analysis/analyzers/base_analyzer.py b/src/analysis/analyzers/base_analyzer.py index e42551b..b42930e 100644 --- a/src/analysis/analyzers/base_analyzer.py +++ b/src/analysis/analyzers/base_analyzer.py @@ -148,8 +148,8 @@ class BaseAnalyzer(ABC): data_objects = self.create_data_objects(regex_data) return data_objects, token_usage else: - # 最后的降级方案 - logger.warning(f"{self.get_data_type()}正则表达式提取失败,返回空列表") + # 最后的降级方案 - 两种方法都失败 + logger.error(f"{self.get_data_type()}分析失败: JSON解析和正则表达式提取均未成功,返回空列表") return [], token_usage except Exception as e: diff --git a/src/analysis/analyzers/golden_quote_analyzer.py b/src/analysis/analyzers/golden_quote_analyzer.py index eb9f22e..5c27b3a 100644 --- a/src/analysis/analyzers/golden_quote_analyzer.py +++ b/src/analysis/analyzers/golden_quote_analyzer.py @@ -39,7 +39,7 @@ class GoldenQuoteAnalyzer(BaseAnalyzer): 构建金句分析提示词 Args: - messages: 圣经的文本消息列表 + messages: 群聊的文本消息列表 Returns: 提示词字符串 diff --git a/src/analysis/analyzers/topic_analyzer.py b/src/analysis/analyzers/topic_analyzer.py index a867491..6f1835f 100644 --- a/src/analysis/analyzers/topic_analyzer.py +++ b/src/analysis/analyzers/topic_analyzer.py @@ -56,8 +56,8 @@ class TopicAnalyzer(BaseAnalyzer): text = content.get("data", {}).get("text", "").strip() if text and len(text) > 2 and not text.startswith("/"): # 清理消息内容 - text = text.replace('"', '"').replace('"', '"') - text = text.replace(''', "'").replace(''', "'") + text = text.replace('“', '"').replace('”', '"') + text = text.replace('‘', "'").replace('’', "'") text = text.replace('\n', ' ').replace('\r', ' ') text = text.replace('\t', ' ') text = re.sub(r'[\x00-\x1f\x7f-\x9f]', '', text) diff --git a/src/analysis/llm_analyzer.py b/src/analysis/llm_analyzer.py index 1f9a776..370e6cd 100644 --- a/src/analysis/llm_analyzer.py +++ b/src/analysis/llm_analyzer.py @@ -127,19 +127,4 @@ class LLMAnalyzer: 修复后的JSON文本 """ return fix_json(text) - - def _extract_topics_with_regex(self, result_text: str, max_topics: int) -> List[SummaryTopic]: - """ - 向后兼容的话题正则提取方法 - 现在委托给json_utils模块处理 - - Args: - result_text: 需要提取的文本 - max_topics: 最大话题数量 - - Returns: - 话题对象列表 - """ - - topics_data = extract_topics_with_regex(result_text, max_topics) - return [SummaryTopic(**topic) for topic in topics_data] \ No newline at end of file + \ No newline at end of file diff --git a/src/analysis/utils/json_utils.py b/src/analysis/utils/json_utils.py index 357357d..08dff0a 100644 --- a/src/analysis/utils/json_utils.py +++ b/src/analysis/utils/json_utils.py @@ -62,8 +62,14 @@ def fix_json(text: str) -> str: # 1. 修复缺失的逗号 text = re.sub(r'}\s*{', '}, {', text) - # 2. 确保字段名有引号 - text = re.sub(r'([{,]\s*)([a-zA-Z_][a-zA-Z0-9_]*)\s*:', r'\1"\2":', text) + # 2. 确保字段名有引号(仅在对象开始或逗号后,避免破坏字符串值) + def quote_field_names(match): + prefix = match.group(1) + key = match.group(2) + return f'{prefix}"{key}":' + + # 只在 { 或 , 后面匹配字段名,避免在字符串值中误匹配 + text = re.sub(r'([{,]\s*)([a-zA-Z_][a-zA-Z0-9_]*)\s*:', quote_field_names, text) # 3. 移除多余的逗号 text = re.sub(r',\s*}', '}', text)