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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
"""Link clinical evidence text to UMLS graph nodes.

Primary backend: surface-form dictionary built from the filtered KG (``id_maps.parquet``)
plus optional enrichment from ``mrxns_eng`` / ``mrsty`` in the UMLS DuckDB.

Optional backend: QuickUMLS (when ``leveldb`` + ``quickumls`` are installed).
"""

from __future__ import annotations

import json
import re
import unicodedata
from pathlib import Path

import numpy as np
import polars as pl

from config.paths import (
    CUI_TO_GRAPH_IDX_JSON,
    GRAPH_ID_MAPS_PARQUET,
    QUERY_LINKER_CACHE_JSON,
    UMLS_DUCKDB_PATH,
)

# Clinical semantic types (diseases, drugs, symptoms, procedures, anatomy, findings).
CLINICAL_SEMTYPES: frozenset[str] = frozenset(
    {
        "T047",  # Disease or Syndrome
        "T048",  # Mental or Behavioral Dysfunction
        "T184",  # Sign or Symptom
        "T121",  # Pharmacologic Substance
        "T109",  # Organic Chemical
        "T123",  # Biologically Active Substance
        "T195",  # Antibiotic
        "T200",  # Clinical Drug
        "T023",  # Body Part, Organ, or Organ Component
        "T031",  # Body Substance
        "T033",  # Finding
        "T034",  # Laboratory or Test Result
        "T060",  # Diagnostic Procedure
        "T061",  # Therapeutic or Preventive Procedure
        "T037",  # Injury or Poisoning
        "T046",  # Pathologic Function
        "T191",  # Neoplastic Process
    }
)

PREFERRED_SABS: frozenset[str] = frozenset(
    {
        "SNOMEDCT_US",
        "MSH",
        "RXNORM",
        "MEDCIN",
        "NCI",
        "LNC",
        "ICD10CM",
        "ICD10",
        "ICD9CM",
        "NDDF",
        "MMSL",
        "VANDF",
    }
)

STOPWORDS: frozenset[str] = frozenset(
    {
        "a",
        "an",
        "the",
        "and",
        "or",
        "of",
        "in",
        "on",
        "at",
        "to",
        "for",
        "with",
        "without",
        "by",
        "from",
        "is",
        "are",
        "was",
        "were",
        "be",
        "been",
        "being",
        "has",
        "have",
        "had",
        "not",
        "no",
        "due",
        "associated",
        "secondary",
        "primary",
        "unspecified",
        "other",
        "specified",
        "history",
        "personal",
        "patient",
        "given",
        "then",
        "there",
        "will",
        "some",
        "any",
        "all",
        "both",
        "each",
        "than",
        "that",
        "this",
        "these",
        "those",
        "into",
        "during",
        "after",
        "before",
        "while",
        "when",
        "where",
        "which",
        "who",
        "whom",
        "whose",
        "if",
        "as",
        "but",
        "so",
        "such",
        "via",
        "per",
        "also",
        "only",
        "just",
        "very",
        "more",
        "most",
        "less",
        "least",
        "new",
        "old",
        "acute",
        "chronic",
        "stage",
        "type",
        "status",
        "present",
        "absent",
        "possible",
        "probable",
        "suspected",
    }
)

_SURFACE_RE = re.compile(r"[^a-z0-9]+")


def normalize_surface(text: str) -> str:
    """Lowercase, strip accents, collapse punctuation to spaces."""
    text = unicodedata.normalize("NFKD", text)
    text = "".join(ch for ch in text if not unicodedata.combining(ch))
    text = text.lower().strip()
    text = _SURFACE_RE.sub(" ", text)
    return " ".join(text.split())


def _tokenize(text: str) -> list[str]:
    return [t for t in normalize_surface(text).split() if t and t not in STOPWORDS]


