Home > other >  spacy extract entity relationships parse dep tree
spacy extract entity relationships parse dep tree

Time:12-25

I am trying to extract entities and their relationships from the text. I am attempting to parse the dependency tree with entity extraction to perform that action. Something must be wrong with the recursive function logic that is preventing me from being able to parse that information, but I am not seeing what it is. I wanted to use the dependency tree entities to form a (person,action,location) extraction.

Desired output: Person: Lou Pinella, action:exited, Loc:Stadium

Code Example:

import spacy
from spacy import displacy

nlp = spacy.load('en_core_web_lg')
doc = nlp("Lou Pinella exited from the far left side of the Stadium.")

def get_children_ent(head):
    if head.children:
        for child in head.children:
            if child.ent_type_ == "LOC":
                print(f'Loc found: {child}') # it is hitting this branch
                return child
            else:
                return get_children_ent(child)
    else:
        return "No Children"

for ent in doc.ents:
    print(ent.text, ent.label_)
    if ent.label_ == "PERSON":
        person = ent.text
        head = ent.root.head
        loc = get_children_ent(head)
        print(f'Person: {person}')
        print(f'Head: {head}')
        print(f'Person: {person}, action:{head}, Loc:{loc}')
    
   

displacy.render(doc, options={"fine_grained": True})

Print Statements - You can see it is hitting the location logic and printing that, but the return is still None in the recursive function.

Lou Pinella PERSON
Loc found: Stadium
Person: Lou Pinella
Head: exited
Person: Lou Pinella, action:exited, Loc:None
Stadium LOC

EDITED: added return get_child_ent(child) to the else.

CodePudding user response:

Your recursive call isn't returning a value. You need this:

            else:
                return get_children_ent(child)
  • Related