File size: 5,594 Bytes
7ca0188 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | """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
|