File size: 7,797 Bytes
1fe0c4a
 
 
 
3daa112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fe0c4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3daa112
 
1fe0c4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3daa112
1fe0c4a
 
 
3daa112
 
1fe0c4a
3daa112
 
 
1fe0c4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3daa112
 
 
 
 
 
 
1fe0c4a
 
3daa112
 
 
 
 
 
 
1fe0c4a
 
 
 
3daa112
 
 
 
 
 
 
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import re
from typing import Dict, Optional
from rapidfuzz import fuzz

CHOKEPOINT_ALIASES = {
    "suez": "suez_canal",
    "suez canal": "suez_canal",
    "the canal": "suez_canal",
    "hormuz": "strait_of_hormuz",
    "strait of hormuz": "strait_of_hormuz",
    "hormuz strait": "strait_of_hormuz",
    "persian gulf": "strait_of_hormuz",
    "malacca": "strait_of_malacca",
    "strait of malacca": "strait_of_malacca",
    "malacca strait": "strait_of_malacca",
    "panama": "panama_canal",
    "panama canal": "panama_canal",
    "bab el-mandeb": "bab_el_mandeb",
    "bab el mandeb": "bab_el_mandeb",
    "bab elmandeb": "bab_el_mandeb",
    "red sea": "bab_el_mandeb",
    "bosporus": "bosporus",
    "turkish straits": "bosporus",
    "istanbul strait": "bosporus",
    "dardanelles": "bosporus",
}

PORT_ALIASES = {
    "rotterdam": "rotterdam",
    "hamburg": "hamburg",
    "antwerp": "antwerp",
    "singapore": "singapore",
    "shanghai": "shanghai",
    "shenzhen": "shenzhen",
    "hong kong": "hong_kong",
    "busan": "busan",
    "dubai": "dubai",
    "jebel ali": "dubai",
    "fujairah": "fujairah",
    "los angeles": "los_angeles",
    "long beach": "long_beach",
    "new york": "new_york",
    "piraeus": "piraeus",
    "istanbul": "istanbul",
    "mumbai": "mumbai",
    "colon": "colon",
    "colombo": "colombo",
    "jeddah": "jeddah",
}

CARRIER_ALIASES = {
    "maersk": "maersk",
    "msc": "msc",
    "mediterranean shipping": "msc",
    "cma cgm": "cma_cgm",
    "cma": "cma_cgm",
    "cosco": "cosco",
    "hapag-lloyd": "hapag_lloyd",
    "hapag lloyd": "hapag_lloyd",
    "ocean network express": "one",
    "evergreen": "evergreen",
    "yang ming": "yang_ming",
    "zim": "zim",
    "pil": "pil",
}

SEVERITY_KEYWORDS = {
    "full": [
        "full blockage",
        "full closure",
        "completely blocked",
        "total closure",
        "shut down",
        "shut down completely",
        "completely shut",
    ],
    "temporary": [
        "temporary",
        "maintenance",
        "brief",
        "short-term",
        "reopening soon",
        "brief closure",
    ],
    "partial": [
        "partial",
        "reduced capacity",
        "restricted",
        "attacks",
        "houthi",
        "drone",
        "missile",
        "disrupted",
    ],
}

FUZZY_THRESHOLD = 75

_MIN_TOKEN_LENGTH = 3

_STOP_WORDS = frozenset(
    {
        "the",
        "and",
        "for",
        "are",
        "but",
        "not",
        "you",
        "all",
        "can",
        "had",
        "her",
        "was",
        "one",
        "our",
        "out",
        "has",
        "have",
        "been",
        "from",
        "this",
        "that",
        "with",
        "they",
        "will",
        "what",
        "when",
        "make",
        "like",
        "just",
        "over",
        "such",
        "take",
        "than",
        "them",
        "very",
        "does",
        "into",
        "also",
        "some",
        "could",
        "would",
        "should",
        "about",
        "which",
        "their",
        "there",
        "these",
        "those",
        "being",
        "after",
        "before",
        "between",
        "through",
        "during",
        "without",
    }
)

_PHONETIC_MAP: Optional[Dict[str, Dict[str, str]]] = None


