Skip to main content

Drop-in Rust-accelerated replacement for NLTK. Same API, 5-50x faster.

Project description

fastNLTK

Drop-in Rust-accelerated replacement for NLTK.
Same API. Same data. 5–50× faster.

CI PyPI version Python Rust License codecov


Overview

fastNLTK is a drop-in replacement for the Natural Language Toolkit that keeps the exact same Python API while replacing hot paths with native Rust code. Import from fastnltk instead of nltk — no other changes needed. All NLTK corpus data works without re-downloading.

NLTK is the most widely-used NLP teaching library in the world, but its pure-Python implementation means regex, loops, and dict lookups all run through the interpreter. That's 10–50× slower than compiled code — and production regressions have made NLTK's performance unpredictable (e.g. 0.55s → 216s on 30K chars between versions).

fastNLTK preserves the exact API while replacing the engine.

# Before
import nltk
nltk.download("punkt")
tokens = nltk.word_tokenize("Hello, world!")

# After — same code, different import
import fastnltk as nltk   # or: from fastnltk import word_tokenize
tokens = nltk.word_tokenize("Hello, world!")

Performance

68 automated benchmarks across all 16 Rust modules. Geometric mean 8.5× vs NLTK (51 NLTK comparison benchmarks, 17 fastNLTK-only). Every function below has an NLTK counterpart unless noted in BENCHMARKS.md.

Module Benchmarks Best Speedup Engine
classify 4 339× Maxent GIS training in Rust
metrics 4 168× Pure algorithmic port, zero Python overhead
tokenize 16 94× Compiled regex + logos lexer
tag 9 73× rustling HMM, hashbrown FastMap lookups
sentiment 1 38× VADER in Rust, no regex re-compilation
sem 1 28× Expression parser in Rust
parse 2 26× Earley + CFG parsing
collocations 3 23× FastMap ngram frequency counting
stem 8 15× rust-stemmers (Snowball C) + native Rust
translate 1 BLEU in Rust
tree 1 Tree parser in Rust
chunk 1 Regexp chunk parser
probability 4 FreqDist, ConditionalFreqDist, prob dists
cluster 1 K-means Lloyd's algorithm
chat 1 Eliza chatbot
ccg 1 CCG category parsing
lm 6 MLE, Lidstone, Laplace, StupidBackoff, KneserNey, WittenBell ¹
inference 4 Tableau, Resolution, Discourse, DefaultReasoner ¹
Totals 68 339× geom mean 8.5× (51 NLTK comparisons, 16 Rust modules)

Full benchmark details →

API Coverage

Module Rust‑accelerated Python shim Status
tokenize Treebank, Toktok, Tweet, Regexp, Space, MWE, TextTiling, Punkt, SExpr, Logos DFA Fallback to NLTK
stem Porter, Lancaster, Snowball, Regexp, WordNet, ARLSTem, Cistem, ISRI, RSLP
tag PerceptronTagger, TnT, HMM, DefaultTagger, Unigram/Bigram/TrigramTagger, RegexpTagger, AffixTagger
classify NaiveBayes, Maxent, TextCat
probability FreqDist, ConditionalFreqDist, MLEProbDist, LaplaceProbDist
lm MLE, Lidstone, Laplace, KneserNey, WittenBell, StupidBackoff Fallback to NLTK
collocations Bigram/Trigram/Quadgram finders
ccg Chart parser, lexicon, combinators
inference Tableau prover, Resolution prover, Discourse QA
drt DRS parsing, FOL conversion
sem Expression parser, model evaluation
metrics Association, agreement, segmentation, distance, Jaccard, Spearman
chunk Regexp chunker (NP/VP extraction)
cluster K-means clustering
sentiment VADER sentiment analysis
parse CFG, Earley chart parser
tree Tree data structure (bracket parse, subtrees, productions)
corpus NLTK corpus reader wrappers Reading API
chat Eliza-style chatbot
translate BLEU score
data Resource finder, bincode cache

Quick Start

pip install fastnltk
python -m nltk.downloader punkt averaged_perceptron_tagger wordnet
from fastnltk import word_tokenize, pos_tag, sent_tokenize
from fastnltk.corpus import nltk_data

# Tokenization
tokens = word_tokenize("The quick brown fox jumps over the lazy dog.")
print(tokens)
# → ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog', '.']

# Sentence segmentation
sents = sent_tokenize("Dr. Smith went home. He ate dinner.")
print(sents)
# → ['Dr. Smith went home.', 'He ate dinner.']

