Halvani / Constituent-Treelib

A lightweight Python library for constructing, processing, and visualizing constituent trees.
https://pypi.org/project/constituent-treelib
MIT License
61 stars 10 forks source link

How to get the deapth of the ConstituentTree ? #4

Closed suyog-pipliwaal closed 1 year ago

suyog-pipliwaal commented 1 year ago

I am a student studying NLP at the university. I am studying the different ways to parse and build a tree for a language like English. I am just wondering if is there any way to determine the depth of a tree for a given sentence in English.

Halvani commented 1 year ago

There you go...

from constituent_treelib import ConstituentTree, BracketedTree, Language, Structure

language = Language.English

# Define which specific SpaCy model should be used (default is Medium)
spacy_model_size = ConstituentTree.SpacyModelSize.Medium

# Create the pipeline (note, the required models will be downloaded and installed automatically)
nlp = ConstituentTree.create_pipeline(language, spacy_model_size)

sentence = "Tokyo Disneyland opened on April 15, 1983 and is mostly based on its sister castle parks in Anaheim, California, and Bay Lake, Florida."

tree = ConstituentTree(sentence, nlp)

Resulting tree...

To determine the height of the tree, we simply access the underlying nltk tree and the corresponding height function:

tree.nltk_tree.height()

>>> 12

That's it.