我正在用spacy做一些NLP工作。我有一个句子和一个名词块,如何使用spacy找到名词块的起始词索引和结束词索引?例如,如果我有“我住在纽约市”,而名词块是"New York",那么我想要3作为输出
发布于 2022-02-10 19:22:00
import spacy
nlp = spacy.load("en_core_web_sm")
text="I live in New York City"
doc = nlp(text)
#To find the Noun chunks
Noun_chunks= [chunk.text for chunk in doc.noun_chunks]
#for loop to find the index of noun chunks
for noun in Noun_chunks:
starting_index = text.index(noun)
ending_index = text.index(noun) + len(noun) - 1
print(noun,starting_index,ending_index)
#output
#There are 2 noun_chunks: I and New York City
#I 0 0 (starting at 0 and ending at 0)
#New York City 10 22 (starting at 10 and ending at 22)https://stackoverflow.com/questions/71069504
复制相似问题