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.

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.5.0.tar.gz (14.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

daachorse-0.5.0-cp310-abi3-win_amd64.whl (194.0 kB view details)

Uploaded CPython 3.10+Windows x86-64

daachorse-0.5.0-cp310-abi3-win32.whl (185.3 kB view details)

Uploaded CPython 3.10+Windows x86

daachorse-0.5.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (310.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

daachorse-0.5.0-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl (333.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.5+ i686

daachorse-0.5.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (569.6 kB view details)

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

File details

Details for the file daachorse-0.5.0.tar.gz.

File metadata

  • Download URL: daachorse-0.5.0.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for daachorse-0.5.0.tar.gz
Algorithm Hash digest
SHA256 a8a2acf1e304de70e498a68a1f050157a0dedf87c8ab78cad0b3b191a801e171
MD5 d1ebc09a61771bc6101a2aacc37876b9
BLAKE2b-256 f7fdc4820bba8fa92fab3b4959faa7d2a2e0da8445f27a15284c5c15fe6835fb

See more details on using hashes here.

File details

Details for the file daachorse-0.5.0-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: daachorse-0.5.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 194.0 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using 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}

File hashes

Hashes for daachorse-0.5.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 2d552e3accb7e4ab05a3995a64aa499209236bb68513e54732dce237320ff7e3
MD5 7809d4d6a567c671892daa3c4e4755a7
BLAKE2b-256 25e53156f428ce27d2ba00d9dadc45a6266686a51a750ca4a692e9dfdfe5c512

See more details on using hashes here.

File details

Details for the file daachorse-0.5.0-cp310-abi3-win32.whl.

File metadata

  • Download URL: daachorse-0.5.0-cp310-abi3-win32.whl
  • Upload date:
  • Size: 185.3 kB
  • Tags: CPython 3.10+, Windows x86
  • Uploaded using 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}

File hashes

Hashes for daachorse-0.5.0-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 83782054af226b48236530fdd06141f06b748891c37764335c1ac77dbd22366e
MD5 ab96f4bfec9ca45d82f9a74725a70aa7
BLAKE2b-256 bec7d83cf8a2b4eb88eb3e47beb269be937834ae0b342bcbb5c27911a5e947c7

See more details on using hashes here.

File details

Details for the file daachorse-0.5.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

  • Download URL: daachorse-0.5.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 310.1 kB
  • Tags: CPython 3.10+, manylinux: glibc 2.17+ x86-64
  • Uploaded using 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}

File hashes

Hashes for daachorse-0.5.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c1db6a4510fea9beb47e55bac96951f22a4ca9ccdae9d58de877f65d25c8103f
MD5 e1bd72629df33a150f6949c80f28d1d5
BLAKE2b-256 3822272e0054748147ed5d5aff55abcb486604f80854e6deffa569580e5dbaa8

See more details on using hashes here.

File details

Details for the file daachorse-0.5.0-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

  • Download URL: daachorse-0.5.0-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl
  • Upload date:
  • Size: 333.1 kB
  • Tags: CPython 3.10+, manylinux: glibc 2.5+ i686
  • Uploaded using 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}

File hashes

Hashes for daachorse-0.5.0-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 61990ad8ec31e6021b8a02c85815b436c7488610dac743efa2a303c0045d4eea
MD5 54be38952e63964d336fac6d73b11fd2
BLAKE2b-256 ad8d13bb04f00eaf432e27ca0e04f07036a2405b181d3356b5f71a911279fe34

See more details on using hashes here.

File details

Details for the file daachorse-0.5.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

  • Download URL: daachorse-0.5.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
  • Upload date:
  • Size: 569.6 kB
  • Tags: CPython 3.10+, macOS 10.12+ universal2 (ARM64, x86-64), macOS 10.12+ x86-64, macOS 11.0+ ARM64
  • Uploaded using 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}

File hashes

Hashes for daachorse-0.5.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 da7b7c50acea44ba67dfd387e2d9ad1fe28596cd94d10bd9ab3105817a5c07cb
MD5 a49241ef3636e214f91e0c57c5aafdf9
BLAKE2b-256 4bfbf88dfce3105d4995775a54da9465e6b835beb1c0edf92a0217e8d234aa1d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page