依赖关系分析树上的最低共同祖先

时间:2018-09-20 12:13:52

标签: python dictionary tree stanford-nlp lowest-common-ancestor

我看到了类似的帖子:How to make a tree from the output of a dependency parser?,那里的人们帮助我获得了从根到叶的道路。我想找到两个单词的最低共同祖先,因为我想找到两个节点之间的路径。

到目前为止,我有这个:

def lowestcommonancestor(categories,p,q):
    for category in categories:
        if category['Name']==p or category['Name']==q or category['Name']==None:
            return category
        if 'children' in category:
            node1=lowestcommonancestor(category['children'],p,q)
            if node1 is not None: #to see if the other word is present as a child
                if 'children' in node1:
                    node2=lowestcommonancestor(node1['children'],p,q)
                    if node2 is not None:
                        return node1
                else:# if not then we need to return the root
                    return category
    return None

类别:

[{'Name': 'arrested', 'Relationship': 'ROOT', 'children': [{'Name': 'week', 'Relationship': 'nmod:tmod', 'children': [{'Name': 'U', 'Relationship': 'compound'}, {'Name': 'smoke', 'Relationship': 'compound'}]}, {'Name': 'you', 'Relationship': 'nsubjpass'}, {'Name': 'could', 'Relationship': 'aux'}, {'Name': 'be', 'Relationship': 'auxpass'}, {'Name': 'fuck', 'Relationship': 'dobj', 'children': [{'Name': 'U', 'Relationship': 'compound'}, {'Name': 'dumb', 'Relationship': 'amod'}]}]}]

当我致电lowestcommonancestor(categories,'fuck','U')时,我得到week,它是U的父母,但不是fuck的父母。因此,我应该将arrested作为节点。

我无法完成功能,我想我没有返回正确的节点。请帮助。

0 个答案:

没有答案