skillfed

ahocorapy

ahocorapy - Pure python ahocorasick implementation

ahocorapy v1.8.0 77.4K downloads/30d#14,530 on PyPI217
Permissive license MIT Active released

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 ahocorapy

uv

uv add ahocorapy

poetry

poetry add ahocorapy

Installing 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

Development Status :: 5 - Production/StableIntended Audience :: DevelopersProgramming Language :: Python :: 2Programming Language :: Python :: 2.7Programming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.3Programming Language :: Python :: 3.4Programming Language :: Python :: 3.5Programming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Topic :: Software Development :: Libraries :: Python Modules

Tags

aho-corasick algorithm pythonmulti-keyword searchpattern matching multiple stringsfast substring searchkeyword tree lookupsequence matching algorithmtext search library
string-matchingalgorithm-implementationpure-python

More Python Modules packages