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

时间:2017-09-24 13:04:44

标签: spacy

根据spacy docs,我试图通过迭代一个句子来获取单词及其实体标记的位置

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'

1 个答案:

答案 0 :(得分:2)

它似乎是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