spaCy - Token Properties



In this chapter, we will learn about the properties with regards to the Token class in spaCy.

Properties

The token properties are listed below along with their respective descriptions.

Sr.No. Token Property & Description
1

Token.ancestors

Used for the rightmost token of this token’s syntactic descendants.

2

Token.conjuncts

Used to return a tuple of coordinated tokens.

3

Token.children

Used to return a sequence of the token’s immediate syntactic children.

4

Token.lefts

Used for the leftward immediate children of the word.

5

Token.rights

Used for the rightward immediate children of the word.

6

Token.n_rights

Used for the number of rightward immediate children of the word.

7

Token.n_lefts

Used for the number of leftward immediate children of the word.

8

Token.subtree

This yields a sequence that contains the token and all the token’s syntactic descendants.

9

Token.vector

This represents a real-valued meaning.

10

Token.vector_norm

This represents the L2 norm of the token’s vector representation.

Token.ancestors

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']

Token.conjuncts

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']

Token.children

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', '.']

Token.lefts

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']

Token.rights

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', '.']

Token.n_rights

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

Token.n_lefts

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

Token.subtree

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', '.']

Token.vector

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

Token.vector_norm

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
Advertisements