user14416085
user14416085

Reputation: 61

how to get height of dependency tree with spacy?

enter image description here

I have sentence 'today i go to school early' I want to use spacy and iteration to get the max height (depth) of the dependency tree.

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]

I am stuck here and can not navigate further. Could you please help and correct my above code if anything wrong?

Upvotes: 1

Views: 1581

Answers (1)

mbatchkarov
mbatchkarov

Reputation: 16049

Here is a recursive implementation based on this question

import spacy

en_nlp = spacy.load('en_core_web_sm')
doc = en_nlp("The quick brown fox jumps over the lazy dog.")
depths = {}

def walk_tree(node, depth):
    depths[node.orth_] = depth
    if node.n_lefts + node.n_rights > 0:
        return [walk_tree(child, depth + 1) for child in node.children]


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

This prints:

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

EDIT:

If you just want the max depth and nothing else, this will do

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])

Upvotes: 2

Related Questions