Skip to main content

A fast implementation of the Aho-Corasick algorithm using the compact double-array data structure

Project description

python-daachorse

daachorse is a fast implementation of the Aho-Corasick algorithm using the compact double-array data structure. This is a Python wrapper.

PyPI Build Status Documentation Status

Installation

Install pre-built package from PyPI

Run the following command:

$ pip install daachorse

Build from source

You need to install the Rust compiler following the documentation beforehand. daachorse uses pyproject.toml, so you also need to upgrade pip to version 19 or later.

$ pip install --upgrade pip

After setting up the environment, you can install daachorse as follows:

$ pip install git+https://github.com/daac-tools/python-daachorse

Example usage

Daachorse contains some search options, ranging from basic matching with the Aho-Corasick algorithm to trickier matching. All of them will run very fast based on the double-array data structure and can be easily plugged into your application as shown below.

Finding overlapped occurrences

To search for all occurrences of registered patterns that allow for positional overlap in the input text, use find_overlapping(). When you instantiate a new automaton, unique identifiers are assigned to each pattern in the input order. The match result has the character positions of the occurrence and its identifier.

>> import daachorse
>> patterns = ['bcd', 'ab', 'a']
>> pma = daachorse.Automaton(patterns)
>> pma.find_overlapping('abcd')
[(0, 1, 2), (0, 2, 1), (1, 4, 0)]

Finding non-overlapped occurrences with standard matching

If you do not want to allow positional overlap, use find() instead. It performs the search on the Aho-Corasick automaton and reports patterns first found in each iteration.

>> import daachorse
>> patterns = ['bcd', 'ab', 'a']
>> pma = daachorse.Automaton(patterns)
>> pma.find('abcd')
[(0, 1, 2), (1, 4, 0)]

Finding non-overlapped occurrences with longest matching

If you want to search for the longest pattern without positional overlap in each iteration, use MATCH_KIND_LEFTMOST_LONGEST in the construction.

>> import daachorse
>> patterns = ['ab', 'a', 'abcd']
>> pma = daachorse.Automaton(patterns, daachorse.MATCH_KIND_LEFTMOST_LONGEST)
>> pma.find('abcd')
[(0, 4, 2)]

Finding non-overlapped occurrences with leftmost-first matching

If you want to find the the earliest registered pattern among ones starting from the search position, use MATCH_KIND_LEFTMOST_FIRST.

This is so-called the leftmost first match, a bit tricky search option. For example, in the following code, ab is reported because it is the earliest registered one.

>> import daachorse
>> patterns = ['ab', 'a', 'abcd']
>> pma = daachorse.Automaton(patterns, daachorse.MATCH_KIND_LEFTMOST_FIRST)
>> pma.find('abcd')
[(0, 2, 0)]

License

Licensed under either of

at your option.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

daachorse-0.1.7.tar.gz (13.8 kB view hashes)

Uploaded Source

Built Distributions

daachorse-0.1.7-cp311-none-win_amd64.whl (162.1 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

daachorse-0.1.7-cp311-none-win32.whl (150.8 kB view hashes)

Uploaded CPython 3.11 Windows x86

daachorse-0.1.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (589.5 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

daachorse-0.1.7-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl (605.2 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.5+ i686

daachorse-0.1.7-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (524.6 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

daachorse-0.1.7-cp310-none-win_amd64.whl (162.1 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

daachorse-0.1.7-cp310-none-win32.whl (150.8 kB view hashes)

Uploaded CPython 3.10 Windows x86

daachorse-0.1.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (589.5 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

daachorse-0.1.7-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl (605.2 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.5+ i686

daachorse-0.1.7-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (524.6 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

daachorse-0.1.7-cp39-none-win_amd64.whl (162.4 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

daachorse-0.1.7-cp39-none-win32.whl (150.9 kB view hashes)

Uploaded CPython 3.9 Windows x86

daachorse-0.1.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (589.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

daachorse-0.1.7-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl (605.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.5+ i686

daachorse-0.1.7-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (525.5 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

daachorse-0.1.7-cp38-none-win_amd64.whl (162.6 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

daachorse-0.1.7-cp38-none-win32.whl (151.2 kB view hashes)

Uploaded CPython 3.8 Windows x86

daachorse-0.1.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (590.1 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

daachorse-0.1.7-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.whl (605.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.5+ i686

daachorse-0.1.7-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (526.0 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

daachorse-0.1.7-cp37-none-win_amd64.whl (162.6 kB view hashes)

Uploaded CPython 3.7 Windows x86-64

daachorse-0.1.7-cp37-none-win32.whl (151.2 kB view hashes)

Uploaded CPython 3.7 Windows x86

daachorse-0.1.7-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (590.2 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

daachorse-0.1.7-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.whl (605.9 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.5+ i686

daachorse-0.1.7-cp37-cp37m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (526.0 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page