fix(ocr): 预分割阶段先剥离bot旧回复再检测噪声

问题:OCR混合文本(用户提问+bot旧回复)被 _is_noise_text 误判为噪声丢弃。
例:'运营方有权...@游戏专用服务器近期有哪些公告...' → 含'运营方有权'触发协议噪声检测→整段丢弃。

修复:在 _pre_split_noise_filter 中先调用 _strip_bot_reply_fragments 剥离bot旧回复,
再对剥离后的文本做 _is_noise_text 检测。
剥离后为空→丢弃;剥离后仍有噪声→丢弃;否则保留。
This commit is contained in:
youpaishidifu
2026-08-13 14:26:33 +08:00
parent d0e3950d6c
commit 93c50ecef3
+31 -23
View File
@@ -5735,13 +5735,15 @@ class MessageMonitor:
clean_segments = []
noise_count = 0
for i, seg in enumerate(segments):
is_noise = self._is_noise_text(seg)
# ⭐ 2026-08-13: 先剥离bot旧回复,再检测噪声,防止混合文本被误判为噪声
stripped_seg = self._strip_bot_reply_fragments(seg)
is_noise = self._is_noise_text(stripped_seg) if stripped_seg else True
if is_noise:
noise_count += 1
logger.debug(f"[{platform}] 预分割: 段[{i}] 含噪声特征,丢弃: {seg[:60]!r}")
logger.debug(f"[{platform}] 预分割: 段[{i}] 含噪声特征,丢弃: {stripped_seg[:60]!r}" if stripped_seg else f"[{platform}] 预分割: 段[{i}] 剥离bot回复后为空,丢弃: {seg[:60]!r}")
else:
clean_segments.append(seg)
logger.debug(f"[{platform}] 预分割: 段[{i}] 干净,保留: {seg[:60]!r}")
clean_segments.append(stripped_seg)
logger.debug(f"[{platform}] 预分割: 段[{i}] 干净,保留: {stripped_seg[:60]!r}")
# ---- 结果汇总 ----
if not clean_segments:
@@ -5889,26 +5891,32 @@ class MessageMonitor:
r'运营方.*权利|服务.*提供.*义务|'
r'我们.*保留.*权利|承担.*责任)'
)
if _AGREEMENT_PLAYER.search(text):
logger.debug(f"[ocr] 过滤器2结果: 检测到用户协议条款内容(正式法律用语),返回True: {text[:60]!r}")
return True
if _AGREEMENT_OPERATOR.search(text):
logger.debug(f"[ocr] 过滤器2结果: 检测到运营方条款内容,返回True: {text[:60]!r}")
return True
if _AGREEMENT_TITLE.match(text):
logger.debug(f"[ocr] 过滤器2结果: 检测到协议条款文档标题,返回True: {text[:60]!r}")
return True
# 同时包含"权利"和"义务"的正式陈述句(法律文档特征),排除疑问句
if '权利' in text and '义务' in text and not text.endswith(('?', '?', '吗', '呢')):
logger.debug(f"[ocr] 过滤器2结果: 检测到含'权利'+'义务'的法律文档陈述句,返回True: {text[:60]!r}")
return True
# ⭐ 拓展协议关键词检测:短文本中出现多个协议特征词 → 高概率协议内容
if len(text) < 80 and _AGREEMENT_KEYWORDS.search(text):
# 至少匹配2个协议特征关键词才拦截,避免误杀正常聊天
_match_count = len(_AGREEMENT_KEYWORDS.findall(text))
if _match_count >= 2:
logger.debug(f"[ocr] 过滤器2结果: 检测到多个协议特征词({_match_count}个),返回True: {text[:60]!r}")
# ⭐ 疑问词检测:文本含用户提问特征时,不应丢弃
_QUESTION_WORDS = ('?', '?', '吗', '呢', '哪些', '什么', '怎么', '如何', '为什么', '能', '可以')
is_question = any(q in text for q in _QUESTION_WORDS)
if is_question:
logger.debug(f"[ocr] 过滤器2: 检测到疑问词,跳过协议噪声检测")
else:
if _AGREEMENT_PLAYER.search(text):
logger.debug(f"[ocr] 过滤器2结果: 检测到用户协议条款内容(正式法律用语),返回True: {text[:60]!r}")
return True
if _AGREEMENT_OPERATOR.search(text):
logger.debug(f"[ocr] 过滤器2结果: 检测到运营方条款内容,返回True: {text[:60]!r}")
return True
if _AGREEMENT_TITLE.match(text):
logger.debug(f"[ocr] 过滤器2结果: 检测到协议条款文档标题,返回True: {text[:60]!r}")
return True
# 同时包含"权利"和"义务"的正式陈述句(法律文档特征)
if '权利' in text and '义务' in text:
logger.debug(f"[ocr] 过滤器2结果: 检测到含'权利'+'义务'的法律文档陈述句,返回True: {text[:60]!r}")
return True
# ⭐ 拓展协议关键词检测:短文本中出现多个协议特征词 → 高概率协议内容
if len(text) < 80 and _AGREEMENT_KEYWORDS.search(text):
# 至少匹配2个协议特征关键词才拦截,避免误杀正常聊天
_match_count = len(_AGREEMENT_KEYWORDS.findall(text))
if _match_count >= 2:
logger.debug(f"[ocr] 过滤器2结果: 检测到多个协议特征词({_match_count}个),返回True: {text[:60]!r}")
return True
# ⭐ 混合文字乱码检测:OCR将不同行/区域的文字混在一起
# 如"HHWH尸端自哪内柙登录槟J!" — Latin大写字母散落在CJK字符中间
# 正常聊天中Latin缩写是连续块(如"登录PHP服务器"、"GTA5玩家")