* Fix docstring for EntityRenderer * Add warning in displacy if doc.spans are empty * Implement parse_spans converter One notable change here is that the default spans_key is sc, and it's set by the user through the options. * Implement SpanRenderer Here, I implemented a SpanRenderer that looks similar to the EntityRenderer except for some templates. The spans_key, by default, is set to sc, but can be configured in the options (see parse_spans). The way I rendered these spans is per-token, i.e., I first check if each token (1) belongs to a given span type and (2) a starting token of a given span type. Once I have this information, I render them into the markup. * Fix mypy issues on typing * Add tests for displacy spans support * Update colors from RGB to hex Co-authored-by: Ines Montani <ines@ines.io> * Remove unnecessary CSS properties * Add documentation for website * Remove unnecesasry scripts * Update wording on the documentation Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com> * Put typing dependency on top of file * Put back z-index so that spans overlap properly * Make warning more explicit for spans_key Co-authored-by: Ines Montani <ines@ines.io> Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
16 KiB
title | teaser | new | menu | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Visualizers | Visualize dependencies and entities in your browser or in a notebook | 2 |
|
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. That's why our popular visualizers, displaCy and displaCy ENT are also an official part of the core library. If you're running a Jupyter notebook, displaCy will detect this and return the markup in a format ready to be rendered and exported.
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 pipeline or
modifications you like. If you're using Streamlit, check
out the spacy-streamlit
package that helps you integrate spaCy visualizations into your apps!
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")
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 | Description |
---|---|
compact |
"Compact mode" with square arrows that takes up less space. Defaults to False . |
color |
Text color (HEX, RGB or color names). Defaults to "#000000" . |
bg |
Background color (HEX, RGB or color names). Defaults to "#ffffff" . |
font |
Font name or font family for all text. Defaults to "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)
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 Doc
s or Span
s. 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'