def _metaphone(word: str) -> str:
    code = []
    i = 0
    w = word.upper()
    n = len(w)
    if n == 0:
        return ""

    if w[0] in ("A", "E", "I", "O", "U"):
        code.append("A")
    elif w[0] == "K" or w[0] == "J":
        code.append("K")
    elif w[0] == "G":
        code.append("K")
    elif w[0] == "P":
        code.append("P")
    elif w[0] == "B":
        code.append("P")
    elif w[0] in ("S", "Z", "C"):
        code.append("S")
    elif w[0] == "T" or w[0] == "D":
        code.append("T")
    elif w[0] == "F" or w[0] == "V":
        code.append("F")
    elif w[0] == "M" or w[0] == "N":
        code.append("M")
    elif w[0] == "L":
        code.append("L")
    elif w[0] == "R":
        code.append("R")
    elif w[0] == "H":
        code.append("H")
    else:
        code.append(w[0])

    i = 1
    while i < n and len(code) < 6:
        c = w[i]
        if c in ("A", "E", "I", "O", "U"):
            i += 1
            continue
        if c in ("B", "P", "F", "V"):
            code.append(c)
        elif c in ("C", "S", "K", "G", "J", "Q", "X", "Z"):
            code.append("S")
        elif c in ("D", "T"):
            code.append("T")
        elif c == "L":
            code.append("L")
        elif c in ("M", "N"):
            code.append("M")
        elif c == "R":
            code.append("R")
        elif c == "H":
            code.append("H")
        i += 1

    return "".join(code)


def _build_phonetic_map(alias_dict: dict) -> Dict[str, str]:
    mapping: dict[str, str] = {}
    for alias, canonical in alias_dict.items():
        tokens = alias.split()
        for token in tokens:
            ph = _metaphone(token)
            if ph and ph not in mapping:
                mapping[ph] = canonical
    return mapping


def _get_phonetic_maps() -> Dict[str, Dict[str, str]]:
    global _PHONETIC_MAP
    if _PHONETIC_MAP is None:
        _PHONETIC_MAP = {
            "chokepoints": _build_phonetic_map(CHOKEPOINT_ALIASES),
            "ports": _build_phonetic_map(PORT_ALIASES),
            "carriers": _build_phonetic_map(CARRIER_ALIASES),
        }
    return _PHONETIC_MAP


def _extract_tokens(text: str) -> list[str]:
    return re.findall(r"\b[a-z]+\b", text.lower())


def _extract_phrases(text: str, max_words: int = 3) -> list[str]:
    words = _extract_tokens(text)
    phrases: list[str] = []
    for length in range(2, max_words + 1):
        for i in range(len(words) - length + 1):
            phrases.append(" ".join(words[i : i + length]))
    return phrases


def _find_entities(
    text: str, alias_dict: dict, phonetic_map: Optional[dict] = None
) -> list:
    lower = text.lower()
    found = set()

    for alias, canonical in alias_dict.items():
        if alias in lower:
            found.add(canonical)

    tokens = _extract_tokens(lower)
    phrases = _extract_phrases(lower, max_words=3)
    candidates = [
        t for t in tokens if t not in _STOP_WORDS and len(t) >= _MIN_TOKEN_LENGTH
    ] + phrases

    for alias, canonical in alias_dict.items():
        if canonical in found:
            continue
        for candidate in candidates:
            score = fuzz.ratio(candidate, alias)
            if score >= FUZZY_THRESHOLD:
                found.add(canonical)
                break

    if not found and phonetic_map:
        for token in tokens:
            if len(token) < _MIN_TOKEN_LENGTH or token in _STOP_WORDS:
                continue
            ph = _metaphone(token)
            if ph in phonetic_map:
                found.add(phonetic_map[ph])

    return sorted(found)


def _detect_severity(text: str, has_chokepoints: bool) -> str:
    lower = text.lower()
    for level, keywords in SEVERITY_KEYWORDS.items():
        for kw in keywords:
            pattern = r"\b" + re.escape(kw) + r"\b"
            if re.search(pattern, lower):
                return level
    if has_chokepoints:
        return "partial"
    return "partial"


def extract_entities(message: str) -> dict:
    maps = _get_phonetic_maps()
    chokepoints = _find_entities(message, CHOKEPOINT_ALIASES, maps["chokepoints"])
    ports = _find_entities(message, PORT_ALIASES, maps["ports"])
    carriers = _find_entities(message, CARRIER_ALIASES, maps["carriers"])
    severity = _detect_severity(message, bool(chokepoints))
    return {
        "chokepoints": chokepoints,
        "ports": ports,
        "carriers": carriers,
        "severity": severity,
    }