Skip to main content

nltk-punkt-tokenize

A modern implementation of the Punkt unsupervised sentence boundary detector (Kiss & Strunk, 2006) that cannot be made to execute code by a model file.

Zero dependencies. Standard library only. 19 pretrained models ship inside the wheel; nothing is downloaded, ever.

The dependency direction is deliberate: this package is intended for NLTK to depend on, so that Punkt can be vendored out of NLTK. Nothing here imports NLTK — not the library, not the tests, not the CI.

pip install nltk-punkt-tokenize
import punkt

punkt.sent_tokenize("Dr. Smith went home. He was tired.")
# ['Dr. Smith went home.', 'He was tired.']

punkt.sent_tokenize("Das ist z.B. ein Satz. Und noch einer.", "german")
# ['Das ist z.B. ein Satz.', 'Und noch einer.']

Why this exists

Punkt is a good algorithm with a bad delivery mechanism. NLTK's models were distributed as Python pickles fetched at run time, which is CVE-2024-39705: the pickle grammar contains an opcode meaning "import this module, look up this name, and call it with these arguments", so loading a model is arbitrary code execution. No amount of care in the calling code changes that, because the file chooses what gets called.

The usual reflex — swap pickle for json and declare victory — does not finish the job. JSON cannot name a class, so it is not a code-execution primitive; but json.loads builds the entire object graph before any validation code runs, which leaves the whole denial-of-service family intact. See Security.

What's different

Models are data, not programs. A Punkt model is four containers of strings and integers. The bundled ones are compiled into the package as Python literals, so loading one is an import — no file to find, no format to parse, no initialisation. Everything else is read by a hand-written bounded parser that has no way to construct a class or call a function.

The layers are separated. NLTK's punkt.py is one 1,800-line module where the language rules, the learner, and the tokenizer share mutable state through a common base class. Here:

Layer Holds Depends on
punkt.rules Language conventions, regexes, the token object nothing
punkt.model Learned parameters as data, plus every codec rules
punkt.annotate The Kiss & Strunk decision procedure rules, model
punkt.training The learner. Writes models, never tokenizes the above
punkt.inference The tokenizer. Reads models, never trains the above

The trainer has no tokenize(). The tokenizer has no train(). Hyperparameters live in a frozen TrainerConfig rather than as class attributes you have to monkey-patch.

It matches NLTK exactly. NLTK's output was captured once, from identical parameters, into tests/data/parity.json. The suite asserts against that snapshot: equal sentences and spans over 400 generated documents, equal trained models, and bit-exact log-likelihood scores. Because the reference is recorded rather than imported, parity is checked on every machine and in every CI job — and NLTK never has to be installed to check it. Four divergences are deliberate and asserted as such, so they cannot quietly become five.

Usage

Splitting

tokenizer = punkt.PunktSentenceTokenizer(punkt.load_model("english"))

tokenizer.tokenize(text)                          # list[str]
list(tokenizer.span_tokenize(text))               # [(start, end), ...]
tokenizer.tokenize(text, realign_boundaries=False)

Whitespace inside a sentence is preserved exactly, including newlines. Only whitespace between sentences is dropped.

Training

Punkt learns from unannotated text — no labelled sentence boundaries required.

params = punkt.train(corpus_text, language="danish")
punkt.write_model(params, "danish.punkt.gz")

Incrementally, for a corpus too large to hold in memory:

trainer = punkt.PunktTrainer()
for chunk in chunks:
    trainer.train(chunk, finalize=False)
params = trainer.get_params()

Tuning:

config = punkt.TrainerConfig(abbrev_threshold=0.5, include_all_collocations=True)
params = punkt.train(corpus_text, config=config)

Understanding a decision

for decision in tokenizer.debug_decisions("Dr. Smith went home."):
    print(punkt.format_decision(decision))
Text: 'Dr. Smith' (at offset 2)
Sentence break? False (default decision)
Collocation? False
'dr.':
    known abbreviation: True
    is initial: False
'smith':
    known sentence starter: False
    orthographic heuristic suggests is a sentence starter? unknown
    orthographic contexts in training: {'MID-UC', 'UNK-UC'}

A new language

