Nlp 如何使用spacy获得依赖树的高度?

Nlp 如何使用spacy获得依赖树的高度?,nlp,spacy,Nlp,Spacy,我有一句话“今天我上学很早” 我想使用spacy和迭代来获得依赖树的最大高度(深度) nlp = spacy.load("en_core_web_sm") doc = nlp("today i go to school early") height = 0 for token in doc: root = [token for token in doc if token.head == token][0] 我被困在这里,无法继续前进。如果上面的代码

我有一句话“今天我上学很早” 我想使用spacy和迭代来获得依赖树的最大高度(深度)

nlp = spacy.load("en_core_web_sm")
doc = nlp("today i go to school early")
height = 0
for token in doc:
   root = [token for token in doc if token.head == token][0]

我被困在这里,无法继续前进。如果上面的代码有任何错误,您能帮我纠正一下吗?

这里是一个基于

这张照片是:

{'jumps': 0, 'fox': 1, 'The': 2, 'quick': 2, 'brown': 2, 'over': 1, 'dog': 2, 'the': 3, 'lazy': 3, '.': 1}
3

编辑:

如果你只想要最大深度,其他什么都不要,这就行了

def walk_tree(node, depth):
    if node.n_lefts + node.n_rights > 0:
        return max(walk_tree(child, depth + 1) for child in node.children)
    else:
        return depth


print([walk_tree(sent.root, 0) for sent in doc.sents])

谢谢,有没有返回深度的最大值?我尝试将深度{}放入函数中,但效果不好
def walk_tree(node, depth):
    if node.n_lefts + node.n_rights > 0:
        return max(walk_tree(child, depth + 1) for child in node.children)
    else:
        return depth


print([walk_tree(sent.root, 0) for sent in doc.sents])