def build_cui_to_graph_idx(
    id_maps_parquet: Path = GRAPH_ID_MAPS_PARQUET,
    max_graph_idx: int | None = None,
) -> dict[str, int]:
    """Map CUI -> best graph row index (highest degree node per CUI)."""
    scan = pl.scan_parquet(id_maps_parquet).select("idx", "cui", "degree")
    if max_graph_idx is not None:
        scan = scan.filter(pl.col("idx") < max_graph_idx)
    df = (
        scan.filter(pl.col("cui").is_not_null())
        .sort(["cui", "degree"], descending=[False, True])
        .group_by("cui")
        .agg(pl.col("idx").first())
        .collect()
    )
    return dict(zip(df["cui"].to_list(), df["idx"].to_list()))


def build_clinical_cuis(
    cui_to_idx: dict[str, int],
    umls_duckdb: Path | None = UMLS_DUCKDB_PATH,
) -> set[str]:
    """Filter CUIs to clinical semantic types when DuckDB is available."""
    if umls_duckdb is None or not Path(umls_duckdb).exists():
        return set(cui_to_idx)
    import duckdb

    con = duckdb.connect(str(umls_duckdb), read_only=True)
    con.register("graph_cuis", pl.DataFrame({"cui": list(cui_to_idx)}))
    sem = ",".join(f"'{s}'" for s in CLINICAL_SEMTYPES)
    rows = con.execute(
        f"""
        SELECT DISTINCT g.cui
        FROM graph_cuis g
        JOIN mrsty s ON g.cui = s.CUI
        WHERE s.STY IN ({sem})
        """
    ).fetchall()
    con.close()
    return {r[0] for r in rows} if rows else set(cui_to_idx)


def build_surface_lookup(
    cui_to_idx: dict[str, int],
    clinical_cuis: set[str],
    id_maps_parquet: Path = GRAPH_ID_MAPS_PARQUET,
    umls_duckdb: Path | None = UMLS_DUCKDB_PATH,
    max_graph_idx: int | None = None,
    min_surface_len: int = 3,
) -> dict[str, list[tuple[str, float]]]:
    """Build normalized surface form -> [(cui, score)] from KG + optional MRXNS."""
    lookup: dict[str, dict[str, float]] = {}

    def add(surface: str, cui: str, score: float) -> None:
        if cui not in clinical_cuis:
            return
        norm = normalize_surface(surface)
        if len(norm) < min_surface_len:
            return
        bucket = lookup.setdefault(norm, {})
        bucket[cui] = max(bucket.get(cui, 0.0), score)

    scan = pl.scan_parquet(id_maps_parquet).select("idx", "cui", "sab", "str")
    if max_graph_idx is not None:
        scan = scan.filter(pl.col("idx") < max_graph_idx)
    df = scan.filter(
        pl.col("cui").is_not_null()
        & pl.col("str").is_not_null()
        & (pl.col("str").str.len_chars() >= min_surface_len)
    ).collect()

    for cui, sab, surface in zip(
        df["cui"].to_list(), df["sab"].to_list(), df["str"].to_list()
    ):
        score = 1.0 if sab in PREFERRED_SABS else 0.85
        add(surface, cui, score)

    if umls_duckdb is not None and Path(umls_duckdb).exists():
        import duckdb

        con = duckdb.connect(str(umls_duckdb), read_only=True)
        con.register("graph_cuis", pl.DataFrame({"cui": list(clinical_cuis)}))
        sem = ",".join(f"'{s}'" for s in CLINICAL_SEMTYPES)
        rows = con.execute(
            f"""
            SELECT DISTINCT m.CUI, m.NSTR
            FROM mrxns_eng m
            JOIN graph_cuis g ON m.CUI = g.cui
            JOIN mrsty s ON m.CUI = s.CUI
            WHERE s.STY IN ({sem})
              AND length(m.NSTR) >= {int(min_surface_len)}
            """
        ).fetchall()
        con.close()
        for cui, nstr in rows:
            surface = " ".join(nstr.split())
            add(surface, cui, 0.9)

    return {k: sorted(v.items(), key=lambda x: -x[1]) for k, v in lookup.items()}


