spaCy/website/docs/usage/visualizers.md
harmbuisman c066fb8a4e
#10672: fixes displacy output for manual unsorted entities (#10673)
* #10672: fixes displacy output for manual unsorted entities

* #10672: removed unused import

* fix prettier formatting

Co-authored-by: Harm Buisman <h.buisman@iknl.nl>
Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
2022-04-27 09:51:58 +02:00

16 KiB
Raw Blame History

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

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")

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 Description
compact "Compact mode" with square arrows that takes up less space. Defaults to False. bool
color Text color (HEX, RGB or color names). Defaults to "#000000". str
bg Background color (HEX, RGB or color names). Defaults to "#ffffff". str
font Font name or font family for all text. Defaults to "Arial". str

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'