<jupyter_start><jupyter_text>![JohnSnowLabs](https://nlp.johnsnowlabs.com/assets/images/logo.png)[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/JohnSnowLabs/nlu/blob/master/examples/colab/visualization/NLU_visualizations_tutorial.ipynb) With NLU and [Spark-NLP-Display](https://github.com/JohnSnowLabs/spark-nlp-display) you can visualize the outputs of various NLP models visualization Available vizualizations- ner- dep- resolution- relation- assertion  Install NLU<jupyter_code>!wget https://setup.johnsnowlabs.com/nlu/colab.sh -O - | bash
import nlu<jupyter_output>--2022-04-15 03:40:22--  https://setup.johnsnowlabs.com/nlu/colab.sh
Resolving setup.johnsnowlabs.com (setup.johnsnowlabs.com)... 192.168.127.12
Connecting to setup.johnsnowlabs.com (setup.johnsnowlabs.com)|192.168.127.12|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://raw.githubusercontent.com/JohnSnowLabs/nlu/master/scripts/colab_setup.sh [following]
--2022-04-15 03:40:23--  https://raw.githubusercontent.com/JohnSnowLabs/nlu/master/scripts/colab_setup.sh
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 172.16.31.10, 192.168.127.12, 172.16.31.10, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|172.16.31.10|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1665 (1.6K) [text/plain]
Saving to: ‘STDOUT’

-                   100%[===================>]   1.63K  --.-KB/s    in 0s      

2022-04-15 03:40:23 (34.4 MB/s) - written to stdout [1665/1665]

Installing  NLU [...]<jupyter_text>Visualize Named Entity Recognizer (NER) resultsApplicable to any of the [100+ NER models! See here for an overview](https://nlp.johnsnowlabs.com/models?task=Named+Entity+Recognition)<jupyter_code>nlu.load('ner').viz("<NAME> from America and <NAME> from Germany don't share many oppinions.")<jupyter_output>onto_recognize_entities_sm download started this may take some time.
Approx size to download 160.1 MB
[OK!]<jupyter_text>Visualize Dependency tree Visualizes the structure of the labeled dependency tree and part of speech tags<jupyter_code>nlu.load('dep.typed').viz("Billy went to the mall")
#Bigger Example
nlu.load('dep.typed').viz("<NAME> from America and <NAME> from Germany don't share many oppinions but they both love <NAME> Labs software")<jupyter_output>dependency_typed_conllu download started this may take some time.
Approximate size to download 2.3 MB
[OK!]
pos_anc download started this may take some time.
Approximate size to download 3.9 MB
[OK!]
dependency_conllu download started this may take some time.
Approximate size to download 16.7 MB
[OK!]<jupyter_text>Licensed visualizationsThe following cells showcase visualizations with licensed models. Make sure to upload your `spark_nlp_for_healthcare.json` or follow [the instrunctions here to get access to licensed features](https://nlu.johnsnowlabs.com/docs/en/examples_hcautomatically-authorize-google-colab-via-json-file) .      If you do not have any credentials yet, you can [grab some here](https://www.johnsnowlabs.com/spark-nlp-try-free/)  Visualize resolution for entitiesApplicable to any of the [100+ Resolver models! See here for an overview](https://nlp.johnsnowlabs.com/models?task=Entity+Resolution)<jupyter_code>nlu.load('med_ner.jsl.wip.clinical resolve_chunk.rxnorm.in').viz("He took 2 pills of Aspirin daily")
# bigger example
data = "This is an 82 - year-old male with a history of prior tobacco use , hypertension , chronic renal insufficiency , COPD , gastritis , and TIA who initially presented to Braintree with a non-ST elevation MI and Guaiac positive stools , transferred to St . Margaret\'s Center for Women & Infants for cardiac catheterization with PTCA to mid LAD lesion complicated by hypotension and bradycardia requiring Atropine , IV fluids and transient dopamine possibly secondary to vagal reaction , subsequently transferred to CCU for close monitoring , hemodynamically stable at the time of admission to the CCU ."
nlu.load('med_ner.jsl.wip.clinical resolve_chunk.rxnorm.in').viz(data)<jupyter_output>ner_wikiner_glove_840B_300 download started this may take some time.
Approximate size to download 14.8 MB
[OK!]<jupyter_text>Visualize resolution sentencesApplicable to any of the [100+ Resolver models! See here for an overview](https://nlp.johnsnowlabs.com/models?task=Entity+Resolution)<jupyter_code>nlu.load('med_ner.jsl.wip.clinical resolve.icd10cm').viz('She was diagnosed with a respiratory congestion')
# bigger example
data = 'The patient is a 5-month-old infant who presented initially on Monday with a cold, cough, and runny nose for 2 days. Mom states she had no fever. Her appetite was good but she was spitting up a lot. She had no difficulty breathing and her cough was described as dry and hacky. At that time, physical exam showed a right TM, which was red. Left TM was okay. She was fairly congested but looked happy and playful. She was started on Amoxil and Aldex and we told to recheck in 2 weeks to recheck her ear. Mom returned to clinic again today because she got much worse overnight. She was having difficulty breathing. She was much more congested and her appetite had decreased significantly today. She also spiked a temperature yesterday of 102.6 and always having trouble sleeping secondary to congestion'
nlu.load('med_ner.jsl.wip.clinical resolve.icd10cm').viz(data)<jupyter_output><empty_output><jupyter_text>Visualize assertion status of entitiesApplicable to any of the [10 + Assertion models! See here for an overview](https://nlp.johnsnowlabs.com/models?task=Assertion+Status)<jupyter_code>nlu.load('med_ner.clinical assert').viz("The MRI scan showed no signs of cancer in the left lung")
#bigger example
data ='This is the case of a very pleasant 46-year-old Caucasian female, seen in clinic on 12/11/07 during which time MRI of the left shoulder showed no evidence of rotator cuff tear. She did have a previous MRI of the cervical spine that did show an osteophyte on the left C6-C7 level. Based on this, negative MRI of the shoulder, the patient was recommended to have anterior cervical discectomy with anterior interbody fusion at C6-C7 level. Operation, expected outcome, risks, and benefits were discussed with her. Risks include, but not exclusive of bleeding and infection, bleeding could be soft tissue bleeding, which may compromise airway and may result in return to the operating room emergently for evacuation of said hematoma. There is also the possibility of bleeding into the epidural space, which can compress the spinal cord and result in weakness and numbness of all four extremities as well as impairment of bowel and bladder function. However, the patient may develop deeper-seated infection, which may require return to the operating room. Should the infection be in the area of the spinal instrumentation, this will cause a dilemma since there might be a need to remove the spinal instrumentation and/or allograft. There is also the possibility of potential injury to the esophageus, the trachea, and the carotid artery. There is also the risks of stroke on the right cerebral circulation should an undiagnosed plaque be propelled from the right carotid. She understood all of these risks and agreed to have the procedure performed.'
nlu.load('med_ner.clinical assert').viz(data)<jupyter_output><empty_output><jupyter_text>Visualize relationships between entitiesApplicable to any of the [20 + Relation Extractor models! See here for an overview](https://nlp.johnsnowlabs.com/models?task=Relation+Extraction)<jupyter_code>nlu.load('med_ner.jsl.wip.clinical relation.temporal_events').viz('He developed cancer after a mercury poisoning in 1999 ') 
# bigger example
data = 'This is the case of a very pleasant 46-year-old Caucasian female, seen in clinic on 12/11/07 during which time MRI of the left shoulder showed no evidence of rotator cuff tear. She did have a previous MRI of the cervical spine that did show an osteophyte on the left C6-C7 level. Based on this, negative MRI of the shoulder, the patient was recommended to have anterior cervical discectomy with anterior interbody fusion at C6-C7 level. Operation, expected outcome, risks, and benefits were discussed with her. Risks include, but not exclusive of bleeding and infection, bleeding could be soft tissue bleeding, which may compromise airway and may result in return to the operating room emergently for evacuation of said hematoma. There is also the possibility of bleeding into the epidural space, which can compress the spinal cord and result in weakness and numbness of all four extremities as well as impairment of bowel and bladder function. However, the patient may develop deeper-seated infection, which may require return to the operating room. Should the infection be in the area of the spinal instrumentation, this will cause a dilemma since there might be a need to remove the spinal instrumentation and/or allograft. There is also the possibility of potential injury to the esophageus, the trachea, and the carotid artery. There is also the risks of stroke on the right cerebral circulation should an undiagnosed plaque be propelled from the right carotid. She understood all of these risks and agreed to have the procedure performed'
pipe = nlu.load('med_ner.jsl.wip.clinical relation.clinical').viz(data)<jupyter_output><empty_output><jupyter_text>Configuring visualizations - `labels_to_viz` : Defines a subset of NER labels to viz i.e. ['PER'] , by default=[] which will display all labels. Applicable only for NER viz- `viz_colors`  : Applicable for [ner, resolution, assert ] key = label, value=hex color, i.e. viz_colors={'TREATMENT':'008080', 'problem':'800080'}<jupyter_code>data = 'Dr. <NAME> suggested that Fritz takes 5mg penicilin for his cough'
# Define custom colors for labels
viz_colors={'STRENGTH':'#800080', 'DRUG_BRANDNAME':'#77b5fe', 'GENDER':'#ebde34'}
nlu.load('med_ner.jsl.wip.clinical').viz(data,viz_colors =viz_colors)
data = 'Dr. <NAME> suggested that Fritz takes 5mg penicilin for his cough'
# Filter wich NER label to viz
labels_to_viz=['SYMPTOM']
nlu.load('med_ner.jsl.wip.clinical').viz(data,viz_colors=viz_colors,labels_to_viz=labels_to_viz)<jupyter_output><empty_output><jupyter_text>Specify visualization type manuallyNLU tries to automatically infer a viz type if none is specified. You can manually specify which component to viz by setting `viz_type=type` for one type out of `ner,dep,resolution,relation`<jupyter_code>data = "<NAME> from America and <NAME> from Germany don't share many oppinions, but they both love John Snow Labs software!"
nlu.load('ner').viz(data,viz_type='ner')<jupyter_output><empty_output><jupyter_text>Viz Dependency<jupyter_code>import nlu
data = "<NAME> from America and <NAME> from Germany don't share many oppinions, but they both love John Snow Labs software!"
viz = nlu.load('dep.typed').viz(data,viz_type='dep')<jupyter_output><empty_output><jupyter_text>Viz Medical NER<jupyter_code>import nlu
data = "<NAME> and <NAME> from Germany don't share many oppinions, but they both fear cancer!"
nlu.load('med_ner.jsl.wip.clinical').viz(data,viz_type='ner')<jupyter_output><empty_output><jupyter_text>Viz Resolution Chunks<jupyter_code>import nlu
nlu_ref = 'med_ner.jsl.wip.clinical en.resolve_chunk.icd10cm.neoplasms'
data = """The patient is a 5-month-old infant who presented initially on Monday with a cold, cough, and runny nose for 2 days. Mom states she had no fever. Her appetite was good but she was spitting up a lot. She had no difficulty breathing and her cough was described as dry and hacky. At that time, physical exam showed a right TM, which was red. Left TM was okay. She was fairly congested but looked happy and playful. She was started on Amoxil and Aldex and we told to recheck in 2 weeks to recheck her ear. Mom returned to clinic again today because she got much worse overnight. She was having difficulty breathing. She was much more congested and her appetite had decreased significantly today. She also spiked a temperature yesterday of 102.6 and always having trouble sleeping secondary to congestion."""
pipe = nlu.load(nlu_ref)
viz = pipe.viz(data,viz_type='resolution')<jupyter_output><empty_output><jupyter_text>Viz Resolution Sentence<jupyter_code>import nlu 
data = ["""He has a starvation ketosis but nothing found for significant for dry oral mucosa"""]
nlu.load('med_ner.jsl.wip.clinical resolve.icd10pcs').viz(data,viz_type='resolution' )<jupyter_output><empty_output><jupyter_text>Viz Assert<jupyter_code>nlu_ref = 'med_ner.jsl.wip.clinical assert'
data = "The patient was tested for cancer, but none was detected, he is free of cancer."
nlu.load(nlu_ref).viz(data,viz_type='assert')<jupyter_output><empty_output><jupyter_text>Viz Relation<jupyter_code>nlu_ref = 'med_ner.jsl.wip.clinical relation.temporal_events'
data = "He was advised chest X-ray or CT scan after checking his SpO2 which was <= 93%"
pipe = nlu.load(nlu_ref).viz(data,viz_type='relation')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup