DanielTheRocketMan
DanielTheRocketMan

Reputation: 3249

Is there a simple way to get the position of a token in sequence with spacy?

I want to the start index and the end index of every token in a sequence. Is there a simple way to do that with spacy?

For instance:

text='Brown is a nice guy'
spacy_doc=nlp(text)

for sent in spacy_doc.sents:
    for token in sent:
        print(token.text, token.i)

    
Brown 0
is 1
a 2
nice 3
guy 4

This is not what I need. I need

Brown 0,4
is 6,7
a 9,9
nice 11,14
guy 16,18

Upvotes: 2

Views: 1846

Answers (1)

Raqib
Raqib

Reputation: 1442

import spacy

text = 'Brown is a nice guy'
nlp = spacy.load("en_core_web_sm")
doc = nlp(text)

for token in doc:
  print(token.text, token.idx, token.idx + len(token.text) - 1)

Output

Brown 0 4
is 6 7
a 9 9
nice 11 14
guy 16 18

Upvotes: 6

Related Questions