Get position of word in sentence with spacy

荒凉一梦 提交于 2020-07-18 08:55:11

问题


I'm aware of the basic spacy workflow for getting various attributes from a document, however I can't find a built in function to return the position (start/end) of a word which is part of a sentence.

Would anyone know if this is possible with Spacy?


回答1:


These are available as attributes of the tokens in the sentences. Doc says:

idx int The character offset of the token within the parent document.

i int The index of the token within the parent document.

>>> import spacy
>>> nlp = spacy.load('en')
>>> parsed_sentence = nlp(u'This is my sentence')
>>> [(token.text,token.i) for token in parsed_sentence]
[(u'This', 0), (u'is', 1), (u'my', 2), (u'sentence', 3)]
>>> [(token.text,token.idx) for token in parsed_sentence]
[(u'This', 0), (u'is', 5), (u'my', 8), (u'sentence', 11)]


来源:https://stackoverflow.com/questions/46049612/get-position-of-word-in-sentence-with-spacy

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