nlp - 如何在spaCy中获取句子中 token 的索引?

标签 nlp spacy dependency-parsing

有没有一种优雅的方法来获取句子中单词/标记的索引?
我知道 token 的属性 https://spacy.io/api/token#attributesi属性返回整个父文档中的索引。但是父文档可以包含多个句子。
例子:

"This is an example. This is another example."


我需要的是两个 "This"作为索引返回 0 , 两者 "is"作为索引返回 1等等...

最佳答案

一个空间 Doc对象还允许您遍历 doc.sents , 分别是 Span objects的个别句子。要在父文档中获取跨度的开始和结束索引,您可以查看 startend属性。因此,如果您遍历句子并从 token.i 中减去句子开始索引,您将在句子中获得 token 的相对索引:

for sent in doc.sents:
    for token in sent:
        print(token.text, token.i - sent.start)

默认的句子切分使用依赖解析,通常更准确。但是,您也可以插入基于规则或完全自定义的解决方案(see here 了解详细信息)。

关于nlp - 如何在spaCy中获取句子中 token 的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50742516/

相关文章:

multithreading - 多线程运行Stanford CoreNLP服务器

amazon-web-services - en-core-web-sm 模块错误 - 无服务器部署 AWS Lambda

nlp - 使用 Spacy en_core_web_lg 模型的 POS 标记不一致

python - 在Python中,无论时态、形式如何检测句子中的关键词?

python - 使用依赖规则匹配的aspect-opinion提取中的命名实体识别

python - 使用 Python3.6 安装 NLTK 时出错

java - 使用 OpenNLP 进行句子检测

python - NLP项目数据库

python - 将依存解析的输出转换为树