diff --git a/knowledge_base.py b/knowledge_base.py index 535f166..7f8387c 100644 --- a/knowledge_base.py +++ b/knowledge_base.py @@ -532,6 +532,9 @@ class KnowledgeBase: if len(query_stripped) < 2 or len(tokens) == 0: return [] + # ⭐ 检测安装意图:查询包含"安装/怎么装/如何安装"等词 + has_install_keyword = any(w in query_stripped for w in ('安装', '怎么装', '如何安装', '安装方法', '安装步骤', '安装教程')) + # ⭐ 过滤查询停用词:通用名词(服务器、玩家、游戏等)会导致TF-IDF虚高 # 修复"服务器的公告"被player.md"运营方权利"压过index.md"近期公告"的问题 from auto_reply import AutoReply @@ -583,6 +586,10 @@ class KnowledgeBase: # 核心词:查询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) + # ⭐ 修复:安装意图查询时,将"安装"也加入核心词列表 + # 防止"背包插件怎么装"被protect.md的"登录插件"链接文本压过 + if has_install_keyword: + query_core_tokens.add('安装') for i, doc in enumerate(self.docs): heading = doc.get("heading", "") # 清理链接语法 @@ -884,6 +891,15 @@ class KnowledgeBase: body = doc.get("body", "").strip() heading = doc.get("heading", "") if body.startswith('|') and '|' in body[1:] and ('操作' in body or '支持' in body): + # ⭐ 修复:安装/下载意图查询时,不强制加入无关表格 + # 防止"背包插件怎么装"被protect.md"操作方式|是否支持"表格压过 + _query_is_install_or_download = has_install_keyword or download_intent + if _query_is_install_or_download: + # 安装/下载意图:表格必须含安装/下载相关词才boost + _install_or_dl_terms = {'安装', '下载', '前置', '插件', '依赖'} + _tbl_has_relevance = any(t in body for t in _install_or_dl_terms) + if not _tbl_has_relevance: + continue # 跳过无关表格 # 检查heading是否与查询语义相关 h_tokens = set(t for t in jieba.cut(heading) if len(t) >= 2) heading_relevant = bool(query_tokens_set & h_tokens)