改进内容相关性检查:增加body内容匹配,增强日志诊断
- _has_content_relevance 增加 content/body 检查,防止TF-IDF匹配到heading无关但body相关的section - 增加 score 参数传递,用于日志记录 - 日志增加 doc_body 预览,便于诊断匹配失败原因
This commit is contained in:
+20
-8
@@ -45,11 +45,14 @@ class AutoReply:
|
||||
f"[{platform}] 匹配度 {score:.2f} < 阈值 {self._min_score},跳过回复"
|
||||
)
|
||||
return None
|
||||
# 内容相关性检查:查询词与匹配文档标题至少有一个共同2字+词,防偶然匹配
|
||||
if not self._has_content_relevance(msg, top_doc):
|
||||
# 内容相关性检查:查询词与匹配文档至少有一个共同2字+词,防偶然匹配
|
||||
if not self._has_content_relevance(msg, top_doc, score):
|
||||
heading = top_doc.get("heading", "")[:40]
|
||||
body_preview = (top_doc.get("content", "") or top_doc.get("body", ""))[:60]
|
||||
logger.debug(
|
||||
f"[{platform}] 内容相关性检查失败,跳过: msg={msg[:40]!r} doc_heading={heading!r}"
|
||||
f"[{platform}] 内容相关性检查失败,跳过: "
|
||||
f"msg={msg[:40]!r} score={score:.2f} doc_heading={heading!r} "
|
||||
f"doc_body={body_preview!r}"
|
||||
)
|
||||
return None
|
||||
with self._lock:
|
||||
@@ -73,18 +76,27 @@ class AutoReply:
|
||||
return {"platform": platform, "answer": answer, "sender": sender}
|
||||
|
||||
@staticmethod
|
||||
def _has_content_relevance(query: str, doc: dict) -> bool:
|
||||
"""查询词与匹配文档标题至少有一个共同2字+词,防偶然匹配。
|
||||
def _has_content_relevance(query: str, doc: dict, score: float = 0.0) -> bool:
|
||||
"""查询词与匹配文档至少有一个共同2字+词,防偶然匹配。
|
||||
短查询(≤2个2字+词)直接放行,不做严格检查。
|
||||
检查顺序:heading → title → content/body。
|
||||
"""
|
||||
q_tokens = set(t for t in jieba.cut(query) if len(t) >= 2)
|
||||
if len(q_tokens) <= 2:
|
||||
return True # 短查询不拦截
|
||||
# 1. 检查 heading + title
|
||||
doc_text = doc.get("heading", "") + " " + doc.get("title", "")
|
||||
d_tokens = set(t for t in jieba.cut(doc_text) if len(t) >= 2)
|
||||
if not (q_tokens & d_tokens):
|
||||
return False
|
||||
return True
|
||||
if q_tokens & d_tokens:
|
||||
return True
|
||||
# 2. 检查 content/body(TF-IDF匹配的段落正文)
|
||||
body = doc.get("content", "") or doc.get("body", "")
|
||||
if body:
|
||||
# 截断前500字防超长文档
|
||||
b_tokens = set(t for t in jieba.cut(body[:500]) if len(t) >= 2)
|
||||
if q_tokens & b_tokens:
|
||||
return True
|
||||
return False
|
||||
|
||||
def _get_answer(self, platform: str, msg: str, doc: dict, score: float) -> str or None:
|
||||
"""提取答案 + 防重复检查。
|
||||
|
||||
Reference in New Issue
Block a user