File size: 942 Bytes
0a3aede 2c1c247 3f9a9ea 2c1c247 3f9a9ea 2c1c247 0a3aede 3f9a9ea 2c1c247 3f9a9ea |
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 |
# mcp/nlp.py
import spacy
from scispacy.linking import EntityLinker
@spacy.util.cache_dir("~/.cache/scispacy")
def load_model():
nlp = spacy.load("en_core_sci_scibert")
linker = EntityLinker(name="umls", resolve_abbreviations=True, threshold=0.75)
nlp.add_pipe(linker)
return nlp
nlp = load_model()
def extract_umls_concepts(text: str) -> list[dict]:
"""
Returns unique UMLS concepts with confidence scores and semantic types.
"""
doc = nlp(text)
best = {}
for ent in doc.ents:
for cui, score in ent._.umls_ents:
meta = nlp.get_pipe("scispacy_linker").kb.cui_to_entity[cui]
if cui not in best or score > best[cui]["score"]:
best[cui] = {
"cui": cui,
"name": meta.canonical_name,
"score": float(score),
"types": meta.types
}
return list(best.values())
|