spaCy - Doc._ _getitem_ _ Method



This method of Doc class is used to get a token object at a particular position say n. Here, n is an integer. It also supports the negative indexing and follows the usual Python semantics.

For example, doc[-2] is doc[len(doc) - 2].

Argument

The table below explains its argument −

NAME TYPE DESCRIPTION
N integer It represents the index of the token.

We can also get a span object which starts at a position say start and ends at a position say end. Both these positions are token index.

Example 1

An example of Doc._ _getitem_ _ method is given below −

import spacy
doc = nlp_model("This is Tutorialspoint.com")
doc[0].text

Output

When you run the code, you will see the following output −

'This'

Example 2

Refer the example of Doc._ _getitem_ _ method given below −

doc[-1].text

Output

When you run the code, you will see the following output −

'Tutorialspoint.com'

Example 3

Here is an another example of Doc._ _getitem_ _ method −

span = doc[1:3]
span.text

Output

When you run the code, you will see the following output −

'is Tutorialspoint.com'
spacy_containers.htm
Advertisements