Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Ask DeepWiki

AhocorasickNER

AhocorasickNER is a Named Entity Recognition (NER) tool based on the Aho-Corasick algorithm. It matches text against a list of known words and phrases you define. Use it for rule-based entity extraction with pre-defined vocabularies.


Features

  • Multi-pattern string matching with the Aho-Corasick algorithm
  • Word-boundary-aware matching with greedy longest-match
  • Case-sensitive or case-insensitive modes
  • Three inference backends: pyahocorasick (C), pure NumPy, and ONNX
  • OpenVoiceOS plugin integration

Installation

uv pip install ahocorasick-ner                    # core (pyahocorasick backend)
uv pip install ahocorasick-ner[numpy]             # + pure numpy backend
uv pip install ahocorasick-ner[onnx]              # + ONNX export/inference
uv pip install ahocorasick-ner[datasets]          # + HuggingFace dataset loaders

Quick Start

from ahocorasick_ner import AhocorasickNER

ner = AhocorasickNER()
ner.add_word("city", "New York")
ner.add_word("city", "London")
ner.add_word("country", "Japan")
ner.fit()

for entity in ner.tag("I flew from New York to London, then on to Japan."):
    print(entity)
# {'start': 12, 'end': 19, 'word': 'New York', 'label': 'city'}
# {'start': 24, 'end': 29, 'word': 'London', 'label': 'city'}
# {'start': 43, 'end': 47, 'word': 'Japan', 'label': 'country'}

Backends

All three backends share the same add_word / fit / tag / save / load API.

Backend Import Dependency Persistence Use case
pyahocorasick from ahocorasick_ner import AhocorasickNER pyahocorasick (C ext) .ahocorasick (pickle) Fastest, default
numpy from ahocorasick_ner.numpy_backend import NumpyAhocorasickNER numpy .npz No C deps, portable
ONNX from ahocorasick_ner.onnx_backend import OnnxAhocorasickNER onnx + onnxruntime .onnx + .npz Edge/WASM deployment
# Numpy backend: no C extensions at inference
from ahocorasick_ner.numpy_backend import NumpyAhocorasickNER
ner = NumpyAhocorasickNER()
ner.add_word("city", "Tokyo")
ner.fit()
ner.save("model.npz")

# ONNX backend: portable to any onnxruntime deployment
from ahocorasick_ner.onnx_backend import OnnxAhocorasickNER
ner = OnnxAhocorasickNER()
ner.add_word("city", "Tokyo")
ner.fit()
ner.save("model")  # creates model.onnx + model.npz

See examples/ for complete working examples and a benchmark script.


Benchmarks

With 100k+ known phrases, this tool tags documents in milliseconds because the Aho-Corasick FSM matches all patterns in a single pass. Run the benchmark yourself:

uv pip install ahocorasick-ner[numpy,onnx]
uv run python examples/benchmark.py

Limitations

  • Greedy longest-match only, so no nested or overlapping entities
  • No fuzzy matching (typos or misspellings will not match)
  • All entities must be known beforehand

Testing

Run all tests, including the NumPy and ONNX backend tests.

# Install with test dependencies
uv pip install -e ".[test]"

# Run tests
uv run pytest test/unittests -v

# Run with coverage
uv run pytest test/unittests --cov=ahocorasick_ner --cov-report=term-missing

Tests require numpy, onnx, and onnxruntime. These packages are included in the test extra.



License

Apache 2.0. Free for commercial and non-commercial use.


Acknowledgements

Metadata

Release files for ahocorasick-ner 0.3.2a3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ahocorasick-ner 0.3.2a3
File Size Uploaded
ahocorasick_ner-0.3.2a3.tar.gz 25.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ahocorasick-ner 0.3.2a3
File Interpreter ABI Platform
ahocorasick_ner-0.3.2a3-py3-none-any.whl Python 3 none any Details

Total release size: 52.9 kB

Release files / ahocorasick_ner-0.3.2a3.tar.gz

Download URL ahocorasick_ner-0.3.2a3.tar.gz
Size 25.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b86d7440cb9ab62d47846f38148049f8a083b4d697750642d16c8e5fa58ae9ff
BLAKE2b-256 checksum
How to use checksums
cc199c33da5c2b77fce34000cbbc7ac4c5004cb0adefed4cd548a2dd16a3b39f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / ahocorasick_ner-0.3.2a3-py3-none-any.whl

Download URL ahocorasick_ner-0.3.2a3-py3-none-any.whl
Size 27.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f9aa3b5e7ad3f5038a08c6a3c7249865d9ed3f1856cc6e69293584e979633bb0
BLAKE2b-256 checksum
How to use checksums
c1fb19fa700497e0ee26fb0077f1ed8d33156ae130fed387db3285e2efdb4344
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page