Visualizing Parts of Speech

spaCy offers an outstanding visualizer called displaCy:


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

# Import the displaCy library
from spacy import displacy

In [2]:
# Create a simple Doc object
doc = nlp(u"The quick brown fox jumped over the lazy dog's back.")

In [3]:
# Render the dependency parse immediately inside Jupyter:
displacy.render(doc, style='dep', jupyter=True, options={'distance': 110})


The DET quick ADJ brown ADJ fox NOUN jumped VERB over ADP the DET lazy ADJ dog NOUN 's PART back. NOUN det amod amod nsubj prep det amod poss case pobj

The dependency parse shows the coarse POS tag for each token, as well as the dependency tag if given:


In [13]:
for token in doc:
    print(f'{token.text:{10}} {token.pos_:{7}} {token.dep_:{7}} {spacy.explain(token.dep_)}')


The        DET     det     determiner
quick      ADJ     amod    adjectival modifier
brown      ADJ     amod    adjectival modifier
fox        NOUN    nsubj   nominal subject
jumped     VERB    ROOT    None
over       ADP     prep    prepositional modifier
the        DET     det     determiner
lazy       ADJ     amod    adjectival modifier
dog        NOUN    poss    possession modifier
's         PART    case    None
back       NOUN    pobj    object of preposition
.          PUNCT   punct   punctuation

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 [15]:
displacy.serve(doc, style='dep', options={'distance': 110})


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

127.0.0.1 - - [12/Oct/2018 16:54:07] "GET / HTTP/1.1" 200 8304
127.0.0.1 - - [12/Oct/2018 16:54:07] "GET /favicon.ico HTTP/1.1" 200 8304
    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`

**NOTE**: We'll use this method moving forward because, at this time, several of the customizations we want to show don't work well in Jupyter.


Handling Large Text

displacy.serve() accepts a single Doc or list of Doc objects. Since large texts are difficult to view in one line, you may want to pass a list of spans instead. Each span will appear on its own line:


In [16]:
doc2 = nlp(u"This is a sentence. This is another, possibly longer sentence.")

# Create spans from Doc.sents:
spans = list(doc2.sents)

displacy.serve(spans, style='dep', options={'distance': 110})


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

127.0.0.1 - - [12/Oct/2018 16:57:03] "GET / HTTP/1.1" 200 7328
127.0.0.1 - - [12/Oct/2018 16:57:03] "GET /favicon.ico HTTP/1.1" 200 7328
    Shutting down server on port 5000.

Click this link to view the dependency: http://127.0.0.1:5000
Interrupt the kernel to return to jupyter.


Customizing the Appearance

Besides setting the distance between tokens, you can pass other arguments to the options parameter:

NAMETYPEDESCRIPTIONDEFAULT
`compact`bool"Compact mode" with square arrows that takes up less space.`False`
`color`unicodeText color (HEX, RGB or color names).`#000000`
`bg`unicodeBackground color (HEX, RGB or color names).`#ffffff`
`font`unicodeFont name or font family for all text.`Arial`

For a full list of options visit https://spacy.io/api/top-level#displacy_options


In [17]:
options = {'distance': 110, 'compact': 'True', 'color': 'yellow', 'bg': '#09a3d5', 'font': 'Times'}

displacy.serve(doc, style='dep', options=options)


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

127.0.0.1 - - [12/Oct/2018 17:02:17] "GET / HTTP/1.1" 200 8533
127.0.0.1 - - [12/Oct/2018 17:02:17] "GET /favicon.ico HTTP/1.1" 200 8533
    Shutting down server on port 5000.

Click this link to view the dependency: http://127.0.0.1:5000
Interrupt the kernel to return to jupyter.


Great! Now you should be familiar with visualizing spaCy's dependency parse. For more info on displaCy visit https://spacy.io/usage/visualizers
In the next section we'll look at Named Entity Recognition, followed by displaCy's NER visualizer.

Next Up: Named Entity Recognition