This release is a pre-release and may not be stable for production use.
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.
Related Projects
- OpenVoiceOS: the voice assistant platform this library's
opmextra plugs into (seeahocorasick_ner/opm.py) - simple_NER: another TigreGotico NER library, referenced in the dataset reference docs
License
Apache 2.0. Free for commercial and non-commercial use.
Acknowledgements
- pyahocorasick: C-based Aho-Corasick implementation
- Hugging Face Datasets: domain-specific corpora
Metadata
Release files for ahocorasick-ner 0.3.2a4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ahocorasick_ner-0.3.2a4.tar.gz | 25.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ahocorasick_ner-0.3.2a4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 52.9 kB
Release files / ahocorasick_ner-0.3.2a4.tar.gz
| Download URL | ahocorasick_ner-0.3.2a4.tar.gz |
|---|---|
| Size | 25.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
70932baa27c4c26ef41ea8af23c06d207cbe32735999c0c812134fbfe4b2b26b
|
|
BLAKE2b-256 checksum How to use checksums |
cbfe24b3dec2c8f2e5fa93e3fc3e7217f3f788e72fa7a38e16820647c64023d9
|
| 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.2a4-py3-none-any.whl
| Download URL | ahocorasick_ner-0.3.2a4-py3-none-any.whl |
|---|---|
| Size | 27.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
10a987a79d7f60f65c02bf9c91c5b3f6f13f6fa3d97eb6e81f16f42a653af973
|
|
BLAKE2b-256 checksum How to use checksums |
aee6ae9656943c6ff4057d1df7e78702d5e725c9d84f360f1caf764e8cdffa02
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|