spaCy/website/docs/usage/visualizers.md
2020-05-24 17:23:00 +02:00

15 KiB
Raw Blame History

title teaser new menu
Visualizers Visualize dependencies and entities in your browser or in a notebook 2
Dependencies
dep
Entities
ent
Jupyter Notebooks
jupyter
Rendering HTML
html

As of v2.0, our popular visualizers, displaCy and displaCy ENT are finally an official part of the library. Visualizing a dependency parse or named entities in a text is not only a fun NLP demo it can also be incredibly helpful in speeding up development and debugging your code and training process. If you're running a Jupyter notebook, displaCy will detect this and return the markup in a format ready to be rendered and exported.

What about the old visualizers?

Our JavaScript-based visualizers displacy.js and displacy-ent.js will still be available on GitHub. If you're looking to implement web-based visualizations, we generally recommend using those instead of spaCy's built-in displacy module. It'll allow your application to perform all rendering on the client and only rely on the server for the text processing. The generated markup is also more compatible with modern web standards.

The quickest way to visualize Doc is to use displacy.serve. This will spin up a simple web server and let you view the result straight from your browser. displaCy can either take a single Doc or a list of Doc objects as its first argument. This lets you construct them however you like using any model or modifications you like.

Visualizing the dependency parse

The dependency visualizer, dep, shows part-of-speech tags and syntactic dependencies.

### Dependency example
import spacy
from spacy import displacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("This is a sentence.")
displacy.serve(doc, style="dep")

displaCy visualizer

The argument options lets you specify a dictionary of settings to customize the layout, for example:

There's currently a known issue with the compact mode for sentences with short arrows and long dependency labels, that causes labels longer than the arrow to wrap. So if you come across this problem, especially when using custom labels, you'll have to increase the distance setting in the options to allow longer arcs.

Argument Type Description Default
compact bool "Compact mode" with square arrows that takes up less space. False
color str Text color (HEX, RGB or color names). "#000000"
bg str Background color (HEX, RGB or color names). "#ffffff"
font str Font name or font family for all text. "Arial"

For a list of all available options, see the displacy API documentation.

Options example

options = {"compact": True, "bg": "#09a3d5",
           "color": "white", "font": "Source Sans Pro"}
displacy.serve(doc, style="dep", options=options)

displaCy visualizer (compact mode)

Visualizing long texts

Long texts can become difficult to read when displayed in one row, so it's often better to visualize them sentence-by-sentence instead. As of v2.0.12, displacy supports rendering both Doc and Span objects, as well as lists of Docs or Spans. Instead of passing the full Doc to displacy.serve, you can also pass in a list doc.sents. This will create one visualization for each sentence.

import spacy
from spacy import displacy

nlp = spacy.load("en_core_web_sm")
text = """In ancient Rome, some neighbors live in three adjacent houses. In the center is the house of Senex, who lives there with wife Domina, son Hero, and several slaves, including head slave Hysterium and the musical's main character Pseudolus. A slave belonging to Hero, Pseudolus wishes to buy, win, or steal his freedom. One of the neighboring houses is owned by Marcus Lycus, who is a buyer and seller of beautiful women; the other belongs to the ancient Erronius, who is abroad searching for his long-lost children (stolen in infancy by pirates). One day, Senex and Domina go on a trip and leave Pseudolus in charge of Hero. Hero confides in Pseudolus that he is in love with the lovely Philia, one of the courtesans in the House of Lycus (albeit still a virgin)."""
doc = nlp(text)
sentence_spans = list(doc.sents)
displacy.serve(sentence_spans, style="dep")

Visualizing the entity recognizer

The entity visualizer, ent, highlights named entities and their labels in a text.

### Named Entity example
import spacy
from spacy import displacy

text = "When Sebastian Thrun started working on self-driving cars at Google in 2007, few people outside of the company took him seriously."

nlp = spacy.load("en_core_web_sm")
doc = nlp(text)
displacy.serve(doc, style="ent")

import DisplacyEntHtml from 'images/displacy-ent2.html'