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.2a2

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.2a2
File Size Uploaded
ahocorasick_ner-0.3.2a2.tar.gz 25.8 kB Details

Built distribution (wheel)

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

Total release size: 52.9 kB

Release files / ahocorasick_ner-0.3.2a2.tar.gz

Download URL ahocorasick_ner-0.3.2a2.tar.gz
Size 25.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3965fa9b04a35c1313d32caf39310dc5101b36f3ac59ce84e773ab961fc605b6
BLAKE2b-256 checksum
How to use checksums
97ecf325ec1ed4000f788a0bd42cdb15948399a6c850e3af46b01da5b4190fb9
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.2a2-py3-none-any.whl

Download URL ahocorasick_ner-0.3.2a2-py3-none-any.whl
Size 27.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6339aca4471671f9ffd6a80d5ebf99ada0cd132975138a0bb2719d58e84085e1
BLAKE2b-256 checksum
How to use checksums
a4fbd9f7c1f926526902b256a259eab6ec33ef82f5bfa99e1c8c107d93fd9af0
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