From 561b054db86bff6f903fff36c565ba9e9cf300a8 Mon Sep 17 00:00:00 2001 From: Adam Moussa Date: Thu, 23 Jul 2026 14:55:59 -0400 Subject: [PATCH] fix: Refactor `comment_intent` to use `_contains_url_with_host` for URL host matching. --- lambdas/classifier/classify.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lambdas/classifier/classify.py b/lambdas/classifier/classify.py index c2493c3..1c575b4 100644 --- a/lambdas/classifier/classify.py +++ b/lambdas/classifier/classify.py @@ -25,6 +25,7 @@ from __future__ import annotations import html import os import re +from urllib.parse import unquote, urlparse # Axis 2 — Hold Reason → category. HOLD_TO_CATEGORY = { @@ -118,6 +119,19 @@ def strip_html(comment: str | None) -> str: # --------------------------------------------------------------------------- +def _contains_url_with_host(text: str, expected_host: str) -> bool: + """Return True when ''text'' contains an absolute URL with hostname ''expected_host''.""" + for raw_url in re.findall(r"https?://[^\s<>'\"()]+", text, flags=re.IGNORECASE): + candidate = unquote(raw_url).rstrip(".,;:!?)]}\"'") + try: + host = urlparse(candidate).hostname + except ValueError: + continue + if host and host.lower() == expected_host: + return True + return False + + def comment_intent(comment: str) -> str | None: """Ordered, most-specific-first rule ladder over the *stripped* text. @@ -147,7 +161,7 @@ def comment_intent(comment: str) -> str | None: # -- SIM Ticket: Amazon SIM tooling references. if ( re.search(r"\bsim\b.*\b(ticket|tt|v\d{6,})\b", low) - or "t.corp.amazon.com" in low + or _contains_url_with_host(t, "t.corp.amazon.com") or re.search(r"\bsim\s*tt\b", low) ): return "SIM Ticket"