Spacy:使用实体标签获取单词的位置

Spacy: get position of word with entity tag

根据 spacy 文档,我正在尝试通过遍历句子来获取单词的位置及其实体标签

import spacy
nlp = spacy.load('en')
doc = nlp(u'London is a big city in the United Kingdom.')
for ent in doc.ents:
    print(ent.label_, ent.text)
    # GPE London
    # GPE United Kingdom

我试图用标签 ent.i 和 ent.idx 获取单词的位置,但是这些都不起作用并给出以下错误

AttributeError: 'spacy.tokens.span.Span' object has no attribute 'i'

它似乎是 ent.start

import spacy
nlp = spacy.load('en')
doc = nlp(u'London is a big city in the United Kingdom.')
for ent in doc.ents:
    print(ent.label_, ent.text, ent.start)
    #GPE London 0
    #GPE the United Kingdom 6