A lightweight Python library for constructing, processing, and visualizing constituent trees.
Project description
Constituent Treelib (CTL)
A lightweight Python library for constructing, processing, and visualizing constituent trees.
Description
CTL allows you to easily construct a constituent tree representation of sentences, visualize them and export them into various file formats. Moreover, you can conveniently extract phrases according to their phrasal categories, which can then be used as features for a variety of NLP tasks.
CTL is built on top of benepar (Berkeley Neural Parser) as well as the two well-known NLP frameworks spaCy and NLTK. Here, spaCy is used for tokenization and sentence segmentation, while benepar performs the actual parsing of the sentences. NLTK, on the other hand, provides the fundamental data structure for storing and processing the parsed sentences.
To gain a clearer picture of what a constituent tree looks like, let's consider the following example. Given the sentence S = "Stanley Getz was an American jazz saxophonist." CTL first parses S into a so-called bracketed tree string representation (shown below in a Penn tree-bank style):
(S
(NP (NNP Stanley) (NNP Getz))
(VP
(VBD was)
(NP (DT an) (JJ American) (NN jazz) (NN saxophonist)))
(. .))
This string represents the actual constituent tree, which can then be visualized and exported to a desired format, here for instance as a PNG file:
This representation[^1] shows three aspects of the structure of S:
- Linear order of the words and their part-of-speech:
NNP = Stanley
,NNP = Getz
,VBD = was
, ... - Groupings of the words and their part-of-speech into phrases:
NP = Stanley Getz
,VP = was an American jazz saxophonist
andNP = an American jazz saxophonist
- Hierarchical structure of the phrases:
S
,NP
,VP
andNP
Applications
Constituent trees offer a wide range of applications, such as:
- Analysis and comparison of sentence structures between different languages for (computational) linguists
- Extracting phrasal features for certain NLP tasks (e.g., Machine Translation, Information Extraction, Paraphrasing, Stylometry, Deception Detection or Natural Language Watermarking)
- Using the resulting representations as an input to train GNNs for specific tasks (e.g., Chemical–Drug Relation Extraction or Semantic Role Labeling)
Features
- Easy construction of constituent trees from raw (or already processed) sentences
- Multilingual (currently CTL supports eight languages)
- Convenient export of tree visualizations to various file formats
- Extraction of phrases according to given phrasal categories
- Automatic setup of the necessary NLP pipeline, which downloads and installs the models automatically on demand
- Extensively documented source code
- No API dependency (CTL can be used completely offline)
Installation
The easiest way to install CTL is to use pip, where you can choose between (1) the PyPI[^2] repository and (2) this repository.
1 pip install constituent-treelib
2 pip install git+https://github.com/Halvani/constituent_treelib.git
The latter command will pull and install the latest commit from this repository as well as the required Python dependencies. Besides these, CTL also relies on the following two open-source tools to export the constructed constituent tree into various file formats:
1 To export the constituent tree into a PDF, the command line tool wkhtmltopdf is required. Once downloaded and installed, the path to the wkhtmltopdf binary must be passed to the export function.
2 To export the constituent tree into the file formats JPG, PNG, GIF, BMP, EPS, PSD, TIFF and YAML, the software suite ImageMagick is required.
Quickstart
Below you can find several examples of the core functionality of CTL. More examples can be found in the jupyter notebook demo.
Creating an NLP pipeline
To instantiate a ConstituentTree
object, CTL requires a spaCy-based NLP pipeline that incorporates a benepar component. Although you can set up this pipeline yourself, it is recommended (and more convenient) to let CTL do it for you automatically via the create_pipeline()
method. Given the desired language, this method creates the NLP pipeline and also downloads[^3] the corresponding spaCy and benepar models, if requested. The following code shows an example of this:
from constituent_treelib import ConstituentTree, BracketedTree
language = ConstituentTree.Language.English
spacy_model_size = ConstituentTree.SpacyModelSize.Medium
nlp = ConstituentTree.create_pipeline(language, spacy_model_size, download_models = True)
>>> ✔ Download and installation successful
>>> You can now load the package via spacy.load('en_core_web_md')
>>> [nltk_data] Downloading package benepar_en3 to
>>> [nltk_data] [..] \nltk_data...
>>> [nltk_data] Unzipping models\benepar_en3.zip.
Define a sentence
Next, we instantiate a ConstituentTree
object and pass it the created NLP pipeline along with a sentence to parse, e.g. the memorable quote "You must construct additional pylons!"^4. Rather than a raw sentence, ConstituentTree
also accepts an already parsed sentence wrapped as a BracketedTree object, or alternatively in the form of an NLTK tree. The following example illustrates all three options:
from nltk import Tree
# Raw sentence
sentence = 'You must construct additional pylons!'
# Parsed sentence wrapped as a BracketedTree object
bracketed_tree_string = '(S (NP (PRP You)) (VP (MD must) (VP (VB construct) (NP (JJ additional) (NNS pylons)))) (. !))'
sentence = BracketedTree(bracketed_tree_string)
# Parsed sentence in the form of an NLTK tree
sentence = Tree('S', [Tree('NP', [Tree('PRP', ['You'])]), Tree('VP', [Tree('MD', ['must']), Tree('VP', [Tree('VB', ['construct']), Tree('NP', [Tree('JJ', ['additional']), Tree('NNS', ['pylons'])])])]), Tree('.', ['!'])])
tree = ConstituentTree(sentence, nlp)
Extract phrases
Once we have created tree
, we can now extract phrases according to given phrasal categories e.g., verb phrases:
phrases = tree.extract_all_phrases()
print(phrases['VP'])
>>> ['must construct additional pylons', 'construct additional pylons']
As can be seen here, the second verb phrase is contained in the former. To avoid this, we can instruct the method to disregard nested phrases:
phrases = tree.extract_all_phrases(avoid_nested_phrases=True)
print(phrases['VP'])
>>> ['must construct additional pylons']
Export the tree
CTL offers you the possibility to export the constructed constituent tree into various file formats, which are listed below. Most of these formats result in a visualization of the tree, while the remaining file formats are used for data exchange.
Extension | Description | Output |
---|---|---|
Portable Document Format | Vector graphic | |
SVG | Scalable Vector Graphics | Vector graphic |
EPS | Encapsulated PostScript | Vector graphic |
JPG | Joint Photographic Experts Group | Raster image |
PNG | Portable Network Graphics | Raster image |
GIF | Graphics Interchange Format | Raster image |
BMP | Bitmap | Raster image |
PSD | Photoshop Document | Raster image |
TIFF | Tagged Image File Format | Raster image |
JSON | JavaScript Object Notation | Data exchange format |
YAML | Yet Another Markup Language | Data exchange format |
TXT | Plain-Text | Pretty-print text visualization |
TEX | LaTeX-Document | LaTeX-typesetting |
The following example shows an export the tree into a PDF file:
tree.export_tree(destination_filepath='my_tree.pdf', verbose=True)
>>> PDF - file successfully saved to: my_tree.pdf
Note, in case of any raster/vector image format, the resulting visualization will be cropped with respect to unnecessary margins. This is particularly useful if the visualizations are to be used in papers. Another way to save space is to shrink the tree by removing internal postag nodes, which can be accomplished as follows:
tree_compact = ConstituentTree(sentence, nlp, remove_postag_nodes=True)
As a result, the tree can be reduced from 5 levels to 4:
Available models and languages
CTL currently supports eight languages: English, German, French, Polish, Hungarian, Swedish, Chinese and Korean. The performance of the respective models can be looked up in the benepar repository.
License
The code and the jupyter notebook demo of CTL are released under the MIT License. See LICENSE for further details.
Citation
If you find this repository helpful, feel free to cite it in your paper or project:
@misc{HalvaniConstituentTreelib:2023,
title={{A Lightweight Python Library for Constructing, Processing, and Visualizing Constituent Trees}},
author={Oren Halvani},
year={2023},
publisher = {GitHub},
howpublished = {\url{https://github.com/Halvani/constituent_treelib}}
}
Please also give credit to the authors of benepar and cite their work.
[^1]: Note, if you are not familiar with the bracket labels of constituent trees, have a look at the following Gist or alternatively this website.
[^2]: It's recommended to install CTL from PyPI (Python Package Index). However, if you want to benefit from the latest update of CTL, you should use this repository instead, since I will only update PyPi at irregular intervals.
[^3]: After the models have been downloaded, they are cached so that there are no redundant downloads when the method is called again. However, loading and initializing the spaCy and benepar models can take a while, so it makes sense to invoke the create_pipeline()
method only once if you want to process multiple sentences.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file constituent_treelib-0.0.2.tar.gz
.
File metadata
- Download URL: constituent_treelib-0.0.2.tar.gz
- Upload date:
- Size: 15.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecfb94f86c3cf03e68d19b305cdf96cc8619605de8d1b4335c1fea81de60ff23 |
|
MD5 | 2eb028d109e389c32179840498680ab0 |
|
BLAKE2b-256 | c3bcb66964dd71d5051df405c5897f75bda515d19ab7e0be2603cc9dd190fedf |
File details
Details for the file constituent_treelib-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: constituent_treelib-0.0.2-py3-none-any.whl
- Upload date:
- Size: 17.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24bf588c348a2c5af93edae177b2639e67dfe259d8b295dca99098ff43dfb061 |
|
MD5 | 79544fe9d43841d73e1d834e083e2370 |
|
BLAKE2b-256 | 1a1dbe2f40d9019c38dcfc62b5825ab1c4311300222d75d767d6871ee3de468b |