"""Deterministic rule-based entity extraction for BER passenger queries.""" from __future__ import annotations import re from typing import Any class EntityExtractor: """Extract normalized airport entities without external models or services.""" TERMINALS = ( (re.compile(r"\b(?:terminal\s*(?:1|one)|t1)\b", re.I), "Terminal 1"), (re.compile(r"\b(?:terminal\s*(?:2|two)|t2)\b", re.I), "Terminal 2"), ) LEVEL = re.compile(r"\b(?:level\s*)?([EU][0-2])\b", re.I) FLIGHT = re.compile( r"\b(?:easyjet\s*)?([A-Z]{2}|U2)\s*[- ]?\s*(\d{1,4})\b", re.I ) GATE_EXPLICIT = re.compile(r"\bgate\s*([A-Z]\d{1,3})\b", re.I) GATE_BARE = re.compile(r"\b([A-Z]\d{1,3})\b", re.I) TIME_PATTERNS = ( (re.compile(r"\b(?:open|opening|closing|hours?)\b", re.I), "opening_hours"), (re.compile(r"\bnow\b", re.I), "now"), (re.compile(r"\btoday\b", re.I), "today"), (re.compile(r"\btomorrow\b", re.I), "tomorrow"), (re.compile(r"\blate\b", re.I), "late"), (re.compile(r"\bearly\b", re.I), "early"), ) ACCESSIBILITY_PATTERNS = ( (re.compile(r"\bwheel\s*chair\b", re.I), "wheelchair"), (re.compile(r"\breduced\s+mobility\b", re.I), "reduced_mobility"), (re.compile(r"\b(?:visually\s+impaired|tactile\s+guidance)\b", re.I), "visual_assistance"), (re.compile(r"\b(?:hearing\s+impaired|induction\s+loop)\b", re.I), "hearing_assistance"), (re.compile(r"\b(?:elderly|injured)\s+passenger\b", re.I), "general_assistance"), (re.compile(r"\b(?:assistance|mobility\s+service|call\s+box)\b", re.I), "general_assistance"), ) SERVICE_PATTERNS = ( ("baggage_tracing", re.compile(r"\b(?:baggage\s+tracing|lost\s+luggage|damaged\s+(?:bag|baggage|luggage))\b", re.I)), ("lost_property", re.compile(r"\b(?:lost\s+property|lost\s+(?:an?\s+)?item|lost\s+and\s+found)\b", re.I)), ("mobility_service", re.compile(r"\b(?:wheel\s*chair|mobility|reduced\s+mobility|assistance|accessibility|call\s+box|induction\s+loop|tactile\s+guidance)\b", re.I)), ("information_desk", re.compile(r"\b(?:information|help\s+desk|info\s+counter)\b", re.I)), ("train_station", re.compile(r"\b(?:train|railway|rail\s+station|station)\b", re.I)), ("bus_stop", re.compile(r"\b(?:bus|bus\s+stop|shuttle)\b", re.I)), ("check_in", re.compile(r"\b(?:check[ -]?in|counter|desk)\b", re.I)), ("security", re.compile(r"\b(?:security|screening|control)\b", re.I)), ("lounge", re.compile(r"\blounge\b", re.I)), ("food_court", re.compile(r"\b(?:food|restaurant|eat|coffee|meal)\b", re.I)), ("restroom", re.compile(r"\b(?:restroom|toilet|washroom|bathroom|wc)\b", re.I)), ("baggage", re.compile(r"\b(?:baggage|luggage|suitcase|bags?|belt|carousel)\b", re.I)), ("transport", re.compile(r"\b(?:taxi|transport)\b", re.I)), ("flight_status", re.compile(r"\b(?:delayed?|status|boarding|gate\s+changed|flight)\b", re.I)), ) @staticmethod def _empty() -> dict[str, Any]: return { "terminal": None, "level": None, "gate": None, "flight_number": None, "service": None, "time_phrase": None, "accessibility_need": None, "raw_matches": {}, } def extract(self, text: str | None) -> dict[str, Any]: """Extract normalized entities and preserve their raw matching fragments.""" result = self._empty() if not text or not text.strip(): return result raw: dict[str, str] = result["raw_matches"] for pattern, normalized in self.TERMINALS: match = pattern.search(text) if match: result["terminal"] = normalized raw["terminal"] = match.group(0) break level = self.LEVEL.search(text) if level: result["level"] = level.group(1).upper() raw["level"] = level.group(0) flight = self.FLIGHT.search(text) flight_span: tuple[int, int] | None = None if flight: result["flight_number"] = f"{flight.group(1).upper()}{flight.group(2)}" raw["flight_number"] = flight.group(0) flight_span = flight.span() gate = self.GATE_EXPLICIT.search(text) if gate: result["gate"] = gate.group(1).upper() raw["gate"] = gate.group(0) else: for candidate in self.GATE_BARE.finditer(text): if flight_span and candidate.start() >= flight_span[0] and candidate.end() <= flight_span[1]: continue result["gate"] = candidate.group(1).upper() raw["gate"] = candidate.group(0) break for service, pattern in self.SERVICE_PATTERNS: match = pattern.search(text) if match: result["service"] = service raw["service"] = match.group(0) break for pattern, normalized in self.TIME_PATTERNS: match = pattern.search(text) if match: result["time_phrase"] = normalized raw["time_phrase"] = match.group(0) break for pattern, normalized in self.ACCESSIBILITY_PATTERNS: match = pattern.search(text) if match: result["accessibility_need"] = normalized raw["accessibility_need"] = match.group(0) break return result