fix: 修复OCR时间戳过滤失效问题,过去日期+问号放行,纯历史消息丢弃 (Task #1659)

This commit is contained in:
youpaishidifu
2026-09-01 10:32:52 +08:00
parent 1f8be2b569
commit b23b243695
+35
View File
@@ -6809,6 +6809,25 @@ class MessageMonitor:
# ⭐ 2026-08-15 修复:扩展正则支持中文日期格式(2026年8月3日)
# ⭐ 2026-08-29 修复:检测日期是否附带时间——bot表格只有日期无时间,QQ消息日期+时分
raw_dates = re.findall(r'\d{4}[/-]\d{1,2}[/-]\d{1,2}|\d{4}年\d{1,2}月\d{1,2}日', text)
# ⭐ Task #1659: 在日期剥离前记录是否有过去日期(用于后续"无日期信息"分支)
# 当日期被剥离后,dates为空,但原始文本可能包含过去日期的时间戳
# 需要记录这个信息,避免把昨天的时间戳误判为"未来时间戳"
_raw_has_past_date = False
if raw_dates:
_today = datetime.now().date()
for _rd in raw_dates:
try:
_parts = _rd.replace('-', '/').replace('年', '/').replace('月', '/').replace('日', '').split('/')
if len(_parts) == 3:
_y, _m, _d = int(_parts[0]), int(_parts[1]), int(_parts[2])
if 1900 <= _y <= 2100 and 1 <= _m <= 12 and 1 <= _d <= 31:
_date_obj = datetime(_y, _m, _d).date()
if (_today - _date_obj).days > 0:
_raw_has_past_date = True
logger.debug(f"[ocr] Task#1659: 剥离前检测到过去日期: {_rd} (距今{(_today - _date_obj).days}天)")
break
except (ValueError, IndexError):
pass
# 检测每个日期后是否紧跟时间(年月日+时:分 → 真消息;只有年月日 → bot表格)
_dates_with_time = 0
_dates_without_time = 0
@@ -7042,6 +7061,22 @@ class MessageMonitor:
logger.debug(f"[ocr] 过滤器1结果: 检测到OCR识别错误的日期模式且时间戳不新鲜,丢弃: {text[:80]!r}")
return False
logger.debug(f"[ocr] 过滤器1结果: 无日期信息,放行给下游DB查重")
# ⭐ Task #1659 修复:当原始文本包含过去日期时,时间戳是过去日期的时间
# 不应被"未来时间戳"逻辑丢弃。例:原始文本含"2026/8/31"(昨天)+"21:07:10"
# 日期被剥离后,dates=[],但时间戳21:07:10是昨天的时间,应该放行。
# 策略:检测到过去日期上下文时,如果有问号(用户查询),放行;否则丢弃(纯历史消息)
if _raw_has_past_date:
# 检查最后一个时间戳之后是否有问号(用户查询信号)
if timestamps:
_last_ts = timestamps[-1]
_last_ts_pos = normalized.rfind(_last_ts)
_after_last_ts = normalized[_last_ts_pos + len(_last_ts):] if _last_ts_pos >= 0 else ''
if '?' in _after_last_ts or '?' in _after_last_ts:
logger.debug(f"[ocr] Task#1659: 检测到原始文本含过去日期+问号(用户查询),放行给下游DB查重")
return True
# 无问号:纯历史消息,丢弃
logger.debug(f"[ocr] Task#1659: 检测到原始文本含过去日期但无问号(纯历史消息),丢弃")
return False
# 如果有时间戳,检查时间戳新鲜度(可选,用于快速放行)
if timestamps:
for ts in timestamps: