Palash Jhamb
Palash Jhamb

Reputation: 625

SpaCy : How to get position of entity in the entire document?

I know that that SpaCy provides start and end of each entity in a sentence. I want the start of the entity in the whole document (not just the sentence).

Upvotes: 1

Views: 1322

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627536

You may get the entity start position in the whole document using ent.start_char:

for ent in doc.ents:
    print(ent.text, ent.start_char)

A quick test:

import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp(u"The live in New York City. They went to Manhattan in the morning.")
for ent in doc.ents:
    print(ent.text, ent.start_char)

Output:

New York City 12
Manhattan 40

Upvotes: 2

Related Questions