nlp-uoregon/trankit

How to get children from a particular token in trankit

HitheshSankararaman opened this issue · 1 comments

In spacy you have a feature called token.children. Do we have something like that in trankit ?
Eg:-
Screenshot from 2022-12-13 17-47-30
In dependency parsing, we can see that the verb "shift" points to "cars", "liability" and "manufacturers" .
In spacy if I give shift.children , I will get cars , liability , manufacturers.

Do we have something similar in trankit ?

Hi @HitheshSankararaman,
Thanks for your question.
In Trankit, we can get the children for a token by comparing the parent ID with the "head" information from each token in a sentence. Below is a sample snippet:

from trankit import Pipeline

p = Pipeline('english')

# perform dependency parsing
tokens = p.posdep('Autonomous cars shift insurance liability toward manufactures', is_sent=True)['tokens']

# set the parent token
parent_token = tokens[2] # i.e., "shift"

# find children by comparing the "head" information
child_tokens = [t for t in tokens if t['head'] == parent_token['id']] # i.e., 'cars', 'liability', 'manufactures'

We will consider adding this function in future releases.