ahocorapy
ahocorapy - Pure python ahocorasick implementation
What it is and what it does
ahocorapy is a pure-Python implementation of the Aho-Corasick algorithm designed to search for multiple keywords in text or arbitrary sequences with linear-time complexity. It was created to address gaps in existing libraries: it supports unicode in Python 2.7 without C extensions (making it platform-independent), and it uses suffix-shortcutting during setup to accelerate lookup times. The library works with any hashable symbols—strings, tuples of integers, lists of tokens, or bytes—making it flexible for character-level, word-level, or token-level matching.
The package trades setup time for faster search performance by precomputing state transitions and match positions. On sparse text it matches C-extension performance on CPython; on dense text (many overlapping matches) it remains slower than C extensions but faster than other pure-Python alternatives. It is fully pickleable using a non-recursive serialization strategy to handle large keyword trees, and it supports case-insensitive matching for string-based searches.
Use it for:
- Scan large documents for a fixed set of names, terms, or patterns without installing compiled dependencies.
- Perform word-level keyword matching by building a tree from token lists instead of character sequences.
- Search binary data or custom symbol sequences (tuples, lists) using the same algorithm without string conversion.
- Serialize and reuse large pre-built keyword trees across application restarts via pickling.
- Find all overlapping keyword matches in a single pass through text, returning both matches and their positions.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
ahocorapy implements the Aho-Corasick algorithm in pure Python to search for multiple keywords in text or arbitrary sequences in linear time, supporting unicode and case-insensitive matching.
Yes. ahocorapy is worth installing for multi-keyword search when you need a pure-Python, platform-independent solution with unicode support and no compiled dependencies. It is actively maintained, has no security vulnerabilities, carries a permissive MIT license, and performs competitively on typical (sparse) text. Install it if C extensions are unavailable or undesirable; if you are searching very dense text with many overlapping matches and latency is critical, a C-based alternative may be faster.
Install
ahocorapy on PyPI
pip
pip install ahocorapyuv
uv add ahocorapypoetry
poetry add ahocorapyInstalling ahocorapy
Before you install
Installation is straightforward with no runtime dependencies and low friction. The package is actively maintained with a release 24 days old and has been stable since its first release in 2018.
License in practice
MIT license permits unrestricted use, modification, and distribution in both open-source and commercial projects with minimal restrictions.
Quickstart
pip install ahocorapy
from ahocorapy.keywordtree import KeywordTree
kwtree = KeywordTree(case_insensitive=True)
kwtree.add('malaga')
kwtree.add('lacrosse')
kwtree.finalize()
result = kwtree.search('My favorite islands are malaga and sylt.')
print(result)
Tree construction is not thread-safe; concurrent calls to add() have undefined behavior. Finalize the tree before using it in multi-threaded search contexts.
Verify before relying
- Whether the library's performance on dense text (0.42s for 100 searches) meets your latency requirements compared to C-extension alternatives.
- Memory overhead of the suffix-shortcutting optimization relative to other pure-Python implementations in your use case.
- Whether pickling support for huge keyword trees is tested at the scale your application requires.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=2.7) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 24 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 77,375/month — #14,530 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: ahocorapy-1.8.0-py2.py3-none-any.whl
Keywords: keyword, search, purepython, aho-corasick, ahocorasick
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
ahocorasick-rsSearches for multiple substrings in text or…
permissive · top 15,000 on PyPI
textsearchFind and replace multiple strings in text with…
permissive · top 15,000 on PyPI
flashtextExtracts or replaces keywords in text using the…
permissive · top 5,000 on PyPI
suffix-treesImplements suffix trees and generalized suffix…
permissive · top 15,000 on PyPI
strsimpyImplements a dozen string similarity and…
permissive · top 15,000 on PyPI
pylcsComputes longest common subsequence, longest…
permissive · top 15,000 on PyPI
regexA drop-in replacement for Python's standard…
permissive · top 100 on PyPI
HLLEstimates the cardinality (unique count) of…
permissive · top 15,000 on PyPI
confusablesDetects and matches words that appear identical…
permissive · top 15,000 on PyPI