Spacy:获取带有实体标签的单词的位置

jac*_*est 4 spacy

我正在尝试通过迭代句子来获取单词的位置及其实体标签,按照 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
Run Code Online (Sandbox Code Playgroud)

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

AttributeError: 'spacy.tokens.span.Span' object has no attribute 'i'
Run Code Online (Sandbox Code Playgroud)

jac*_*est 5

看起来是 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
Run Code Online (Sandbox Code Playgroud)