- spaCy - Discussion
- spaCy - Useful Resources
- spaCy - Quick Guide
- Updating Neural Network Model
- Training Neural Network Model
- spaCy - Container Lexeme Class
- spaCy - Span Class Properties
- spaCy - Container Span Class
- spaCy - Token Properties
- spaCy - Container Token Class
- Doc Class ContextManager and Property
- spaCy - Containers
- spaCy - Compatibility Functions
- spaCy - Utility Functions
- spaCy - Visualization Function
- spaCy - Top-level Functions
- spaCy - Command Line Helpers
- spaCy - Architecture
- spaCy - Models and Languages
- spaCy - Getting Started
- spaCy - Introduction
- spaCy - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
spaCy - Span Class Properties
In this chapter, let us learn the Span properties in spaCy.
Properties
Following are the properties with regards to Span Class in spaCy.
Sr.No. | Span Properties & Description |
---|---|
1 | Span.ents Used for the named entities in the span. |
2 | Span.as_doc Used to create a new Doc object corresponding to the Span. It will have a copy of data too. |
3 | Span.root To provide the token with the shortest path to the root of the sentence. |
4 | Span.lefts Used for the tokens that are to the left of the span whose heads are within the span. |
5 | Span.rights Used for the tokens that are to the right of the span whose heads are within the span. |
6 | Span.n_rights Used for the tokens that are to the right of the span whose heads are within the span. |
7 | Span.n_lefts Used for the tokens that are to the left of the span whose heads are within the span. |
8 | Span.subtree To yield the tokens that are within the span and the tokens which descend from them. |
9 | Span.vector Represents a real-valued meaning. |
10 | Span.vector_norm Represents the L2 norm of the document’s vector representation. |
Span.ents
This Span property is used for the named entities in the span. If the entity recogniser has been appped, this property will return a tuple of named entity span objects.
Example 1
An example of Span.ents property is as follows −
import spacy nlp_model = spacy.load("en_core_web_sm") doc = nlp_model("This is Tutorialspoint.com.") span = doc[0:5] ents = pst(span.ents) ents[0].label
Output
You will receive the following output −
383
Example 2
An another example of Span.ents property is as follows −
ents[0].label_
Output
You will receive the following output −
‘ORG’
Example 3
Given below is another example of Span.ents property −
ents[0].text
Output
You will receive the following output −
Tutorialspoint.com
Span.as_doc
As the name suggests, this Span property will create a new Doc object corresponding to the Span. It will have a copy of data too.
Example
An example of Span.as_doc property is given below −
import spacy nlp_model = spacy.load("en_core_web_sm") doc = nlp_model("I pke India.") span = doc[2:4] doc2 = span.as_doc() doc2.text
Output
You will receive the following output −
India
Span.root
This Span property will provide the token with the shortest path to the root of the sentence. It will take the first token, if there are multiple tokens which are equally high in the tree.
Example 1
An example of Span.root property is as follows −
import spacy nlp_model = spacy.load("en_core_web_sm") doc = nlp_model("I pke New York in Autumn.") i, pke, new, york, in_, autumn, dot = range(len(doc)) doc[new].head.text
Output
You will receive the following output −
York
Example 2
An another example of Span.root property is as follows −
doc[york].head.text
Output
You will receive the following output −
pke
Example 3
Given below is an example of Span.root property −
new_york = doc[new:york+1] new_york.root.text
Output
You will receive the following output −
York
Span.lefts
This Span property is used for the tokens that are to the left of the span, whose heads are within the span.
Example
An example of Span.lefts property is mentioned below −
import spacy nlp_model = spacy.load("en_core_web_sm") doc = nlp_model("This is Tutorialspoint.com.") lefts = [t.text for t in doc[1:4].lefts] lefts
Output
You will receive the following output −
[ This ]
Span.rights
This Span property is used for the tokens that are to the right of the span whose heads are within the span.
Example
An example of Span.rights property is given below −
import spacy nlp_model = spacy.load("en_core_web_sm") doc = nlp_model("This is Tutorialspoint.com.") rights = [t.text for t in doc[1:2].rights] rights
Output
You will receive the following output −
[ Tutorialspoint.com , . ]
Span.n_rights
This Span property is used for the tokens that are to the right of the span whose heads are within the span.
Example
An example of Span.n_rights property is as follows −
import spacy nlp_model = spacy.load("en_core_web_sm") doc = nlp_model("This is Tutorialspoint.com.") doc[1:2].n_rights
Output
You will receive the following output −
2
Span.n_lefts
This Span property is used for the tokens that are to the left of the span whose heads are within the span.
Example
An example of Span.n_lefts property is as follows −
import spacy nlp_model = spacy.load("en_core_web_sm") doc = nlp_model("This is Tutorialspoint.com.") doc[1:2].n_lefts
Output
You will receive the following output −
1
Span.subtree
This Span property yields the tokens that are within the span and the tokens which descend from them.
Example
An example of Span.subtree property is as follows −
import spacy nlp_model = spacy.load("en_core_web_sm") doc = nlp_model("This is Tutorialspoint.com.") subtree = [t.text for t in doc[:1].subtree] subtree
Output
You will receive the following output −
[ This ]
Span.vector
This Span property represents a real-valued meaning. The defaults value is an average of the token vectors.
Example 1
An example of Span.vector property is as follows −
import spacy nlp_model = spacy.load("en_core_web_sm") doc = nlp_model("The website is Tutorialspoint.com.") doc[1:].vector.dtype
Output
You will receive the following output −
dtype( float32 )
Example 2
An another example of Span.vector property is as follows −
Output
You will receive the following output −
(96,)
Span.vector_norm
This doc property represents the L2 norm of the document’s vector representation.
Example
An example of Span.vector_norm property is as follows −
import spacy nlp_model = spacy.load("en_core_web_sm") doc = nlp_model("The website is Tutorialspoint.com.") doc[1:].vector_norm doc[2:].vector_norm doc[1:].vector_norm != doc[2:].vector_norm
Output
You will receive the following output −
TrueAdvertisements