Skip to content

PatternPiece is a light pattern matching package based on Aho-Corasick automaton with Cython.

License

Notifications You must be signed in to change notification settings

xlxwalex/PatternPiece

Repository files navigation

PatternPiece

GitHub Documentation Status

English | 简体中文

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.

Download and source code

You can fetch PatternPiece from:

The documentation is published at https://patternpiece.readthedocs.io/en/latest/

Quick start

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)

License

This library is licensed under very liberal Apache-2.0 license.

About

PatternPiece is a light pattern matching package based on Aho-Corasick automaton with Cython.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published