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.
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.
Found any problems or have any questions? File an issue on the GitHub project.
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']
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 *
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']
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_LEFTMOST_FIRST)
>>> ac.find_matches_as_strings("discontent")
['disco']
>>> ac = AhoCorasick(["disc", "disco"], matchkind=MATCHKIND_LEFTMOST_FIRST)
['disc']
Here we see abcd
matched first, because it starts before b
:
>>> ac = AhoCorasick(["b", "abcd"], matchkind=MATCHKIND_LEFTMOST_FIRST)
>>> ac.find_matches_as_strings("abcdef")
['abcd']
This returns the leftmost-in-the-haystack matching pattern that is longest:
>>> ac = AhoCorasick(["disco", "disc", "discontent"], matchkind=MATCHKIND_LEFTMOST_LONGEST)
>>> ac.find_matches_as_strings("discontent")
['discontent']
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']
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.
- 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.
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!
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 |
This benchmarks matches ~10 patterns against lines of text that are ~70 characters long. Each line matches ~5 patterns.
Higher is better; again, ahocorasick_rs
is faster for both, though with a smaller margin.
find_matches_as_strings or equivalent |
Operations per second |
---|---|
ahocorasick_rs longest matching |
1,930,000 |
pyahocorasick longest matching |
1,120,000 |
ahocorasick_rs overlapping matching |
1,250,000 |
pyahocorasick overlapping matching |
880,000 |