Najlaa Bouali
3 min readMay 11, 2020

A short article of how to use displaCy to visualize ner tags given by another package than spaCy (Flair for example)

In this post, I would like to share with you how you can use displaCy visualizer from spaCy to highlight named entities and their labels in a text using another library than spaCy.

Let’s take the following sentence “Barack Obama is the former president of the United States of America” to highlight the named entities detected with spaCy.

The execution of the code above will display the usual representation of ner tags from spaCy :

Let’s suppose you want to have an identical visualization using the ner module from Flair, You just need to know that the render method from displaCy can consider a dictionary set manually.
This dictionary should at least have those two keys: “text”<Your initial text> and “ents” <the entities detected by your model, which is a list of dicts>. Below, a valid example of this dictionary:

doc={‘ents’: [{‘end’: “THE END POSITION OF THE NAMED ENTITY”,
‘start’: “THE START POSITION OF THE NAMED ENTITY “,
‘label’: “THE LABEL OF THE NAMED ENTITY”}],
text’: “THE TEXT TO ANALYZE”}

We could have obtained the same previous visualization using a predefined dictionary and setting “manual=True”:

Now, let’s get the named entities using the ner tagger from Flair:

After running the code above, go ahead and print “dict_flair” to get this:

{‘entities’: [{‘confidence’: 0.9084628820419312, ‘end_pos’: 12, ‘start_pos’: 0, ‘text’: ‘Barack Obama’, ‘type’: ‘PER’}, {‘confidence’: 0.8057131916284561, ‘end_pos’: 68, ‘start_pos’: 44, ‘text’: ‘United States of America’, ‘type’: ‘LOC’}], ‘labels’: [], ‘text’: ‘Barack Obama is the former president of the United States of America’}

Now, you need to transform this dictionary in order to match the one expected from displaCy:

Our dictionary is ready, we can pass it to displaCy and obtain the visualization below:

That’s it :) !

Note that if you want to render this visualization in your notebook, you need to set “jupyter=True”.

References :