我試圖從文本中提取物體及其關系。我正在嘗試通過物體提取決議依賴關系樹以執行該操作。遞回函式邏輯一定有問題,阻止我決議該資訊,但我沒有看到它是什么。我想使用依賴樹 物體來形成(人物,動作,位置)提取。
期望輸出:人物:Lou Pinella,動作:退出,位置:體育場
代碼示例:
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})
列印陳述句 - 您可以看到它正在命中位置邏輯并列印它,但遞回函式中的回傳仍然是 None 。
Lou Pinella PERSON
Loc found: Stadium
Person: Lou Pinella
Head: exited
Person: Lou Pinella, action:exited, Loc:None
Stadium LOC
已編輯:將 return get_child_ent(child) 添加到 else 中。
uj5u.com熱心網友回復:
您的遞回呼叫沒有回傳值。你需要這個:
else:
return get_children_ent(child)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/392909.html
