What does `i` in `token.i+1` mean when using a token returned by spacy's Language?

from spacy.language import Language

def set_custom_boundaries(doc):
    for token in doc[:-1]:
        if token.text == ';':
            doc[token.i+1].is_sent_start = True
    return doc

All I need to know is what does i+1 do in this code:


knowing that i is not defined in the function, neither as an index nor as a simple variable.

>Solution :

i is not a seperate variable. It is an attribute of token. And notice that it is not i+1 but instead it is token.i + 1. i is from the token object. First python gets the value of i from token then it increases it by one.

Consider the example below:

class X:
    i = 10

token = X()

print(token.i+1) # it is in fact (token.i) + 1 so result is: 11

If you have any question please ask.

Leave a ReplyCancel reply