Extract a path of dependency relations from the ROOT to a token? SPACY. The code I have it extract the whole path
import spacy
sentence = "I saw the man with a telescop"
nlp = spacy.load('en')
doc = nlp(sentence)
for sent in doc.sents:
for token in sent:
print("{}\t{}\t{}\t{}".format(token.i, token.text, token.head, token.dep_))
The dependency tree is basically a graph, so if you want to find the (shortest) path to ROOT, you need to use some graph-based libraries like
networkx
. Let's say you want to extract a path from a token "telescop" to the root. Then you could try to do something like this:Result: