From be0cc208e7982663edf4485e4b941274ace5cf43 Mon Sep 17 00:00:00 2001 From: youpaishidifu Date: Wed, 16 Sep 2026 11:23:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D[=E7=9F=A5=E8=AF=86=E5=BA=93?= =?UTF-8?q?=E6=90=9C=E7=B4=A2]=EF=BC=9A=E8=B0=83=E6=95=B4=E6=9D=83?= =?UTF-8?q?=E9=87=8D=E7=A1=AE=E4=BF=9D'=E5=88=B7=E9=87=8F'=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=8C=B9=E9=85=8D=E6=AD=A3=E7=A1=AEsection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knowledge_base.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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))再做短语匹配