Language rules are an immutable value, not a subclass:

greek = punkt.LanguageVars(sent_end_chars=(".", ";", "!"))
tokenizer = punkt.PunktSentenceTokenizer(params, rules=greek)

Command line

punkt tokenize book.txt                 # one sentence per line
punkt tokenize -m german artikel.txt
punkt spans book.txt                    # start, end, sentence
cat corpus.txt | punkt train - -o mine.punkt.gz -l english
punkt convert english.pickle english.punkt.gz
punkt info english --show
punkt explain "Dr. Smith went home."
punkt languages

Migrating from NLTK

NLTK here
nltk.sent_tokenize(text) punkt.sent_tokenize(text)
nltk.download("punkt") not needed — models are bundled
PunktSentenceTokenizer(train_text) PunktSentenceTokenizer(punkt.train(train_text))
PunktTrainer.ABBREV = 0.5 TrainerConfig(abbrev_threshold=0.5)
tokenizer._params tokenizer.params
PunktLanguageVars subclass LanguageVars(...) value

Existing .pickle models convert without being unpickled:

punkt convert ~/nltk_data/tokenizers/punkt/english.pickle english.punkt.gz

punkt_tab directories are read directly, and write_punkt_tab() produces directories NLTK can load.

Deliberate differences from NLTK 3.8.1

  • Unicode quotes. Curly quotes and guillemets are treated as closing punctuation, so “Hello there.” Bye. realigns correctly. NLTK added this after 3.8.1 (gh-1682); this package follows the newer behaviour.
  • Abbreviation smoothing. The abbreviation log-likelihood adds 1e-8 to its null probability, matching NLTK's development branch. Without it a corpus containing no period-final tokens raises ValueError from log(0).
  • No mutation on read. NLTK stores ortho_context in a defaultdict(int) and reads it with [], so tokenizing silently grows the model with a zero entry per unknown word. Reads here do not mutate.
  • Real booleans. is_initial and friends return bool, not a truthy re.Match. The orthographic heuristic returns a three-valued enum instead of True/False/"unknown" in one variable.

Why the name

nltk-punkt on PyPI is an unrelated 1.2 KB package that downloads NLTK's punkt data — the very thing this replaces — so the distribution is nltk-punkt-tokenize. The import name is just punkt.

Security

Threat model

A model file is untrusted input. It may come from a package index, a colleague, a CI cache, or an attacker.

Attack Defence
Code execution via pickle No loader calls pickle.load. Legacy pickles are parsed with pickletools.genops and replayed through a data-only machine; GLOBAL pushes an inert marker, REDUCE consults a fixed table, PERSID/EXT* are refused
Gzip bomb Decompressed bytes counted while streaming and capped
Multi-gigabyte model Size, line count, line length and per-section entry caps
JSON nesting exhausting the parser stack Bracket depth counted before json.loads is called
Quadratic number parsing Numeric literal length is bounded. int(digits) is O(n²), and Python 3.9/3.10 have no ceiling — a 2 MB run of digits passes both a depth check and a size check, then pins a core for minutes. Measured: >120 s unguarded, 0.09 s guarded
Hash/memory pressure from huge objects Total element count is bounded before parsing. A post-hoc len() cannot help: the dict already exists
Unbounded JSON string String literal length is bounded
Schema confusion Every JSON field is type- and range-checked; unknown keys are refused, not ignored
Malformed data loading as a different model Strict UTF-8, no errors="replace"; unknown sections rejected; flag bits validated against a mask
Callback-driven JSON parsing object_hook, object_pairs_hook and parse_constant are never passed

The four JSON bounds are applied to the raw text in a single linear pass by punkt.model.safety.prescan_json, before the parser is invoked at all — because anything checked after json.loads returns is checked too late.

Limits are configurable:

punkt.read_model("untrusted.punkt", limits=punkt.LoadLimits(max_bytes=8 << 20))

The one place code runs

Bundled models are Python modules, and importing a module executes it. That is the same trust boundary every Python package already has — you trust the code you installed — and it is why the bundled route is used only for models inside the wheel, covered by the wheel's own hash and reviewed at release.

