Search code examples
pythonspacynamed-entity-recognition

Spacy NER entities postition


How can I get the entity position found by NER within spacy?

From the following example:

doc = nlp('Rami Eid is studying at Stony Brook University in New York')
print(list([(ent for ent in doc.ents])

it results:

['Rami Eid','Stony Brook University','New York']

but I need the position of each entity within the sentence, so that I can know which tokens belong to the respective entity.

If I need to search from these results, I may have some cases where single word entities match multiple words of other entities.


Solution

  • An entity is an object of the spacy.Span class, meaning it inherits methods such as start, end etc.

    >>> doc = nlp('Rami Eid is studying at Stony Brook University in New York')
    >>> [(e.start, e.end) for e in doc.ents]
    [(0, 2), (5, 8), (9, 11)]