From 63af5aa14b3bb8cb4c06865a071d2a5bd49ab178 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Wed, 13 May 2026 18:32:54 -0700 Subject: [PATCH] fix: time window regex misses fuzzy quantifiers like 'last few hours' The relative-time regex only matched digits between 'last/past' and the unit, so 'last few hours' fell through to dateparser which then found the bare word 'hours' and resolved it as midnight local time. Extended the regex to capture 'few', 'couple of', 'several', 'a few' as approximate quantifiers, mapped to 3 units each. Numeric expressions and bare 'last hour' still work as before. --- app/services/diagnose.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/services/diagnose.py b/app/services/diagnose.py index 4d1c887..2f0c4c7 100644 --- a/app/services/diagnose.py +++ b/app/services/diagnose.py @@ -25,16 +25,20 @@ except ImportError: _RELATIVE_RE = re.compile( - r"\b(?:last|past)\s+(\d+)?\s*(minute|hour|day|week)s?\b", + r"\b(?:last|past)\s+(?:(?P\d+)|(?Pa\s+few|few|couple(?:\s+of)?|several))?\s*(?Pminute|hour|day|week)s?\b", re.IGNORECASE, ) _RELATIVE_UNITS = {"minute": 1, "hour": 60, "day": 1440, "week": 10080} +# Fuzzy quantifiers map to a reasonable span so "last few hours" → 3h window +_APPROX_N = 3 def _relative_window(match: re.Match) -> tuple[str, str]: """Convert a relative time match to (since_iso, until_iso).""" - n = int(match.group(1) or 1) - unit = match.group(2).lower() + n_str = match.group("n") + approx = match.group("approx") + unit = match.group("unit").lower() + n = int(n_str) if n_str else (_APPROX_N if approx else 1) minutes = n * _RELATIVE_UNITS[unit] return _last_n_minutes(minutes), _now_iso()