Models from anywhere else go through the parsers, which cannot execute anything. What made CVE-2024-39705 a vulnerability was that NLTK's models fell on the wrong side of that line: downloaded at run time, then unpickled.

Two tests enforce this. One walks the package AST asserting no unpickling call exists. The other walks the generated data modules asserting they contain nothing but assignments.

Performance

Loading a bundled model is an unmarshal of a precompiled .pyc rather than a parse:

time
Parse a 20,000-entry text model ~66 ms
Import the equivalent compiled module ~5 ms

The first import of a language compiles its module to a .pyc — 200 ms for English, 800 ms for Finnish, once per installation. pip normally does this at install time, so it is not usually observed.

Training caches the corpus token total, which NLTK recomputes inside two per-type loops; this makes training linear in vocabulary size rather than quadratic, with identical results.

Models

19 languages, from Kiss & Strunk's original multilingual evaluation plus later contributions, as redistributed by NLTK: Czech, Danish, Dutch, English, Estonian, Finnish, French, German, Greek, Italian, Malayalam, Norwegian, Polish, Portuguese, Russian, Slovene, Spanish, Swedish, Turkish.

punkt.available_languages()

Models are looked up in order: an explicit path, then $PUNKT_MODEL_PATH, then ~/.punkt/models, then the bundled modules — so a locally trained english.punkt shadows the bundled one with no code change.

The Russian model contains abbreviations only, with no orthographic data. That is how it is distributed upstream; it will detect fewer boundaries than the others.

Requirements

Python 3.9+. No dependencies, at runtime or otherwise — the package imports only the standard library, and pip install nltk-punkt-tokenize pulls in nothing else. CI asserts this three ways: over the AST, over the distribution metadata, and by making import nltk raise and then exercising the package.

Development

pip install -e ".[dev]"
pytest
pytest -m nltk        # parity tests, requires nltk installed
ruff check src tests
mypy

The test suite never imports NLTK. Parity is asserted against a recorded snapshot of NLTK's output in tests/data/parity.json, which is both cycle-free and pinned, so a change in an installed NLTK cannot silently move the goalposts.

Two maintainer tools exist, neither shipped in the distribution:

# Rebuild the bundled models from NLTK data.
python tools/build_data.py ~/nltk_data/tokenizers/punkt_tab

# Re-baseline the parity snapshot. The only file in the repo that imports NLTK.
pip install nltk && python tools/generate_parity_fixtures.py

References

Kiss, T. & Strunk, J. (2006). Unsupervised Multilingual Sentence Boundary Detection. Computational Linguistics, 32(4), 485–525.

Dunning, T. (1993). Accurate Methods for the Statistics of Surprise and Coincidence. Computational Linguistics, 19(1), 61–74.

Licence

Apache-2.0. The pretrained models are redistributed from the NLTK project and were trained by Jan Strunk and Tibor Kiss; see NOTICE.

Download files

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

Source Distribution

nltk_punkt_tokenize-1.0.0.tar.gz (3.5 MB view details)

Uploaded Source

Built Distribution

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

nltk_punkt_tokenize-1.0.0-py3-none-any.whl (3.4 MB view details)

Uploaded Python 3

File details

Details for the file nltk_punkt_tokenize-1.0.0.tar.gz.

File metadata

  • Download URL: nltk_punkt_tokenize-1.0.0.tar.gz
  • Upload date:
  • Size: 3.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.12

File hashes

Hashes for nltk_punkt_tokenize-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c9e2119ea0292c8db60647f02fb533af367812878461e84d57166e76097913ba
MD5 ccfe4e1a6918596dec4f64475a26a4ca
BLAKE2b-256 394f8d95b3e1bc2f9eee96cdd12fc005663cb1ec0070eb1c9e83444fb934589f

See more details on using hashes here.

File details

Details for the file nltk_punkt_tokenize-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for nltk_punkt_tokenize-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9bf1f5a445dafadf2815623c5fdfc13e2c3cae42e51daf415f7bca51092b08cc
MD5 4cde589abbce185d91e17a5c1bdabebe
BLAKE2b-256 46e85f55a56d0b116f906948bcc1b446e04c60c416a0410dfbbd4852e3355993

See more details on using hashes here.

Supported by

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