pydivsufsort prebuilds libdivsufsort as a shared library and
includes it in a Python package with bindings. Wheels are built for Linux, macOS and Windows (32 and 64 bits) using cibuildwheel and GitHub Actions. Basically, you should be able to install it with pip install pydivsufsort on any system and it should work out of the box. If it doesn't, please create an issue.
Features:
bindings to divsufsort that return numpy arrays
handle string, bytes and almost any integer data type (e.g. int64) and not only char
algorithms work even for non char inputs
additional string algorithms coded in Cython
Installation
On Linux, macOS and Windows:
python -m pip install pydivsufsort
We provide precompiled wheels for common systems using cibuildwheel, and a source distribution for Unix systems. Manual compilation on Windows might require some tweaking, please create an issue.
Features
All methods support string, bytes and numpy array inputs, including datatypes greater than uint8_t (e.g. uint64_t). Below are the signatures of all methods exposed by pydivsufsort. To import a method, just do from pydivsufsort import method_name. All methods are documented in the docstrings. You can display them with help(method_name).
To run many queries on the same string, use the WonderString class: it computes the suffix array and the LCP array only once.
Methods exposed from libdivsufsort
divsufsort(string): suffix array
bw_transform(string, sa=None): Burrows-Wheeler transform (the output keeps the dtype of the input)
sa_search(string, suffix_array, pattern): search for a pattern in a suffix array
Additional string algorithms
kasai(string, suffix_array=None): LCP array computation (lazily computes the suffix array if not provided)
lcp_segtree(string, suffix_array=None, lcp=None): build a segment tree for LCP queries (lazily computes the suffix array and LCP array if not provided)
lcp_query(segtree, queries): query a segment tree for LCP queries. Queries are pairs of indices.
kmp_censor_stream(censor, string): Censor a stream (like a generator of string) using the KMP algorithm
Example usage
from pydivsufsort import divsufsort, kasai
string_inp = "banana$"
string_suffix_array = divsufsort(string_inp)
string_lcp_array = kasai(string_inp, string_suffix_array)
print(string_suffix_array, string_lcp_array)
# [6 5 3 1 0 4 2] [0 1 3 0 0 2 0]
# You can also convert the string input to integers first
import numpy as np
int_inp = np.unique(np.array(list(string_inp)), return_inverse=True)[1]
int_suffix_array = divsufsort(int_inp)
int_lcp_array = kasai(int_inp, int_suffix_array)
print(int_suffix_array, int_lcp_array)
# [6 5 3 1 0 4 2] [0 1 3 0 0 2 0]
WonderString
WonderString(string) computes the suffix array and the LCP array the first time a method needs them, and keeps them for the next calls. To import it, do from pydivsufsort import WonderString.
string: the input, as a numpy array
suffix_array: suffix array
lcp_array: LCP array
search(pattern, return_positions=False): number of occurrences of the pattern, and index of the first one in the suffix array (None if there is no occurrence). With return_positions=True, returns the positions of the occurrences in the string, in suffix array order.
lcp(i, j): length of the longest common prefix of the suffixes that start at i and j. lcp(queries) takes a list of pairs and returns an array.
most_frequent_substrings(length, limit=0, minimum_count=1): positions in the string and counts of the most frequent substrings. See the docstring of most_frequent_substrings for details.
libdivsufsort is compiled in both 32 and 64 bits, as the 32 bits version is faster. pydivsufsort automatically chooses to use the 32 bits version when possible (aka when the input size is less than 2**31-1).
For best performance, use contiguous arrays. If you have a sliced array, pydivsufsort converts it automatically with numpy.ascontiguousarray.
The precompiled libraries use OpenMP. You can disable it by setting the env variable OMP_NUM_THREADS=1, and it will yield the same performance as the version compiled without OpenMP
The original libdivsufsort only supports char as the base type. pydivsufsort can handle arrays of any integer type (even signed), by encoding each element as multiple chars, which makes the computation slower. For integer types other than uint8, sa_search, the Burrows-Wheeler transform and its inverse are implemented in Cython: the search is a binary search on the suffix array, the transform reads the suffix array, and the inverse uses a stable sort of the transform. If your values use an integer type that is bigger than required, but they span over a small contiguous range, pydivsufsort will automatically change their type (see #6).