def save_cui_to_graph_idx(path: Path, cui_to_idx: dict[str, int]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(json.dumps(cui_to_idx), encoding="utf-8")


def load_cui_to_graph_idx(path: Path = CUI_TO_GRAPH_IDX_JSON) -> dict[str, int]:
    payload = json.loads(path.read_text(encoding="utf-8"))
    return {str(k): int(v) for k, v in payload.items()}


def ensure_cui_to_graph_idx(
    path: Path = CUI_TO_GRAPH_IDX_JSON,
    id_maps_parquet: Path = GRAPH_ID_MAPS_PARQUET,
    rebuild: bool = False,
) -> dict[str, int]:
    """Build or load CUI → graph row index (full graph, highest-degree node per CUI)."""
    if path.exists() and not rebuild:
        return load_cui_to_graph_idx(path)
    cui_to_idx = build_cui_to_graph_idx(id_maps_parquet, max_graph_idx=None)
    save_cui_to_graph_idx(path, cui_to_idx)
    return cui_to_idx


def save_linker_cache(
    path: Path,
    cui_to_idx: dict[str, int],
    surface_lookup: dict[str, list[tuple[str, float]]],
) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    payload = {
        "cui_to_idx": cui_to_idx,
        "surface_lookup": {k: list(v) for k, v in surface_lookup.items()},
    }
    path.write_text(json.dumps(payload), encoding="utf-8")


def load_linker_cache(
    path: Path,
) -> tuple[dict[str, int], dict[str, list[tuple[str, float]]]]:
    payload = json.loads(path.read_text(encoding="utf-8"))
    surface_lookup = {
        k: [(cui, float(score)) for cui, score in v]
        for k, v in payload["surface_lookup"].items()
    }
    return payload["cui_to_idx"], surface_lookup


class QueryEntityLinker:
    """Map query text to graph node indices with confidence scores."""

    def __init__(
        self,
        *,
        id_maps_parquet: Path = GRAPH_ID_MAPS_PARQUET,
        umls_duckdb: Path | None = UMLS_DUCKDB_PATH,
        cache_path: Path = QUERY_LINKER_CACHE_JSON,
        quickumls_path: Path | None = None,
        max_graph_idx: int | None = None,
        rebuild_cache: bool = False,
        min_confidence: float = 0.7,
        max_ngram: int = 6,
        quickumls_only: bool = False,
        skip_graph_cui_filter: bool = False,
    ):
        self.min_confidence = min_confidence
        self.max_ngram = max_ngram
        self.quickumls_only = quickumls_only
        self.skip_graph_cui_filter = skip_graph_cui_filter
        self._quickumls = None

        if quickumls_only and skip_graph_cui_filter:
            self.cui_to_idx = {}
            self.surface_lookup = {}
        elif quickumls_only:
            self.cui_to_idx = ensure_cui_to_graph_idx(
                rebuild=rebuild_cache,
                id_maps_parquet=id_maps_parquet,
            )
            self.surface_lookup = {}
        elif cache_path.exists() and not rebuild_cache:
            self.cui_to_idx, self.surface_lookup = load_linker_cache(cache_path)
        else:
            # Build full-graph linker cache; OOB embedding rows are filtered at pool time.
            cui_to_idx = build_cui_to_graph_idx(id_maps_parquet, max_graph_idx=None)
            clinical_cuis = build_clinical_cuis(cui_to_idx, umls_duckdb)
            self.surface_lookup = build_surface_lookup(
                cui_to_idx,
                clinical_cuis,
                id_maps_parquet=id_maps_parquet,
                umls_duckdb=umls_duckdb,
                max_graph_idx=None,
            )
            self.cui_to_idx = {
                c: i for c, i in cui_to_idx.items() if c in clinical_cuis
            }
            save_linker_cache(cache_path, self.cui_to_idx, self.surface_lookup)

        if quickumls_path is not None and Path(quickumls_path).exists():
            self._quickumls, load_err = self._try_load_quickumls(quickumls_path)
            if quickumls_only and self._quickumls is None:
                raise RuntimeError(
                    "quickumls_only=True but QuickUMLS could not be loaded.\n"
                    f"{load_err or 'Unknown error.'}\n"
                    "Fix: run ./setup.sh in the bundle root, then use ./run_export.sh "
                    "(not system python3)."
                )

    @staticmethod
    def _try_load_quickumls(quickumls_path: Path):
        import sys

        try:
            from quickumls import QuickUMLS
        except ImportError as exc:
            return None, (
                f"ImportError: {exc}\n"
                f"  python: {sys.executable}\n"
                "  Hint: create the venv with ./setup.sh and run via ./run_export.sh"
            )
        try:
            matcher = QuickUMLS(
                str(quickumls_path),
                threshold=0.7,
                accepted_semtypes=sorted(CLINICAL_SEMTYPES),
            )
        except Exception as exc:
            return None, (
                f"{type(exc).__name__}: {exc}\n"
                f"  python: {sys.executable}\n"
                f"  quickumls_data: {quickumls_path}"
            )
        return matcher, None

    def _link_quickumls(self, text: str) -> list[tuple[str, float]]:
        if self._quickumls is None:
            return []
        out: list[tuple[str, float]] = []
        for match_group in self._quickumls.match(
            text, best_match=True, ignore_syntax=False
        ):
            for m in match_group:
                cui = m.get("cui")
                sim = float(m.get("similarity", 0.0))
                if cui and sim >= self.min_confidence:
                    out.append((cui, sim))
        return out

    def _link_dictionary(self, text: str) -> list[tuple[str, float]]:
        tokens = _tokenize(text)
        if not tokens:
            return []

        matches: dict[str, float] = {}
        n = len(tokens)
        for start in range(n):
            for length in range(min(self.max_ngram, n - start), 0, -1):
                phrase = " ".join(tokens[start : start + length])
                hits = self.surface_lookup.get(phrase)
                if not hits:
                    continue
                for cui, score in hits:
                    matches[cui] = max(matches.get(cui, 0.0), score)
                break  # longest match at this start position

        # Also try semicolon / clause segments (common in evidence strings).
        for clause in re.split(r"[;,\n]+", text):
            clause = clause.strip()
            if not clause:
                continue
            norm_clause = normalize_surface(clause)
            if norm_clause in self.surface_lookup:
                for cui, score in self.surface_lookup[norm_clause]:
                    matches[cui] = max(matches.get(cui, 0.0), score)

        return sorted(matches.items(), key=lambda x: -x[1])

    def link_cuis(self, text: str) -> list[tuple[str, float]]:
        """Return (cui, confidence) pairs for a query."""
        matches: dict[str, float] = {}
        if self.quickumls_only:
            for cui, score in self._link_quickumls(text):
                if self.skip_graph_cui_filter or cui in self.cui_to_idx:
                    matches[cui] = max(matches.get(cui, 0.0), score)
        else:
            for cui, score in self._link_dictionary(text):
                matches[cui] = max(matches.get(cui, 0.0), score)
            for cui, score in self._link_quickumls(text):
                if cui in self.cui_to_idx:
                    matches[cui] = max(matches.get(cui, 0.0), score)
        return sorted(matches.items(), key=lambda x: -x[1])

    def link_graph_nodes(self, text: str) -> list[tuple[int, float]]:
        """Return (graph_row_idx, confidence) pairs for a query."""
        out: list[tuple[int, float]] = []
        for cui, score in self.link_cuis(text):
            idx = self.cui_to_idx.get(cui)
            if idx is not None:
                out.append((int(idx), score))
        return out

    def coverage_stats(self, texts: list[str]) -> dict[str, float]:
        """Compute entity-linking coverage over a list of queries."""
        n = len(texts)
        if n == 0:
            return {
                "n": 0,
                "pct_with_match": 0.0,
                "pct_zero_match": 100.0,
                "avg_entities": 0.0,
            }
        counts = [len(self.link_graph_nodes(t)) for t in texts]
        with_match = sum(1 for c in counts if c > 0)
        return {
            "n": n,
            "pct_with_match": 100.0 * with_match / n,
            "pct_zero_match": 100.0 * (n - with_match) / n,
            "avg_entities": float(np.mean(counts)),
            "median_entities": float(np.median(counts)),
            "max_entities": int(max(counts)),
        }