spaCy - Token Properties

In this chapter, we will learn about the properties with regards to the Token class in spaCy.
The token properties are listed below along with their respective descriptions.
Token.ancestors
Used for the rightmost token of this token’s syntactic descendants.
Token.conjuncts
Used to return a tuple of coordinated tokens.
Token.children
Used to return a sequence of the token’s immediate syntactic children.
Token.lefts
Used for the leftward immediate children of the word.
Token.rights
Used for the rightward immediate children of the word.
Token.n_rights
Used for the number of rightward immediate children of the word.
Token.n_lefts
Used for the number of leftward immediate children of the word.
Token.subtree
This yields a sequence that contains the token and all the token’s syntactic descendants.
Token.vector
This represents a real-valued meaning.
Token.vector_norm
This represents the L2 norm of the token’s vector representation.
This token property is used for the rightmost token of this token’s syntactic descendants.
Example
An example of Token.ancestors property is given below −
import spacy
nlp_model = spacy.load("en_core_web_sm")
from spacy.tokens import Token
doc = nlp_model("Give it back! He pleaded.")

it_ancestors = doc[1].ancestors
[t.text for t in it_ancestors]
Output
['Give']

This token property is used to return a tuple of co-ordinated tokens. Here, the token itself would not be included.
Example
An example of Token.conjuncts property is as follows −
import spacy
nlp_model = spacy.load("en_core_web_sm")
from spacy.tokens import Token
doc = nlp_model("I like cars and bikes")
cars_conjuncts = doc[2].conjuncts
[t.text for t in cars_conjuncts]
Output
The output is mentioned below −
['bikes']

This token property is used to return a sequence of the token’s immediate syntactic children.
Example
An example of Token.children property is as follows −
import spacy
nlp_model = spacy.load("en_core_web_sm")
from spacy.tokens import Token
doc = nlp_model("This is Tutorialspoint.com.")
give_child = doc[1].children
[t.text for t in give_child]
Output
['This', 'Tutorialspoint.com', '.']

This token property is used for the leftward immediate children of the word. It would be in the syntactic dependency parse.
Example
An example of Token.lefts property is as follows −
import spacy
nlp_model = spacy.load("en_core_web_sm")
from spacy.tokens import Token
doc = nlp_model("This is Tutorialspoint.com.")
left_child = [t.text for t in doc[1].lefts]
left_child
Output
You will get the following output −
['This']

This token property is used for the rightward immediate children of the word. It would be in the syntactic dependency parse.
Example
An example of Token.rights property is given below −
import spacy
nlp_model = spacy.load("en_core_web_sm")
from spacy.tokens import Token
doc = nlp_model("This is Tutorialspoint.com.")
right_child = [t.text for t in doc[1].rights]
right_child
Output
['Tutorialspoint.com', '.']

This token property is used for the number of rightward immediate children of the word. It would be in the syntactic dependency parse.
Example
An example of Token.n_rights property is given below −
import spacy
nlp_model = spacy.load("en_core_web_sm")
from spacy.tokens import Token
doc = nlp_model("This is Tutorialspoint.com.")
doc[1].n_rights
Output
2

This token property is used for the number of leftward immediate children of the word. It would be in the syntactic dependency parse.
Example
An example of Token.n_lefts property is as follows −
import spacy
nlp_model = spacy.load("en_core_web_sm")
from spacy.tokens import Token
doc = nlp_model("This is Tutorialspoint.com.")
doc[1].n_lefts
Output
The output is stated below −
1

This token property yields a sequence that contains the token and all the token’s syntactic descendants.
Example
An example of Token.subtree property is as follows −
import spacy
nlp_model = spacy.load("en_core_web_sm")
from spacy.tokens import Token
doc = nlp_model("This is Tutorialspoint.com.")
subtree_doc = doc[1].subtree
[t.text for t in subtree_doc]
Output
['This', 'is', 'Tutorialspoint.com', '.']

This token property represents a real-valued meaning. It will return a one-dimensional array representing the token’s semantics.
Example 1
An example of Token.vector property is as follows −
import spacy
nlp_model = spacy.load("en_core_web_sm")
doc = nlp_model("The website is Tutorialspoint.com.")
doc.vector.dtype
Output
The output is stated below −
dtype('float32')

Example 2
An another example of Token.vector property is given below −
doc.vector.shape
Output
The output is stated below −
(96,)

This token property represents the L2 norm of the token’s vector representation.
Example
An example of Token.vector_norm property is given below −
import spacy
nlp_model = spacy.load("en_core_web_sm")
doc1 = nlp_model("The website is Tutorialspoint.com.")
doc2 = nlp_model("It is having best technical tutorials.")
doc1[2].vector_norm !=doc2[2].vector_norm
Output
True

 Print
 Add Notes
 Bookmark this page