nlp-uoregon / trankit

Trankit is a Light-Weight Transformer-based Python Toolkit for Multilingual Natural Language Processing
Apache License 2.0
724 stars 99 forks source link

How to get children from a particular token in trankit #63

Closed HitheshSankararaman closed 10 months ago

HitheshSankararaman commented 1 year ago

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 ?

minhhdvn commented 10 months ago

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.