2017-09-24 84 views
0

我試圖得到一個字的位置和它的實體標籤通過遍歷一個句子,按照該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 

我試着獲得與標籤ent.i字的位置,但是ent.idx這些工作既不併給出以下錯誤

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

回答

0

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