Search a string for multiple substrings at once
Project description
ahocorasick_rs: Quickly search for multiple substrings at once
ahocorasick_rs
allows you to search for multiple substrings ("patterns") in a given string ("haystack") using variations of the Aho-Corasick algorithm.
In particular, it's implemented as a wrapper of the Rust aho-corasick
library, and provides a faster alternative to the pyahocorasick
library.
Found any problems or have any questions? File an issue on the GitHub project.
- Quickstart
- Choosing the matching algorithm
- Additional configuration: speed and memory usage tradeoffs
- Implementation details
- Benchmarks
Quickstart
The ahocorasick_rs
library allows you to search for multiple strings ("patterns") within a haystack.
For example, let's install the library:
$ pip install ahocorasick-rs
Then, we can construct a AhoCorasick
object:
>>> import ahocorasick_rs
>>> patterns = ["hello", "world", "fish"]
>>> haystack = "this is my first hello world. hello!"
>>> ac = ahocorasick_rs.AhoCorasick(patterns)
You can construct a AhoCorasick
object from any iterable (including generators), not just lists:
>>> ac = ahocorasick_rs.AhoCorasick((p.lower() for p in patterns))
AhoCorasick.find_matches_as_indexes()
returns a list of tuples, each tuple being:
- The index of the found pattern inside the list of patterns.
- The start index of the pattern inside the haystack.
- The end index of the pattern inside the haystack.
>>> ac.find_matches_as_indexes(haystack)
[(0, 17, 22), (1, 23, 28), (0, 30, 35)]
>>> patterns[0], patterns[1], patterns[0]
('hello', 'world', 'hello')
>>> haystack[17:22], haystack[23:28], haystack[30:35]
('hello', 'world', 'hello')
find_matches_as_strings()
returns a list of found patterns:
>>> ac.find_matches_as_strings(haystack)
['hello', 'world', 'hello']
Choosing the matching algorithm
Match kind
There are three ways you can configure matching in cases where multiple patterns overlap. For a more in-depth explanation, see the underlying Rust library's documentation of matching.
Assume we have this starting point:
>>> from ahocorasick_rs import AhoCorasick, MatchKind
Standard
(the default)
This returns the pattern that matches first, semantically-speaking. This is the default matching pattern.
>>> ac AhoCorasick(["disco", "disc", "discontent"])
>>> ac.find_matches_as_strings("discontent")
['disc']
>>> ac = AhoCorasick(["b", "abcd"])
>>> ac.find_matches_as_strings("abcdef")
['b']
In this case disc
will match before disco
or discontent
.
Similarly, b
will match before abcd
because it ends earlier in the haystack than abcd
does:
>>> ac = AhoCorasick(["b", "abcd"])
>>> ac.find_matches_as_strings("abcdef")
['b']
LeftmostFirst
This returns the leftmost-in-the-haystack matching pattern that appears first in the list of given patterns. That means the order of patterns makes a difference:
>>> ac = AhoCorasick(["disco", "disc"], matchkind=MatchKind.LeftmostFirst)
>>> ac.find_matches_as_strings("discontent")
['disco']
>>> ac = AhoCorasick(["disc", "disco"], matchkind=MatchKind.LeftmostFirst)
['disc']
Here we see abcd
matched first, because it starts before b
:
>>> ac = AhoCorasick(["b", "abcd"], matchkind=MatchKind.LeftmostFirst)
>>> ac.find_matches_as_strings("abcdef")
['abcd']
LeftmostLongest
This returns the leftmost-in-the-haystack matching pattern that is longest:
>>> ac = AhoCorasick(["disco", "disc", "discontent"], matchkind=MatchKind.LeftmostLongest)
>>> ac.find_matches_as_strings("discontent")
['discontent']
Overlapping matches
You can get all overlapping matches, instead of just one of them, but only if you stick to the default matchkind, MatchKind.Standard
:
>>> from ahocorasick_rs import AhoCorasick
>>> patterns = ["winter", "onte", "disco", "discontent"]
>>> ac = AhoCorasick(patterns)
>>> ac.find_matches_as_strings("discontent", overlapping=True)
['disco', 'onte', 'discontent']
Additional configuration: speed and memory usage tradeoffs
Algorithm implementations: trading construction speed, memory, and performance
You can choose the type of underlying automaton to use, with different performance tradeoffs.
The short version: if you want maximum matching speed, and you don't have too many patterns, try the Implementation.DFA
implementation and see if it helps.
The underlying Rust library supports four choices, which are exposed as follows:
None
uses a heuristic to choose the "best" Aho-Corasick implementation for the given patterns, balancing construction time, memory usage, and matching speed. This is the default.Implementation.NoncontiguousNFA
: A noncontiguous NFA is the fastest to be built, has moderate memory usage and is typically the slowest to execute a search.Implementation.ContiguousNFA
: A contiguous NFA is a little slower to build than a noncontiguous NFA, has excellent memory usage and is typically a little slower than a DFA for a search.Implementation.DFA
: A DFA is very slow to build, uses exorbitant amounts of memory, but will typically execute searches the fastest.
>>> from ahocorasick_rs import AhoCorasick, Implementation
>>> ac = AhoCorasick(["disco", "disc"], implementation=Implementation.DFA)
Trading memory for speed
If you use find_matches_as_strings()
, there are two ways strings can be constructed: from the haystack, or by caching the patterns on the object.
The former takes more work, the latter uses more memory if the patterns would otherwise have been garbage-collected.
You can control the behavior by using the store_patterns
keyword argument to AhoCorasick()
.
AhoCorasick(..., store_patterns=None)
: The default. Use a heuristic (currently, whether the total of pattern string lengths is less than 4096 characters) to decide whether to store patterns or not.AhoCorasick(..., store_patterns=True)
: Keep references to the patterns, potentially speeding upfind_matches_as_strings()
at the cost of using more memory. If this uses large amounts of memory this might actually slow things down due to pressure on the CPU memory cache, and/or the performance benefit might be overwhelmed by the algorithm's search time.AhoCorasick(..., store_patterns=False)
: Don't keep references to the patterns, saving some memory but potentially slowing downfind_matches_as_strings()
, especially when there are only a small number of patterns and you are searching a small haystack.
Implementation details
- Matching releases the GIL, to enable concurrency.
- Not all features from the underlying library are exposed; if you would like additional features, please file an issue or submit a PR.
Benchmarks
As with any benchmark, real-world results will differ based on your particular situation. If performance is important to your application, measure the alternatives yourself!
That being said, I've seen ahocorasick_rs
run 1.5× to 7× as fast as pyahocorasick
, depending on the options used.
You can run the included benchmarks, if you want, to see some comparative results locally.
Clone the repository, then:
pip install pytest-benchmark ahocorasick_rs pyahocorasick
pytest benchmarks/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for ahocorasick_rs-0.19.0-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8fa9368c0289a08bd8d3e65da7c313971737bbe42500aa21aed0fcebe4db3b1 |
|
MD5 | c773d6368154c09c909833f862215148 |
|
BLAKE2b-256 | f7bb232ced6f637a9a868b74f5af45cd4e49c4820047239e504147c8c65df9eb |
Hashes for ahocorasick_rs-0.19.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6cf855035e1db5f160aebcfe20862c4ed1fbc1a3b08052427e61888432c59ae0 |
|
MD5 | 4d3b5b9ef8aac6cd4c0a3313def406d9 |
|
BLAKE2b-256 | 869e3da1f4f7554c76f82ad6fb1df8fbe7c8f0aa8debbbfb7fe92184c2c9a253 |
Hashes for ahocorasick_rs-0.19.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cd790f1089d90b6feafa41b901883608e1fd0de26ae004c5dd683602e7197f7 |
|
MD5 | 16e16969257554c66f999ef23ff77a09 |
|
BLAKE2b-256 | 044141a513aa971d726d02e6bee1cd3afc7dec311cc430adf261d3a31386c8c9 |
Hashes for ahocorasick_rs-0.19.0-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed8f6bba9d0d39e4652ff1b32e38ad33e60e2d3f10102b4b492abdda0731da4c |
|
MD5 | e634564d0c767e4b3e3af67582b1e7e0 |
|
BLAKE2b-256 | c9fc7bcc223fc4cc273e1787f3b59ac751c773dea6b90a6d0d76e397a3e7d813 |
Hashes for ahocorasick_rs-0.19.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46a1b665398ae0296bd9b9d19752ffdfd7db0287cb5a3c744bed675cf63b485e |
|
MD5 | 6628b5b92145e54a8e09ee6709d82105 |
|
BLAKE2b-256 | ea8b447f69d5ce68f7fe333abaf659bb2889ba23ef855e581b4012ae053858df |
Hashes for ahocorasick_rs-0.19.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 497b46af4c5ce485b6154118418232c8183a07e3a7ee0c1b5ac5cf6ac9ffcc37 |
|
MD5 | b25a0c1a867879c8fc40e7411efcd513 |
|
BLAKE2b-256 | 06bb8c794691c66eb763d0e9e4841e93e52bcf1373561d56482866764c4bfc4b |
Hashes for ahocorasick_rs-0.19.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b75ddd0ceff6ee99b243b0beb89ddcb2cc79e8a8fc78147321cc00051ab1398d |
|
MD5 | 2c1a5e203d681b81a403a017d493accb |
|
BLAKE2b-256 | f76a6b95faff4a84c998328fc51110687f0fb695723b6d3e9e1e6252a97b1ecc |
Hashes for ahocorasick_rs-0.19.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b65a1dbcba89a989db594f73be7d8392377b79f079bd9172e937a8e436fc43fa |
|
MD5 | 292acbb056f3145391094f4fc62e7c4e |
|
BLAKE2b-256 | fec67c5317fc228cfbb69c5a1c66b2b7119c4b0284e4e802fd8b7da530a7283d |
Hashes for ahocorasick_rs-0.19.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a0a8ae1d335013100ff8b703f4b55a8390dddca8ff60a350203eafeea5bc565 |
|
MD5 | 91b98e17e59f26bf92dbb54512c7e81f |
|
BLAKE2b-256 | 65846618aeba06ea3c071b4584d832a9083517493c65224b01d712d6a2280698 |
Hashes for ahocorasick_rs-0.19.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fb65bd1a137da2b6f24eae6e47b8f8438b53d5fdbf2f13270e4380621a3f3d1 |
|
MD5 | 39a86f50ea6cd92706be94101bcff318 |
|
BLAKE2b-256 | ab1bd373b33a8c6c13ea20054b9df7bbc54e84ad913c9d30d5485f445a485e5a |
Hashes for ahocorasick_rs-0.19.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9af2d542a6903a008ff932c0e052cce8db2f0a805492596c8222ed1c3f4fe20b |
|
MD5 | c56755259818302e0b598cd67c9090e0 |
|
BLAKE2b-256 | 39484341fac79ef1b628edaa531345076119139215a9c43c02684105401f7c97 |
Hashes for ahocorasick_rs-0.19.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3294b181196ac0d7f8250c004823802b9c080980f6d781327c1a3956230f7a28 |
|
MD5 | 99647a66351e090b4a1e098cc245e21f |
|
BLAKE2b-256 | 8335dd785e284e181fa2a1d0064479271846ca60013b81d8e71cc5b0afee6cf1 |
Hashes for ahocorasick_rs-0.19.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ca6aa15d8a13edfaa06c555a15ff1f1084893d596290f27a4b0962642644674 |
|
MD5 | 5fabf7fbbd146f1b286cd0300027434f |
|
BLAKE2b-256 | d5468913c05f98ca5be43b8fdab8bcc3c2c163d6fa9b7834b7768c21a776f553 |
Hashes for ahocorasick_rs-0.19.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d443565a16f027a53ead71dac6c544bff9ceaf6824d85eb3503293b3face8fbb |
|
MD5 | 2d3ecee4db202471c43cd6762a94b09f |
|
BLAKE2b-256 | e09814a09634639fc495b597301fe939f019eeae3dcd19b791d12bc4f017caba |
Hashes for ahocorasick_rs-0.19.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40309ea5339cd82ac60afadcf4b0c01a80aa3465fdca1c70d702e513df7cab04 |
|
MD5 | 8354c6279802aa30ba2ea1f821eb8285 |
|
BLAKE2b-256 | 0f5b33324f00ec46f11b2e12cf9fdbb5f960745293fcd0c24ce8c646bafaa07b |
Hashes for ahocorasick_rs-0.19.0-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 604b97209586d4b562a58c730b2110a44fc36cb090fbbb7c5fcdcee958b362b4 |
|
MD5 | f8745a70d673a093d8250c95229b4df8 |
|
BLAKE2b-256 | f0dedc6ef4875658ca591d0c0e8ab6f68c5ca7941f52364f63cae5b73f318257 |
Hashes for ahocorasick_rs-0.19.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 272f27ba8d996853e5405f5f0d4f5c3dfd0741e5f0b9ee13a8efd3303870a0a4 |
|
MD5 | c36d00dcd00a8b8fa7be98c82a2c8142 |
|
BLAKE2b-256 | 061879166884cc5d5244dfaf7252108800456f21dc8bf88a8e3bcff491df63f7 |
Hashes for ahocorasick_rs-0.19.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83dee4ed34039db52e8b24346d99efcfa2976d2837cd92fe6c017d7a3345cf87 |
|
MD5 | 4ce9983c408b398c226c09bf2d1ab44e |
|
BLAKE2b-256 | 92e66e208e4615f19497c4968ef01a2e50d3a8e4bcfe5be093617f6bfd2fb256 |
Hashes for ahocorasick_rs-0.19.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba4f3b37d9f6054091314ddb2ff904e884f657621d5dbecb80bcbc82c83189da |
|
MD5 | c7b30b019a34e9294d2984eebf315134 |
|
BLAKE2b-256 | e3e644530fab50f8206a5baec06372da3f764a809163ae507cb8d396ab2db1fe |