diff --git a/monitor.py b/monitor.py index e1fbd03..a413901 100644 --- a/monitor.py +++ b/monitor.py @@ -4794,6 +4794,44 @@ class MessageMonitor: ) stripped_bot = True break + # 策略4:头部剥离——OCR把bot回复拼在用户消息前面("小帽子"问题) + # 例:"官方下载 bbsmc分流 那我知道咱们服务器有哪些公告信息呀?" + # → "那我知道咱们服务器有哪些公告信息呀?" + # 修复:定位中文疑问词(那/请问/想知道等),剥离问句前的bot回复头部 + if not stripped_bot: + # 常见中文疑问词/短语,用于定位用户消息的起始位置 + _question_markers = [ + '那我知道', '那请问', '请问', '想知道', '想问', + '怎么', '哪里', '什么', '哪些', '哪个', + ] + _q_start = len(ts) + for _marker in _question_markers: + _pos = ts.find(_marker) + if _pos >= 0 and _pos < _q_start: + _q_start = _pos + # 如果没找到疑问词,回退到找问号 + if _q_start == len(ts): + _q_mark = max(ts.find('?'), ts.find('?')) + if _q_mark >= 0: + _q_start = _q_mark + + _head = ts[:_q_start].strip() + _tail = ts[_q_start:] + if _head and len(_head) >= 3 and _tail: + # 检查头部是否匹配已知的bot回复 + for _r in replied: + if len(_r) < 5: + continue + _head_norm = re.sub(r'[\s\W_]+', '', _head) + _r_norm = re.sub(r'[\s\W_]+', '', _r) + if _head_norm and _r_norm and (_head_norm == _r_norm or _head_norm in _r_norm): + ts = _tail + stripped_bot = True + logger.debug( + f"[qq] 头部匹配剥离bot回复: {ts!r} <- " + f"{_head!r} (匹配bot reply: {_r[:30]!r})" + ) + break if stripped_bot: ts = re.sub(r'\s+', ' ', ts).strip().strip(',').strip() return ts