diff --git a/knowledge_base.py b/knowledge_base.py index ce55484..535f166 100644 --- a/knowledge_base.py +++ b/knowledge_base.py @@ -572,10 +572,25 @@ class KnowledgeBase: for i, doc in enumerate(self.docs): heading_hits = sum(idf_dict.get(t, 1.0) for t in query_tokens_set if t in heading_token_sets[i]) body_hits = sum(idf_dict.get(t, 1.0) for t in query_tokens_set if t in self._body_token_sets[i]) - keyword_scores[i] = heading_hits * 3 + body_hits + # ⭐ 增加heading_hits权重到5,确保heading包含查询词的section得分更高 + # 修复"刷量违规是什么意思?"返回违规行为section而非恶意交易/刷量section的问题 + keyword_scores[i] = heading_hits * 5 + body_hits combined_scores = 0.7 * tfidf_scores + 0.3 * keyword_scores + # ⭐ 额外奖励:当heading包含查询中的核心词时,给予乘性奖励 + # 修复"刷量违规是什么意思?"返回违规行为section而非恶意交易/刷量section的问题 + # 核心词:查询tokens中长度>=2且不在停用词表中的词 + from auto_reply import AutoReply + query_core_tokens = set(t for t in jieba.cut(query_stripped) if len(t) >= 2 and t not in AutoReply._STOPWORDS) + for i, doc in enumerate(self.docs): + heading = doc.get("heading", "") + # 清理链接语法 + heading_clean = re.sub(r'\[([^\]]*)\]\([^\)]+\)', r'\1', heading).lower() + # 检查heading是否包含任何核心词 + if any(t in heading_clean for t in query_core_tokens): + combined_scores[i] *= 2.0 + # 内容质量调整:惩罚标题-only/正文过短的 section,奖励正文含查询词的 section # 避免"# Sdf1系列登录插件使用说明"这种纯标题段因为标题词匹配就排第一 # ⭐ 修复:排除heading中的Markdown链接语法(如 [text](url))再做短语匹配