{"id":2206,"library":"pyahocorasick","title":"pyahocorasick","description":"pyahocorasick is a fast and memory-efficient Python library for exact or approximate multi-pattern string search. It allows finding multiple key string occurrences simultaneously in an input text using the Aho-Corasick algorithm. Implemented in C for performance, it supports Python 3.10+ and runs on Linux, macOS, and Windows. The library maintains an active development status with regular releases, with the current version being 2.3.0.","status":"active","version":"2.3.0","language":"en","source_language":"en","source_url":"https://github.com/WojciechMula/pyahocorasick","tags":["string search","aho-corasick","trie","pattern matching","text processing"],"install":[{"cmd":"pip install pyahocorasick","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Requires Python 3.10 or newer. Prior versions (3.6-3.9) are no longer supported in recent releases.","package":"python","optional":false}],"imports":[{"note":"The Python module is named 'ahocorasick', not 'pyahocorasick'.","wrong":"from pyahocorasick import Automaton","symbol":"Automaton","correct":"from ahocorasick import Automaton"}],"quickstart":{"code":"import ahocorasick\n\n# Create an Automaton object\nA = ahocorasick.Automaton()\n\n# Add keywords and associated values (optional)\nwords_to_find = {\n    \"apple\": \"fruit\",\n    \"apply\": \"verb\",\n    \"banana\": \"fruit\",\n    \"band\": \"music\"\n}\n\nfor idx, (word, value) in enumerate(words_to_find.items()):\n    A.add_word(word, (idx, value))\n\n# Finalize the automaton for efficient searching\nA.make_automaton()\n\n# Search in a haystack string\nhaystack = \"I like to eat an apple and apply for a new band that plays banana songs.\"\n\nprint(\"Found matches:\")\nfor end_index, (insertion_order, original_value) in A.iter(haystack):\n    start_index = end_index - len(original_value) + 1\n    print(f\"  Found '{original_value}' at ({start_index}, {end_index})\")\n\n# Example of retrieving a value (trie-like behavior)\nprint(f\"\\nValue for 'apple': {A.get('apple')}\")\n\n# Check existence\nprint(f\"Is 'apple' in the automaton? {'apple' in A}\")","lang":"python","description":"Initialise an `Automaton`, add words (and optional values), then call `make_automaton()` to compile the trie into a searchable Aho-Corasick automaton. The `iter()` method yields `(end_index, value)` for all matches found in the input string."},"warnings":[{"fix":"Upgrade to a supported Python version (3.10+) or pin `pyahocorasick` to an older version compatible with your Python environment (e.g., `<2.1.0` for 3.6/3.7, `<2.2.0` for 3.8, `<2.3.0` for 3.9).","message":"Python 3.9 support was dropped in v2.3.0. Python 3.8 support was dropped in v2.2.0, and Python 3.6/3.7 in v2.1.0. Users on older Python versions must use an earlier `pyahocorasick` release.","severity":"breaking","affected_versions":">=2.1.0"},{"fix":"Rebuild your automaton if it was pickled or saved with a version prior to 1.4.0 when upgrading to 1.4.0 or later.","message":"The internal trie representation changed in v1.4.0, breaking compatibility with pickle and `save()` formats from previous versions. Automata pickled or saved with older versions cannot be loaded by v1.4.0 or newer.","severity":"breaking","affected_versions":">=1.4.0"},{"fix":"Always use `import ahocorasick` to access the library's functionality.","message":"The correct Python module to import is `ahocorasick`, not `pyahocorasick`. Attempting to import `pyahocorasick` will result in an `ImportError`.","severity":"gotcha","affected_versions":"All versions"},{"message":"After adding all words to the `Automaton` (which acts as a Trie initially), you must call the `make_automaton()` method to finalize it into an Aho-Corasick automaton before performing searches with `iter()` or related methods. Failing to do so will result in an `Automaton` in `TRIE` kind state, not `AHOCORASICK` kind.","severity":"gotcha"},{"fix":"Ensure a C compiler (e.g., GCC on Linux, Xcode Command Line Tools on macOS, MSVC on Windows) is installed and configured if you need to install from source or if pre-built wheels are not available for your specific platform/Python version.","message":"Installing `pyahocorasick` from source requires a C compiler to build the CPython extension. While pre-built wheels are generally available on PyPI, source installation without a compiler will fail.","severity":"gotcha","affected_versions":"All versions (when installing from source)"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}