简体   繁体   中英

spacy extract entity relationships parse dep tree

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:
                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

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

            else:
                return get_children_ent(child)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM