Spacy: get position of word with entity tag

冷暖自知 提交于 2019-12-23 21:14:38

问题


I'm trying to get the position of a word and it's entity tag by iterating over a sentence, as per the 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

I've tried to get the position of the word with the tag ent.i and ent.idx however neither of these work and give the following error

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

回答1:


It would appear to be 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


来源:https://stackoverflow.com/questions/46390331/spacy-get-position-of-word-with-entity-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!