Skip to main content

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 byte positions of the occurrence and its identifier.

>>> import daachorse
>>> patterns = [b'bcd', b'ab', b'a']
>>> pma = daachorse.DoubleArrayAhoCorasick(patterns)
>>> pma.find_overlapping(b'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 = [b'bcd', b'ab', b'a']
>>> pma = daachorse.DoubleArrayAhoCorasick(patterns)
>>> pma.find(b'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 = [b'ab', b'a', b'abcd']
>>> pma = daachorse.DoubleArrayAhoCorasick(patterns, daachorse.MATCH_KIND_LEFTMOST_LONGEST)
>>> pma.find(b'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 = [b'ab', b'a', b'abcd']
>>> pma = daachorse.DoubleArrayAhoCorasick(patterns, daachorse.MATCH_KIND_LEFTMOST_FIRST)
>>> pma.find(b'abcd')
[(0, 2, 0)]

Find patterns on a string

To build an automaton for strings, use CharwiseDoubleArrayAhoCorasick instead.

>>> import daachorse
>>> patterns = ['全世界', '世界', 'に']
>>> pma = daachorse.CharwiseDoubleArrayAhoCorasick(patterns)
>>> pma.find('全世界中に')
[(0, 3, 0), (4, 5, 2)]

Profile-Guided Optimization (PGO)

The double array constructed by daachorse is designed to improve CPU cache efficiency. However, it is not always the optimal layout for every document being scanned.

daachorse addresses this issue by supporting Profile-Guided Optimization (PGO). By providing a corpus when constructing the automaton, the memory layout is optimized, which may improve performance on documents similar to the given corpus.

>>> import daachorse
>>> patterns = [b'ab', b'a', b'abcd']
>>> pma = daachorse.DoubleArrayAhoCorasick(patterns, corpus=[b'abcd'])

License

Licensed under either of

at your option.

Metadata

Release files for daachorse 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for daachorse 0.5.0
File Size Uploaded
daachorse-0.5.0.tar.gz 14.3 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for daachorse 0.5.0
File
daachorse-0.5.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
daachorse-0.5.0-cp310-abi3-win32.whl CPython 3.10 abi3 Windows x86-32 Details
daachorse-0.5.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details
daachorse-0.5.0-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl CPython 3.10 abi3 Linux glibc 2.5+ x86-32 Details
daachorse-0.5.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl CPython 3.10 abi3 macOS 10.12+ x86-64, macOS 10.12+ universal2 (ARM64, x86-64), macOS 11.0+ ARM64 Details

Total release size: 1.6 MB

Release files / daachorse-0.5.0.tar.gz

Download URL daachorse-0.5.0.tar.gz
Size 14.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a8a2acf1e304de70e498a68a1f050157a0dedf87c8ab78cad0b3b191a801e171
BLAKE2b-256 checksum
How to use checksums
f7fdc4820bba8fa92fab3b4959faa7d2a2e0da8445f27a15284c5c15fe6835fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / daachorse-0.5.0-cp310-abi3-win_amd64.whl

Download URL daachorse-0.5.0-cp310-abi3-win_amd64.whl
Size 194.0 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
2d552e3accb7e4ab05a3995a64aa499209236bb68513e54732dce237320ff7e3
BLAKE2b-256 checksum
How to use checksums
25e53156f428ce27d2ba00d9dadc45a6266686a51a750ca4a692e9dfdfe5c512
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / daachorse-0.5.0-cp310-abi3-win32.whl

Download URL daachorse-0.5.0-cp310-abi3-win32.whl
Size 185.3 kB
Tags CPython 3.10 Windows x86-32 abi3
SHA-256 checksum
How to use checksums
83782054af226b48236530fdd06141f06b748891c37764335c1ac77dbd22366e
BLAKE2b-256 checksum
How to use checksums
bec7d83cf8a2b4eb88eb3e47beb269be937834ae0b342bcbb5c27911a5e947c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / daachorse-0.5.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL daachorse-0.5.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 310.1 kB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
c1db6a4510fea9beb47e55bac96951f22a4ca9ccdae9d58de877f65d25c8103f
BLAKE2b-256 checksum
How to use checksums
3822272e0054748147ed5d5aff55abcb486604f80854e6deffa569580e5dbaa8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / daachorse-0.5.0-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl

Download URL daachorse-0.5.0-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Size 333.1 kB
Tags CPython 3.10 Linux glibc 2.5+ x86-32 abi3
SHA-256 checksum
How to use checksums
61990ad8ec31e6021b8a02c85815b436c7488610dac743efa2a303c0045d4eea
BLAKE2b-256 checksum
How to use checksums
ad8d13bb04f00eaf432e27ca0e04f07036a2405b181d3356b5f71a911279fe34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / daachorse-0.5.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl

Download URL daachorse-0.5.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Size 569.6 kB
Tags CPython 3.10 abi3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
da7b7c50acea44ba67dfd387e2d9ad1fe28596cd94d10bd9ab3105817a5c07cb
BLAKE2b-256 checksum
How to use checksums
4bfbf88dfce3105d4995775a54da9465e6b835beb1c0edf92a0217e8d234aa1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.5.0 This release

6 release files

0.2.2

26 release files

0.2.1

26 release files

0.2.0

26 release files

0.1.5

20 release files

0.1.4

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page