WojciechMula/pyahocorasick logo

WojciechMula/pyahocorasick

Free

Python module (C extension and plain python) implementing Aho-Corasick algorithm

FreeFree tier
Type
Open Source

About WojciechMula/pyahocorasick

pyahocorasick is a fast and memory-efficient Python library for exact or approximate multi-pattern string search, implementing the Aho-Corasick algorithm. It provides an ahocorasick module that can be used as a dict-like trie or converted into an automaton for efficient searching. The library is implemented in C, supports Python 3.9 and up, and runs on 64-bit Linux, macOS, and Windows. It is licensed under BSD-3-Clause. Key features include the ability to build a search index ahead of time and persist it via pickle, and a simple API for adding words, checking membership, and iterating over all occurrences in input text. The library is used in production for tasks such as counting CRISPR guides in DNA sequencing reads.

Key Features

Exact and approximate multi-pattern string search
Trie and automaton modes for flexible use
C extension for high performance and memory efficiency
Supports Python 3.9 and up
Cross-platform: 64-bit Linux, macOS, Windows
BSD-3-Clause license
Serialization/deserialization via pickle for index persistence
Dict-like interface for trie operations
Iter() method returns all occurrences as (end_index, value) tuples

Pros & Cons

Pros
  • Fast and memory efficient due to C implementation
  • Simple API similar to Python dict for trie usage
  • Multi-pattern search in a single pass over input
  • Cross-platform support for Linux, macOS, Windows
  • Open source with permissive BSD-3 license
  • Proven in production at AstraZeneca for bioinformatics
Cons
  • Requires a C compiler to install from source (via pip includes binary wheels for common platforms)
  • Limited to Python 3.9+ (no support for older Python versions)
  • May be overkill for simple single-pattern search tasks

Best For

Finding multiple keywords in text simultaneouslyBioinformatic sequence analysis (e.g., counting CRISPR guides in DNA reads)Efficient substring matching in large datasetsText processing and search automation

FAQ

How do I install pyahocorasick?
You can install it using pip: pip install pyahocorasick. A C compiler is needed if binary wheels are not available for your platform.
How do I use pyahocorasick for multi-pattern search?
Create an Automaton object, add words with the add_word() method, then call make_automaton() to convert the trie into an automaton. Use the iter() method on input text to get all occurrences as (end_index, value) tuples.
Is pyahocorasick fast?
Yes, it is implemented in C and designed for high performance and memory efficiency. It has been used in production bioinformatics pipelines for tasks like counting CRISPR guides in millions of DNA sequencing reads.
What license does pyahocorasick use?
The library is licensed under BSD-3-Clause, with some utility code (tests, pure Python automaton) dedicated to the Public Domain.