fix: OCR噪声检测 - 用户消息与bot回复相似度≥90%时跳过
问题:OCR把bot回复内容误识别为用户消息,导致重复回复 根因:EasyOCR读取屏幕时,把右侧bot回复区域的文本也读取为用户消息 修复: - 新增_last_bot_reply_content变量存储最近bot回复内容 - record_bot_reply方法更新该变量 - 消息处理前检查相似度,≥90%则跳过 - 添加SequenceMatcher导入
This commit is contained in:
+14
@@ -8,6 +8,7 @@ import unicodedata
|
||||
import faulthandler
|
||||
import sqlite3
|
||||
import hashlib
|
||||
from difflib import SequenceMatcher
|
||||
from typing import Optional, Callable, List
|
||||
|
||||
faulthandler.enable() # 捕获 C 扩展段错误,输出 traceback 到 stderr
|
||||
@@ -231,6 +232,8 @@ class MessageHistory:
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
# 更新最近bot回复内容(用于检测OCR噪声导致的重复)
|
||||
self._last_bot_reply_content[platform] = content.strip()
|
||||
|
||||
def record_user_message(self, platform: str, content: str):
|
||||
"""记录用户消息(用于后续去重)"""
|
||||
@@ -294,6 +297,8 @@ class MessageMonitor:
|
||||
self._last_texts: dict = {}
|
||||
# 最新消息文本(供 GUI 展示)
|
||||
self._last_msg: dict = {}
|
||||
# 最近一次bot回复内容(用于检测OCR噪声导致的重复)
|
||||
self._last_bot_reply_content: dict = {} # platform → content
|
||||
|
||||
# 轮询计数
|
||||
self._poll_count: dict = {}
|
||||
@@ -5283,6 +5288,15 @@ class MessageMonitor:
|
||||
else:
|
||||
# ⭐ 清理OCR噪声前缀(如"必选,")后再搜索知识库
|
||||
cleaned_item = self._clean_ocr_prefix(item)
|
||||
# ⭐ 检查是否与最近bot回复内容相同(OCR噪声干扰,把bot回复当作用户消息)
|
||||
last_reply = self._last_bot_reply_content.get(platform, "")
|
||||
if last_reply and cleaned_item and len(cleaned_item) >= 4:
|
||||
similarity = SequenceMatcher(None, cleaned_item, last_reply).ratio()
|
||||
if similarity >= 0.9:
|
||||
logger.info(f"[{platform}] OCR噪声检测: 与最近bot回复相似度{similarity:.2%},跳过: {cleaned_item[:60]}")
|
||||
seen.add(cleaned_item)
|
||||
self._last_new_msg_time[platform] = time.time()
|
||||
return # 跳过本条消息处理
|
||||
self._last_new_msg_time[platform] = time.time()
|
||||
self._last_msg[platform] = cleaned_item
|
||||
engine_tag = self._last_chosen_engine.get(platform, "")
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user