Visualizing Named Entity Recognition (NER)

Khulood Nasher
4 min readDec 12, 2020

--

In the previous blog, I introduced the named Entity Recognition(NER), please visit NER. Today, I will explain how to visualize NER with spaCY. To visualize the name entity, we will import ‘displacy’ from spaCy. The ‘displacy’ class is a mix of the word “display” and “spaCy”. Please click on my Github to get the python code.

Now, I will create my text as nlp object to get it ready for processing by spacy library.

To visualize the entities, we will use the ‘render’ method where we will define the style as ‘ent’ for entity and ‘jupyter’= True

‘displacy’ class is able to define ‘The Food and Drug Administration’ and tags it as an ‘ORG’ and highlighters it with light blue for the ‘ORG’ tag and highlighters other named entities as we can see above.

Displaying Sentences Line by Line:

Let’s say I have a text of multiple sentences and I want to view those sentences line by line such as the following text:

To do a sentence segmentation and display the above text line by line, we will simply iterate over each sentence of my text through the method ‘.sents’ and we will display the entities as we did before through the class ‘displacy.render’ on the tokens of each sentence and specify the style as ‘ent’ as follows:

We can see that the above text has four sentences and they were displayed line by line. The first sentence starts with ‘ORG’ entity which is ‘The Food and Drug Administration’. The second sentence starts with ‘A landmark’ and it is not a named entity but it is viewed in a new line. The third sentence is a Date entity and it is started in a new line ‘Today’, and the last sentence is a person tag and it is viewed in a new line.

Customizing NER Visualization

We can filter our NER visualization and specify it with entities that we are only interested in through a dictionary where its values are the entity tags. So if I want to visualize the entities of organizations and countries, my value list will include[‘ORG’,’GPE’] while the key of my options is ‘ents’ as follows:

As we can see above, the entities of organizations with blue highlight and countries with orange highlight were the only entities visualized. But what about customizing the colors and effects of my highlight, can we do that? The answer is yes we can do it.

Customizing colors and Effects

There is a default color for each entity tag, however I can customize colors and effects of my tags through a dictionary where the key is the entity tag and the value is the selected color with its effects as follows:

As we can see above, I only customize the ORG entity with the colors and effects, and I use the default color for the Geographical tag. I chose to start with the red color and end with the blue color for each ORG entity. I selected the shade of 90 degrees so both corners of the highlight were colored.

There are many effects we can still add here to visualize our NER.

--

--

Khulood Nasher

Data Scientist,Health Physicist, NLP Researcher, & Arabic Linguist. https://www.linkedin.com/in/khuloodnasher https:/khuloodnasher1.wixsite.com/resume