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 (sometimes) faster alternative to the pyahocorasick
library.
The specific use case is searching for large numbers of patterns (in the thousands) where the Rust library's DFA-based state machine allows for faster matching.
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)
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']
Additional configuration
Match kind
There are three ways you can configure matching in cases where multiple patterns overlap. Assume we have this starting point:
>>> from ahocorasick_rs import *
>>> patterns = ["disco", "disc", "discontent"]
>>> haystack = "discontent"
MATCHKIND_STANDARD
(the default)
This returns the pattern that matches first, semantically-speaking.
>>> AhoCorasick(patterns).find_matches_as_strings(haystack)
['disc']
>>> ac = AhoCorasick(patterns, matchkind=MATCHKIND_STANDARD)
>>> ac.find_matches_as_strings(haystack)
['disc']
In this case disc
will match before disco
or discontent
.
MATCHKIND_LEFTMOST_FIRST
This returns the leftmost matching pattern that appears first in the list of patterns.
>>> ac = AhoCorasick(patterns, matchkind=MATCHKIND_LEFTMOST_FIRST)
>>> ac.find_matches_as_strings(haystack)
['disco']
disco
is returned because it precedes disc
in the list of patterns.
MATCHKIND_LEFTMOST_LONGEST
This returns the leftmost matching pattern that is longest:
>>> ac = AhoCorasick(patterns, matchkind=MATCHKIND_LEFTMOST_LONGEST)
>>> ac.find_matches_as_strings(haystack)
['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']
Implementation details
- The underlying Rust library supports two implementations, one oriented towards reducing memory usage and construction time (NFA), the latter towards faster matching (DFA). The Python wrapper only exposes the DFA version, since expensive setup compensated by fast batch operations is the standard Python tradeoff.
- 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!
Longer strings and many patterns
This benchmark matches ~4,000 patterns against lines of text that are ~700 characters long. Each line matches either zero (90%) or one pattern (10%).
Higher is better; ahocorasick_rs
is much faster in both cases.
find_matches_as_strings or equivalent |
Operations per second |
---|---|
ahocorasick_rs longest matching |
436,000 |
pyahocorasick longest matching |
65,000 |
ahocorasick_rs overlapping matching |
329,000 |
pyahocorasick overlapping matching |
76,000 |
Shorter strings and few patterns
This benchmarks matches ~10 patterns against lines of text that are ~70 characters long. Each line matches ~5 patterns.
Higher is better; ahocorasick_rs
is faster for longest match, slightly slower for overlapping matches.
find_matches_as_strings or equivalent |
Operations per second |
---|---|
ahocorasick_rs longest matching |
1,730,000 |
pyahocorasick longest matching |
1,120,000 |
ahocorasick_rs overlapping matching |
790,000 |
pyahocorasick overlapping matching |
880,000 |
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 Distributions
Built Distributions
Hashes for ahocorasick_rs-0.10.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cacb6c34cd7e20b42a0938da1425b465dec40645026534a81111127224fd460f |
|
MD5 | af0b9443648beb21928c3385657cfa1d |
|
BLAKE2b-256 | dbbe198e18a94c90fa8f35211700d8bfec019074ed9d080f5ace56409dfe2cfe |
Hashes for ahocorasick_rs-0.10.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a22c150fa9709a325e96cea40fd6b428d9967f3d65dc309221d5fbe2b5fd5a1e |
|
MD5 | 6aae0111fcf76c05b66d7131a712431d |
|
BLAKE2b-256 | f3e89e12691ba7ef2732d4772a14719c15d5327902805dc633aea81001c0b0b2 |
Hashes for ahocorasick_rs-0.10.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36eb40a0678e81a08218829dc89e2f368e665ac9216347dfc0cc5feb1281a7f6 |
|
MD5 | 87570f3ca07f55117c69093340ff3c2a |
|
BLAKE2b-256 | 8350d8cdfc1e7974ae3fe6b06f942b78252fafe8d29a406eae549a0f66ac966b |
Hashes for ahocorasick_rs-0.10.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68ad5342312c87b1b11b8d27944aa78cc8556d32fa3569f83b73f861a45ea693 |
|
MD5 | ea549270ff00fcc425cf493712355670 |
|
BLAKE2b-256 | b7ba8855f6d705ce57fce0383b0f9f3cfc2c70b2786cdc30f764d653e4077cc4 |
Hashes for ahocorasick_rs-0.10.0-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac3e40a2b76da7d6463184ebddb105cf1bd571ddd5f10576a079ba88f22debf1 |
|
MD5 | 76362d9e7a58797514028dc5e915e965 |
|
BLAKE2b-256 | 66ee0aaadf9dc0e1432f0702044745d6e65c406e984be2ab3f317450cddabc3b |
Hashes for ahocorasick_rs-0.10.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d2310da3c8fe5d80fa2ecfadd4009257b8614458e3d11178509828747c50125a |
|
MD5 | cdc0db21ec05ddf973522b836f1db58a |
|
BLAKE2b-256 | 0f145f152be1b5355ea99af70e1ec373a899ba19f72b57cb99b846c0e105ff45 |
Hashes for ahocorasick_rs-0.10.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ace8a03b71f97f8ab2828689117e8983905251d068710725e7baf0fa6f13e17e |
|
MD5 | d282f3d6edb3250b31720f1dae70da48 |
|
BLAKE2b-256 | 5ddbe96ada7a51496ba1be85f7c3a7af819f0d2e1643656162da5dd35537ed01 |
Hashes for ahocorasick_rs-0.10.0-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2502b7a3f94a9b05b6b2bf96dc011fb503976d9a3bc670544ee2e921895a3b42 |
|
MD5 | 872bd23ee77c8c945e07ce363579690d |
|
BLAKE2b-256 | 9af081d78b2f78026873743a23aac2f2347845226378c8dc34e81ee8a1edb17c |
Hashes for ahocorasick_rs-0.10.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a558ae9904fe5540672e892141bf76870ae68ed1167c99fe7fac5c817f0579aa |
|
MD5 | e26f2735b5576da2ca9e27cfd40c3c8a |
|
BLAKE2b-256 | 9ace4d9faa94a56768f324bd7a0312a7d2b57e5bf58af30428fa29f0ca09b192 |
Hashes for ahocorasick_rs-0.10.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf0270b79939db2d99dba493315fad06f9fa4997288f0e20ed38955eb6407d25 |
|
MD5 | 0e21235dc7cd4f77c080701ea3792701 |
|
BLAKE2b-256 | 1806b270734ba00cac72177bdab60fae622c3ed7fd9c3e4ccccbe815f5ab99dd |
Hashes for ahocorasick_rs-0.10.0-cp36-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb996ca3996abf8123be08df5ed3756ee4b35b0751ae8ad440180e4dc68b9b73 |
|
MD5 | 0d1b1db0c7831600abedeff01d8eec23 |
|
BLAKE2b-256 | d68f57c4ae4961a26733198c71fb0b63d01ca13f7299d00d4c1947ded1a0430d |
Hashes for ahocorasick_rs-0.10.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c320858c0758c3e496e71751758c8477577e20b42c715b92eb773485a98c7f0b |
|
MD5 | b78b4c20b09a46022dd93ec08bba5e62 |
|
BLAKE2b-256 | 19afae7804a7d5a64ff05b674edf4883e856084c2f1aabb26f7749b60f70a044 |
Hashes for ahocorasick_rs-0.10.0-cp36-cp36m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cbc281206dc2dcfdf2e53e84b6025809a4beda1e699a5bb203f05af08a7459b2 |
|
MD5 | 81dae8faac4d3d262ff10697f3bdb395 |
|
BLAKE2b-256 | d4d72fc6e5189db93524b5209f1519d79dafc9323fc64df900dc5aea139a5b82 |