- spaCy Tutorial
- spaCy - Home
- spaCy - Introduction
- spaCy - Getting Started
- spaCy - Models and Languages
- spaCy - Architecture
- spaCy - Command Line Helpers
- spaCy - Top-level Functions
- spaCy - Visualization Function
- spaCy - Utility Functions
- spaCy - Compatibility Functions
- spaCy - Containers
- Doc Class ContextManager and Property
- spaCy - Container Token Class
- spaCy - Token Properties
- spaCy - Container Span Class
- spaCy - Span Class Properties
- spaCy - Container Lexeme Class
- Training Neural Network Model
- Updating Neural Network Model
- spaCy Useful Resources
- spaCy - Quick Guide
- spaCy - Useful Resources
- spaCy - Discussion
spaCy - Visualization Function
Visualizer functions are mainly used to visualize the dependencies and also the named entities in browser or in a notebook. As of spacy version 2.0, there are two popular visualizers namely displaCy and displaCyENT.
They both are the part of spacy’s built-in visualization suite. By using this visualization suite namely displaCy, we can visualize a dependency parser or named entity in a text.
displaCy()
Here, we will learn about the displayCy dependency visualizer and displayCy entity visualizer.
Visualizing the dependency parse
The displaCy dependency visualizer (dep) will show the POS(Part-of-Speech) tags and syntactic dependencies.
Example
An example for the use of displaCy() dependency visualizer for visualizing the dependency parse is given below −
import spacy from spacy import displacy nlp = spacy.load("en_core_web_sm") doc = nlp("This is Tutorialspoint.com.") displacy.serve(doc, style="dep")
Output
This gives the following output −
We can also specify a dictionary of settings to customize the layout. It will be under argument option (we will discuss in detail later).
The example with options is given below −
import spacy from spacy import displacy nlp = spacy.load("en_core_web_sm") doc = nlp("This is Tutorialspoint.com.") options = {"compact": True, "bg": "#09a3d5", "color": "red", "font": "Source Sans Pro"} displacy.serve(doc, style="dep", options=options)
Output
Given below is the output −
Visualizing named entities
The displaCy entity visualizer (ent) will highlight named entities and their labels in a text.
Example
An example for the use of displaCy entity visualizer for named entities is given below −
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. But Google is starting from behind. The company made a late push into hardware, and Apple's Siri has clear leads in consumer adoption." nlp = spacy.load("en_core_web_sm") doc = nlp(text) displacy.serve(doc, style="ent")
Output
The output is stated below −
We can also specify a dictionary of settings to customize the layout. It will be under argument option (we will discuss in detail later).
The example with options is given below −
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. But Google is starting from behind. The company made a late push into hardware, and Apple's Siri has clear leads in consumer adoption." nlp = spacy.load("en_core_web_sm") doc = nlp(text) colors = {"ORG": "linear-gradient(90deg, #aa9cfc, #fc9ce7)"} options = {"ents": ["ORG"], "colors": colors} displacy.serve(doc, style="ent", options=options)
Output
The output is mentioned below −
displaCy() methods
As of version 2.0, displaCy () function has two methods namely serve and render. Let’s discuss about them in detail. A table is given below of the methods along with their respective descriptions.
Sr.No. | Method & Description |
---|---|
1 | displayCy.serve It will serve the dependency parse tree. |
2 | displayCy.render It will render the dependency parse tree. |
displaCy.serve
It is the method that will serve a dependency parse tree/ named entity visualization to see in a web browser. It will run a simple web browser.
Arguments
The table below explains its arguments −
NAME | TYPE | DESCRIPTION | DEFAULT |
---|---|---|---|
Docs | list, doc, Span | It represents the document to visualize. | |
Style | Unicode | We have two visualization style namely ‘dep’, or ‘ent’. | The default value is ‘dep’. |
Page | bool | It will render the markup as full HTML page. | The default value is true. |
minify | bool | This argument will minify the HTML markup. | The default value is false. |
options | dict | It represents the visualizers-specific options. For example, colors. | {} |
manual | bool | This argument will not parse Doc and instead, expect a dict or list of dicts. | The default value is false. |
Port | int | It is the port number to serve visualization. | 5000 |
Host | unicode | It is the Host number to serve visualization. | '0.0.0.0' |
Example
An example for displayCy.serve method is given below −
import spacy from spacy import displacy nlp = spacy.load("en_core_web_sm") doc1 = nlp("This is Tutorialspoint.com") displacy.serve(doc1, style="dep")
Output
This gives the following output −
displaCy.render
This displaCy method will render a dependency parse tree or named entity visualization.
Arguments
The table below explains its arguments −
NAME | TYPE | DESCRIPTION | DEFAULT |
---|---|---|---|
Docs | list, doc, Span | It represents the document to visualize. | |
Style | Unicode | We have two visualization style namely ‘dep’, or ‘ent’. | The default value is ‘dep’. |
Page | Bool | It will render the markup as full HTML page. | The default value is false. |
minify | Bool | This argument will minify the HTML markup. | The default value is false. |
options | Dict | It represents the visualizers-specific options. For example, colors. | {} |
manual | Bool | This argument will not parse Doc and instead, expect a dict or list of dicts. | The default value is false. |
jupyter | Bool | To return markup ready to be rendered in a notebook, this argument will explicitly enable or disable the Jupyter mode. If we will not provide this argument, it will automatically detect. | None |
Example
An example for the displaCy.render method is stated below −
import spacy from spacy import displacy nlp = spacy.load("en_core_web_sm") doc = nlp("This is Tutorialspoint.") html = displacy.render(doc, style="dep")
Output
Visualizer options
The option argument of dispaCy () function lets us specify additional settings for each visualizer, dependency as well as named entity visualizer.
Dependency Visualizer options
The table below explains the Dependency Visualizer options −
NAME | TYPE | DESCRIPTION | DEFAULT |
---|---|---|---|
fine_grained | bool | Put the value of this argument True, if you want to use fine-grained part-of-speech tags (Token.tag_), instead of coarse-grained tags (Token.pos_). | The default value is False. |
add_lemma | bool | Introduced in version 2.2.4, this argument prints the lemma’s in a separate row below the token texts. | The default value is False. |
collapse_punct | bool | It attaches punctuation to the tokens. | The default value is True. |
collapse_phrases | bool | This argument merges the noun phrases into one token. | The default value is False. |
compact | bool | If you will take this argument as true, you will get the “Compact mode” with square arrows that takes up less space. | The default value is False. |
color | unicode | As name implies, this argument is for the text color (HEX, RGB or color names). | '#000000' |
bg | unicode | As name implies, this argument is for the Background color (HEX, RGB or color names). | '#ffffff' |
font | unicode | It is for the font name. | Default value is 'Arial'. |
offset_x | int | This argument is used for spacing on left side of the SVG in px. | The default value of this argument is 50. |
arrow_stroke | int | This argument is used for adjusting the width of arrow path in px. | The default value of this argument is 2. |
arrow_width | int | This argument is used for adjusting the width of arrow head in px. | The default value of this argument is 10 / 8 (compact). |
arrow_spacing | int | This argument is used for adjusting the spacing between arrows in px to avoid overlaps. | The default value of this argument is 20 / 12 (compact). |
word_spacing | int | This argument is used for adjusting the vertical spacing between words and arcs in px. | The default value of this argument is 45. |
distance | int | This argument is used for adjusting the distance between words in px. | The default value of this argument is 175 / 150 (compact). |
Named Entity Visualizer options
The table below explains the Named Entity Visualizer options −
NAME | TYPE | DESCRIPTION | DEFAULT |
---|---|---|---|
ents | list | It represents the entity types to highlight. Put None for all types. | The default value is None. |
colors | Dict | As name implies, it is use for color overrides. The entity types in uppercase must mapped to color name. | {} |