在spaCy中,是否有一种方法可以提取从中提取实体的句子?

时间:2019-06-20 22:40:18

标签: python spacy ner

考虑以下句子:

  

“敏捷的棕色狐狸跳过了懒狗”

如果我要提取棕色,则只提取棕色很容易,但是我想要以下输出:

  

“棕色”,“敏捷的棕色狐狸跳过了懒狗”

1 个答案:

答案 0 :(得分:3)

是的,在Spacy中有一种方法可以做到这一点。您必须遍历实体 Span 对象,并从每个 Span 对象中提取句子。这是一个示例:

    doc = nlp("John and Claire live in London. They have a dog. Claire walks her 
    dog everyday.")
    for entity in doc.ents:
       print('Entity extracted : ', entity.text)
       print('Sentence extracted from : ', entity.sent)

这应该为您提供以下输出:

Entity extracted :  John
Sentence extracted from :  John and Claire live in London.

Entity extracted :  Claire
Sentence extracted from :  John and Claire live in London.

Entity extracted :  London
Sentence extracted from :  John and Claire live in London.

Entity extracted :  Claire
Sentence extracted from :  Claire walks her dog everyday.

Entity extracted :  everyday
Sentence extracted from :  Claire walks her dog everyday.