# POS tagging
tagged = pos_tag(tokens)
print(tagged)
# → [('The', 'DT'), ('quick', 'JJ'), ('brown', 'NN'), ('fox', 'NN'), ...]

# Parsing
from fastnltk import Tree
tree = Tree.from_string("(S (NP The/DT cat/NN) (VP runs/VBZ))")
print(tree.leaves())      # → ['The/DT', 'cat/NN', 'runs/VBZ']
print(tree.productions()) # → ['S -> NP VP', 'NP -> The/DT cat/NN', ...]

Installation

pip install fastnltk

Pre-built wheels for Linux (x86_64, aarch64), macOS (x86_64, arm64), and Windows (x64). Requires Python 3.8+ and an existing NLTK data installation.

From source

git clone https://github.com/your/fastnltk
cd fastnltk
pip install maturin
maturin develop --release   # Development install
# or
maturin build --release     # Build wheel

Data

fastNLTK uses NLTK's corpus data. If you have NLTK installed with data, no additional downloads are needed:

python -m nltk.downloader punkt averaged_perceptron_tagger wordnet

Development

See CONTRIBUTING.md for detailed setup, code style, testing, and PR workflow.

# Clone and build
git clone https://github.com/your/fastnltk
cd fastnltk
pip install -e ".[dev]"
maturin develop --release

# Run tests
cargo test                       # 279 Rust tests
pytest tests/                     # 254 Python tests

# Quality checks
cargo fmt --all -- --check
cargo clippy --all-targets
ruff check fastnltk/ tests/

# Benchmarks
maturin develop --release
python -m benchmarks.run --save   # Run + save results

License

fastNLTK is licensed under the Apache License, Version 2.0. See LICENSE for details.

fastNLTK is not affiliated with, endorsed by, or sponsored by NLTK or its maintainers. NLTK is a trademark of the NLTK Project.

Contact + Support

Created by Wyatt Ferguson

For any questions or comments heres how you can reach me:

:octopus: Follow me on Github @wyattferguson

:mailbox_with_mail: Email me at wyattxdev@duck.com

:tropical_drink: Follow on BlueSky @wyattf

If you find this useful and want to tip me a little coffee money:

:coffee: Buy Me A Coffee

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastnltk-0.4.0.tar.gz (259.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

fastnltk-0.4.0-cp38-abi3-win_amd64.whl (1.5 MB view details)

Uploaded CPython 3.8+Windows x86-64

fastnltk-0.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

fastnltk-0.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.5 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

File details

Details for the file fastnltk-0.4.0.tar.gz.

File metadata

  • Download URL: fastnltk-0.4.0.tar.gz
  • Upload date:
  • Size: 259.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastnltk-0.4.0.tar.gz
Algorithm Hash digest
SHA256 f9218820fb9a99af33a028f38865b190e707c076119d7f88996389285c56be3b
MD5 7309a1274f3f02b4387aab03dd3ac2e0
BLAKE2b-256 831f496197f12d9076062408f58e94428ae31b9271688c2ff61bcd8f480dae0d

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastnltk-0.4.0.tar.gz:

Publisher: release.yml on wyattferguson/fastNLTK

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastnltk-0.4.0-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: fastnltk-0.4.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastnltk-0.4.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ad94c965eb7c38ed5aff1ddb8e287a97820b1311b1dc6afa4f6245342c2cb9c4
MD5 e9fb2439992f78f8b7950b46c1794df7
BLAKE2b-256 359b5b6dabb3dd81e52274b8e5f139eafc65c517c6e069b2fc403bbf6316cd6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastnltk-0.4.0-cp38-abi3-win_amd64.whl:

Publisher: release.yml on wyattferguson/fastNLTK

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastnltk-0.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fastnltk-0.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7d86351e1c87e14a89aa77d242556c18b0c5780466010cc32c33023d64830abb
MD5 0bec6587838ca3dfd7abd9dd18e81dc1
BLAKE2b-256 067000d9551e667170350dd5c70a4f8707e9a1b6b6e987176bce00fab4910eb1

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastnltk-0.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on wyattferguson/fastNLTK

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastnltk-0.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for fastnltk-0.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3bd1a2e2b4f2c599332191838ccf06d2a2edc43be6b0c8262a80fdcf08e339bb
MD5 8c22c9ca4c736a15c5d13d9827861552
BLAKE2b-256 49bae175614fb96922215652c08ccff829e0949b0ed16b3e541a215791a8ce57

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastnltk-0.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on wyattferguson/fastNLTK

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page