首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在spacy文档中查找span的起始和结束索引

如何在spacy文档中查找span的起始和结束索引
EN

Stack Overflow用户
提问于 2022-02-10 17:18:47
回答 1查看 358关注 0票数 0

我正在用spacy做一些NLP工作。我有一个句子和一个名词块,如何使用spacy找到名词块的起始词索引和结束词索引?例如,如果我有“我住在纽约市”,而名词块是"New York",那么我想要3作为输出

EN

回答 1

Stack Overflow用户

发布于 2022-02-10 19:22:00

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71069504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档