Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

from spacy.language import Language

@Language.component("CustomB")
def set_custom_boundaries(doc):
    for token in doc[:-1]:
        if token.text == ';':
            doc[token.i+1].is_sent_start = True
    return doc
nlp.add_pipe("CustomB",before="parser")

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

doc[token.i+1]

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading