From 8d648f37fb90ba713b073dd59a0941497c9d2a25 Mon Sep 17 00:00:00 2001 From: youpaishidifu Date: Thu, 13 Aug 2026 15:24:46 +0800 Subject: [PATCH] =?UTF-8?q?fix(kb):=20=E6=90=9C=E7=B4=A2=E9=98=B6=E6=AE=B5?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E6=9F=A5=E8=AF=A2=E5=81=9C=E7=94=A8=E8=AF=8D?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2=E9=80=9A=E7=94=A8=E5=90=8D=E8=AF=8D?= =?UTF-8?q?=E5=AF=BC=E8=87=B4TF-IDF=E8=99=9A=E9=AB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:查询"服务器的公告"时,"服务器"的IDF=3.2虚高, player.md的"运营方权利"段落含"服务器"多次→keyword_score=12.8, 压过index.md的"近期公告"(keyword_score=3.2)。 修复:在search()中使用AutoReply._STOPWORDS过滤查询token, 只保留"公告"等核心查询词参与评分。 效果:"服务器的公告"现在正确匹配到index.md的"## 🔈近期公告"。 --- knowledge_base.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/knowledge_base.py b/knowledge_base.py index ccf87f5..d420038 100644 --- a/knowledge_base.py +++ b/knowledge_base.py @@ -505,6 +505,14 @@ class KnowledgeBase: if len(query_stripped) < 2 or len(tokens) == 0: return [] + # ⭐ 过滤查询停用词:通用名词(服务器、玩家、游戏等)会导致TF-IDF虚高 + # 修复"服务器的公告"被player.md"运营方权利"压过index.md"近期公告"的问题 + from auto_reply import AutoReply + query_tokens_filtered = [t for t in tokens if t not in AutoReply._STOPWORDS] + if not query_tokens_filtered: + # 所有token都是停用词,回退到原始tokens + query_tokens_filtered = tokens + # 对每个扩展查询计算 TF-IDF,取最高分 tfidf_scores = np.zeros(len(self.docs)) for eq in expanded_queries: @@ -512,11 +520,11 @@ class KnowledgeBase: scores = cosine_similarity(query_vec, self._tfidf_matrix)[0] tfidf_scores = np.maximum(tfidf_scores, scores) - # 关键词命中 + # 关键词命中:使用过滤后的token expanded_tokens = set() for eq in expanded_queries: - expanded_tokens.update(t for t in jieba.cut(eq) if len(t) >= 2) - query_tokens_set = expanded_tokens + expanded_tokens.update(t for t in jieba.cut(eq) if len(t) >= 2 and t not in AutoReply._STOPWORDS) + query_tokens_set = expanded_tokens if expanded_tokens else set(query_tokens_filtered) idf_dict = {} if hasattr(self._vectorizer, 'idf_') and self._vectorizer.idf_ is not None: