PatternPiece is a extended, lightweight Aho-Corasick sequence multi-pattern, multi-element matching algorithm library, serving as the initial design for the matching method.
PatternPiece is implemented in Cython
and tested on Python 3.8 and up. It works on 64 bits Linux, macOS and Windows. The license is Apache-2.0.
You can fetch PatternPiece from:
The documentation is published at https://patternpiece.readthedocs.io/en/latest/
This module is written in Cython. You need a C compiler installed to compile native extensions. In addition, due to the requirements for parallel computing, you also need to have OpenMP
installed. To install:
pip install patternpiece
Or you can also install from source, first clone the repository:
git clone https://github.com/xlxwalex/PatternPiece.git
cd PatternPiece
pip install -e .
Firstly, we can construct a toy pattern dicts.
>>> patterns = {(1, 200, 30) : 1} # (key -> pattern tuple, value -> pattern index)
Then create an PatternPiece:
>>> from patternpiece import PatternPiece
>>> matcher = PatternPiece(patterns)
It will automatically convert the patterns from a trie to an Aho-Corasick automaton to enable Aho-Corasick search, and then you can match the patterns in sequences:
>>> sequences = [[(1, 10, 100), (2, 20, 200), (3, 30, 300)]]
>>> results = matcher.match(sequences)
Note: you can input multiple sequences, and they will be matched in parallel.
Finally, you can get the results:
results:
[[(1, 0, 3)]] # (Index, start, end)
This library is licensed under very liberal Apache-2.0 license.