ZSS is a Python library that implements the Zhang-Shasha algorithm for computing the tree edit distance between two ordered labeled trees. It is currently at version 1.2.0 and, while not frequently updated, provides a stable and specialized tool for comparing tree structures. The library can be extended with custom node formats and distance metrics, and optionally leverages `editdist` and `numpy` for enhanced functionality and performance.
pip install zssVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to define two simple trees using the built-in `Node` class and compute their edit distance using `simple_distance()`. The `addkid` method is used to construct the tree structure.
Use `zss.distance(A, B, insert_cost=my_insert_cost, remove_cost=my_remove_cost, update_cost=my_update_cost)`.
Implement functions `get_children(node)`, `get_label(node)`, and `label_dist(label_a, label_b)` and pass them as arguments: `simple_distance(A, B, get_children=my_get_children, get_label=my_get_label, label_dist=my_label_dist)`.
For optimal performance and accurate string-based label comparisons, ensure these packages are installed: `pip install zss editdist numpy`.
Ensure the library is installed: `pip install zss`. If using a virtual environment, activate it before running your script.
Define functions to extract children and labels from your custom nodes, and a function to compare their labels. Then pass these to the distance calculation: `simple_distance(A, B, get_children=my_get_children_func, get_label=my_get_label_func, label_dist=my_label_distance_func)`.
When building trees with `zss.Node`, use the `addkid()` method: `Node('parent').addkid(Node('child'))`.