diff --git a/auto_reply.py b/auto_reply.py index d4bbcf1..900532d 100644 --- a/auto_reply.py +++ b/auto_reply.py @@ -117,6 +117,41 @@ class AutoReply: toks = set(t.lower() for t in jieba.cut(text) if len(t) >= 2) return toks - AutoReply._STOPWORDS + @staticmethod + def _fuzzy_token_match(set1: set, set2: set) -> bool: + """模糊token匹配:检查两个token集合中是否有部分匹配的词。 + 规则: + 1. 精确匹配:token完全相同 + 2. 子串匹配:一个token是另一个token的子串(至少2字符) + 3. 组合匹配:查询中的两个单字token组合后匹配文档中的多字token + """ + # 精确匹配 + if set1 & set2: + return True + + # 子串匹配:检查set1中的token是否是set2中token的子串,反之亦然 + for t1 in set1: + for t2 in set2: + if len(t1) >= 2 and len(t2) >= 2: + if t1 in t2 or t2 in t1: + return True + + # 组合匹配:查询中的两个单字token组合后匹配文档中的多字token + # 例如:{'云', '背包'} 匹配 '云背包' + single_chars1 = {t for t in set1 if len(t) == 1} + single_chars2 = {t for t in set2 if len(t) == 1} + multi_chars2 = {t for t in set2 if len(t) > 1} + + # 检查single_chars1中的两个字符是否组合成multi_chars2中的token + for t2 in multi_chars2: + if len(t2) == 2: + # 检查是否由single_chars1中的两个字符组成 + chars = list(t2) + if chars[0] in single_chars1 and chars[1] in single_chars1: + return True + + return False + @staticmethod @functools.lru_cache(maxsize=512) def _full_doc_text(path: str) -> str: @@ -255,7 +290,7 @@ class AutoReply: # ⭐ 排除 action_kw 词参与 body 覆盖统计,避免"使用"(verb)误命中 non_action_q = q_mean - action_kw non_action_body = body_tokens - action_kw - has_body_nonaction = bool(non_action_q & non_action_body) + has_body_nonaction = bool(non_action_q & non_action_body) or AutoReply._fuzzy_token_match(non_action_q, non_action_body) if not has_body_nonaction: # ⭐ 表格兜底(2026-08-17):词级匹配可能因近义词/OCR差异失败 # 例:查询"操作模式" vs 表格"操作方式"——body含'操作'但已被action_kw剔除, diff --git a/knowledge_base.py b/knowledge_base.py index 66ea1d3..5896834 100644 --- a/knowledge_base.py +++ b/knowledge_base.py @@ -45,6 +45,9 @@ class KnowledgeBase: # ⭐ 口语"怎么用"= 使用方法(用户常说"xx怎么用") # 修复"兑换码插件怎么用啊?"无法命中sdf1.md的使用方法section "怎么用": ["使用方法"], + # ⭐ 口语"云包"= 云背包(用户常说"云包插件",OCR识别"云背包"为"云包") + # 修复"怎么装那个云包插件?"无法命中beibao.md的安装方法section + "云包": ["云背包"], } @staticmethod