[go: up one dir, main page]

Skip to content

A Generalized Suffix Tree for any Python iterable using Ukkonen's algorithm, with Lowest Common Ancestor retrieval.

License

Notifications You must be signed in to change notification settings

williamgilpin/suffix-tree

 
 

Repository files navigation

A Generalized Suffix Tree

py39 py310 py311 py312 pypy39 coverage

A Generalized Suffix Tree for any Python sequence, with Lowest Common Ancestor retrieval.

pip install suffix-tree
>>> from suffix_tree import Tree

>>> tree = Tree({"A": "xabxac"})
>>> tree.find("abx")
True
>>> tree.find("abc")
False

This suffix tree:

  • works with any Python sequence, not just strings, if the items are hashable,
  • is a generalized suffix tree for sets of sequences,
  • is implemented in pure Python,
  • builds the tree in time proportional to the length of the input,
  • does constant-time Lowest Common Ancestor retrieval.

Being implemented in Python this tree is not very fast nor memory efficient. The building of the tree takes time proportional to the length of the string of symbols. The query time is proportional to the length of the query string.

To get the best performance turn the python optimizer on: python -O.

Documentation: https://cceh.github.io/suffix-tree/

PyPi: https://pypi.org/project/suffix-tree/

Usage examples:

>>> from suffix_tree import Tree
>>> tree = Tree()
>>> tree.add(1, "xabxac")
>>> tree.add(2, "awyawxawxz")
>>> tree.find("abx")
True
>>> tree.find("awx")
True
>>> tree.find("abc")
False
>>> tree = Tree({"A": "xabxac", "B": "awyawxawxz"})
>>> tree.find_id("A", "abx")
True
>>> tree.find_id("B", "abx")
False
>>> tree.find_id("B", "awx")
True
>>> tree = Tree(
...     {
...         "A": "sandollar",
...         "B": "sandlot",
...         "C": "handler",
...         "D": "grand",
...         "E": "pantry",
...     }
... )
>>> for k, length, path in tree.common_substrings():
...     print(k, length, path)
...
2 4 s a n d
3 3 a n d
4 3 a n d
5 2 a n
>>> tree = Tree({"A": "xabxac", "B": "awyawxawxz"})
>>> for C, path in sorted(tree.maximal_repeats()):
...     print(C, path)
...
1 a w
1 a w x
2 a
2 x
2 x a

About

A Generalized Suffix Tree for any Python iterable using Ukkonen's algorithm, with Lowest Common Ancestor retrieval.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 98.3%
  • Makefile 1.7%