Visualizing Named Entities

Besides viewing Part of Speech dependencies with style='dep', displaCy offers a style='ent' visualizer:


In [1]:
# Perform standard imports
import spacy
nlp = spacy.load('en_core_web_sm')

# Import the displaCy library
from spacy import displacy

In [2]:
doc = nlp(u'Over the last quarter Apple sold nearly 20 thousand iPods for a profit of $6 million. '
         u'By contrast, Sony sold only 7 thousand Walkman music players.')

displacy.render(doc, style='ent', jupyter=True)


Over the last quarter DATE Apple ORG sold nearly 20 thousand CARDINAL iPods PRODUCT for a profit of $6 million MONEY . By contrast, Sony ORG sold only 7 thousand CARDINAL Walkman PRODUCT music players.

Viewing Sentences Line by Line

Unlike the displaCy dependency parse, the NER viewer has to take in a Doc object with an ents attribute. For this reason, we can't just pass a list of spans to .render(), we have to create a new Doc from each span.text:


In [3]:
for sent in doc.sents:
    displacy.render(nlp(sent.text), style='ent', jupyter=True)


Over the last quarter DATE Apple ORG sold nearly 20 thousand CARDINAL iPods PRODUCT for a profit of $6 million MONEY .
By contrast, Sony ORG sold only 7 thousand CARDINAL Walkman PRODUCT music players.
**NOTE**: If a span does not contain any entities, displaCy will issue a harmless warning:

In [4]:
doc2 = nlp(u'Over the last quarter Apple sold nearly 20 thousand iPods for a profit of $6 million. '
         u'By contrast, my kids sold a lot of lemonade.')

In [5]:
for sent in doc2.sents:
    displacy.render(nlp(sent.text), style='ent', jupyter=True)


Over the last quarter DATE Apple ORG sold nearly 20 thousand CARDINAL iPods PRODUCT for a profit of $6 million MONEY .
C:\Anaconda3\lib\runpy.py:193: UserWarning: [W006] No entities to visualize found in Doc object. If this is surprising to you, make sure the Doc was processed using a model that supports named entity recognition, and check the `doc.ents` property manually if necessary.
  "__main__", mod_spec)
By contrast, my kids sold a lot of lemonade.
**WORKAROUND:** We can avert this with an additional bit of code:

In [6]:
for sent in doc2.sents:
    docx = nlp(sent.text)
    if docx.ents:
        displacy.render(docx, style='ent', jupyter=True)
    else:
        print(docx.text)


Over the last quarter DATE Apple ORG sold nearly 20 thousand CARDINAL iPods PRODUCT for a profit of $6 million MONEY .
By contrast, my kids sold a lot of lemonade.

Viewing Specific Entities

You can pass a list of entity types to restrict the visualization:


In [7]:
options = {'ents': ['ORG', 'PRODUCT']}

displacy.render(doc, style='ent', jupyter=True, options=options)


Over the last quarter Apple ORG sold nearly 20 thousand iPods PRODUCT for a profit of $6 million. By contrast, Sony ORG sold only 7 thousand Walkman PRODUCT music players.

Customizing Colors and Effects

You can also pass background color and gradient options:


In [8]:
colors = {'ORG': 'linear-gradient(90deg, #aa9cfc, #fc9ce7)', 'PRODUCT': 'radial-gradient(yellow, green)'}

options = {'ents': ['ORG', 'PRODUCT'], 'colors':colors}

displacy.render(doc, style='ent', jupyter=True, options=options)


Over the last quarter Apple ORG sold nearly 20 thousand iPods PRODUCT for a profit of $6 million. By contrast, Sony ORG sold only 7 thousand Walkman PRODUCT music players.

For more on applying CSS background colors and gradients, visit https://www.w3schools.com/css/css3_gradients.asp


Creating Visualizations Outside of Jupyter

If you're using another Python IDE or writing a script, you can choose to have spaCy serve up HTML separately.

Instead of displacy.render(), use displacy.serve():


In [9]:
displacy.serve(doc, style='ent', options=options)


    Serving on port 5000...
    Using the 'ent' visualizer

127.0.0.1 - - [04/Dec/2018 13:21:26] "GET / HTTP/1.1" 200 2210
127.0.0.1 - - [04/Dec/2018 13:21:26] "GET /favicon.ico HTTP/1.1" 200 2210
    Shutting down server on port 5000.

**After running the cell above, click the link below to view the dependency parse**:

http://127.0.0.1:5000

**To shut down the server and return to jupyter**, interrupt the kernel either through the **Kernel** menu above, by hitting the black square on the toolbar, or by typing the keyboard shortcut `Esc`, `I`, `I`

For more on Visualizing the entity recognizer visit https://spacy.io/usage/visualizers#ent

Next Up: Sentence Segmentation