Search code examples
pythonspacynamed-entity-recognition

In spaCy is there a way to extract the sentence the entity has been extracted from?


considering the following sentence:

"the quick brown fox jumps over the lazy dog"

and if I want to extract brown, extracting only brown is fairly easy to do however I want the following output:

"brown" , "the quick brown fox jumps over the lazy dog"


Solution

  • Yes, there is a way to do this in Spacy. You have to iterate over the entity Span objects and extract the sentence from each Span object. Here is an example:

        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)
    
    

    This should give you the following output :

    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.