NLP using scispaCy

nlp-with-scispacy.py
import scispacy
import spacy
from scispacy.umls_linking import UmlsEntityLinker
from spacy import displacy

# choose a learned model
# nlp = spacy.load("en_core_sci_sm")
# nlp = spacy.load("en_core_sci_md")
nlp = spacy.load("en_ner_bc5cdr_md")

linker = UmlsEntityLinker(resolve_abbreviations=True)
nlp.add_pipe(linker)

# original text swiped from this COVID-19 Interim User Guide Wiki page
# https://wiki.cdisc.org/x/uQspBg
# text = """Data collection may include questions about groups of symptoms, such as
# GI symptoms (nausea, vomiting, diarrhea)
# Cough (non-productive, productive, or haemoptisis)"""

# with typo corrected, from haemoptisis to hemoptysis
text = """Data collection may include questions about groups of symptoms, such as
GI symptoms (nausea, vomiting, diarrhea)
Cough (non-productive, productive, or hemoptysis)"""

doc = nlp(text)

entities = doc.ents
for entity in entities:
    print(entity.text, entity.start_char, entity.end_char, entity.label_)

    for umls_ent in entity._.umls_ents:
        # tuple with 2 values
        conceptId, score = umls_ent

        print(f"Name: {entity}")
        print(f"CUI: {conceptId}, Score {score}")
        print(linker.umls.cui_to_entity[umls_ent[0]])
        print()

# show NER
displacy.serve(doc, style="ent", host="127.0.0.1")
# displacy.serve(doc, style="dep", host="127.0.0.1")