Skip to main content

tokenizesentences

tokenizesentences banner

Tiny, dependency-free English sentence tokenizer.

CI PyPI version Python versions License: MIT DeepWiki

installation

uv add tokenizesentences

or

pip install tokenizesentences

usage

In [1]: from tokenizesentences import tokenize

In [2]: tokenize(
    "Mr. John Johnson Jr. was born in the U.S.A but earned his Ph.D. in Israel before joining Nike Inc. as an engineer. He also worked at craigslist.org as a business analyst."
    )

Out[2]:
[
    'Mr. John Johnson Jr. was born in the U.S.A but earned his Ph.D. in Israel before joining Nike Inc. as an engineer.',
    'He also worked at craigslist.org as a business analyst.'
]

Sentence offsets are available too. Every span is a half-open [start, end) slice of the input, so text[start:end] is the sentence, verbatim:

In [3]: from tokenizesentences import tokenize_spans

In [4]: tokenize_spans("It works. Try it!")
Out[4]: [(0, 9), (10, 17)]

notes

Deterministic, rule-based and English-only: an index-based scanner inspired by the answer of D Greenberg in StackOverflow, with no models and no dependencies.

Sentences are literal substrings of the input, by construction. The text is never rewritten: punctuation stays where the author put it (also inside quotes) and single newlines inside a sentence are preserved. A blank line always ends a sentence.

The heuristics are conservative: when in doubt they join, so a missed boundary is preferred over a spurious split. Known limits, pinned by the test suite:

  • A lowercase word after the mark never splits: she yelled "Stop!" and ran away. is one sentence, and so is informal text like i came. i saw.
  • A dot glued to a word never splits (that is how any domain, filename or version number survives without a TLD list), so the typo He left.Then she cried. stays joined.
  • ... (glued or spaced . . .) ends a sentence only when a capitalized word follows. I does not count: it is always capitalized, so it carries no signal.
  • After ambiguous abbreviations (p.m., Inc., U.S.A.) a sentence break is only detected before common starter words: at 5 p.m. He left splits, at 5 p.m. Monday does not.
  • Titles (Mr., Sen., St.) never end a sentence, so Main St. He walked. stays joined.

why tokenizesentences?

tokenizesentences is designed for pipelines where sentence splitting should be a small, predictable primitive rather than a heavyweight NLP component. It is conservative, fast, dependency-free and lossless:

  • Conservative: ambiguous boundaries are joined rather than split, favoring fewer false boundaries over higher recall.
  • Fast: a small index-based scanner keeps segmentation cheap in high-volume pipelines.
  • Dependency-free: no models, no data downloads, no runtime dependencies.
  • Lossless: sentences are literal slices of the original text, with exact offsets available through tokenize_spans().

That combination is built for the case where a false split costs more than a missed boundary: preprocessing, scraping, indexing, annotation, highlighting, and NLP or LLM pipelines where preserving the original text and its offsets matters more than squeezing out the last point of F1.

benchmarks

  • 40 of the 52 English Golden Rules from pragmatic_segmenter pass verbatim; the other 12 are pinned in tests/test_golden_rules.py as strict xfails with documented reasons (list detection, text rewriting, ellipsis attachment, glued sentences).

  • Boundary detection on the UD English-EWT test split: 2,077 gold sentences of raw web text in 316 reconstructed documents, scored on the 1,761 interior boundaries (the trivial final boundary of each document is excluded). All systems ran the same day (August 2026) on the same inputs with the same scoring; reproduce with uv run python tools/eval_ewt.py --competitors --speed:

    system precision recall F1 throughput
    pysbd 95.67% 84.10% 89.51% 0.05 Mchar/s
    tokenizesentences 99.50% 79.22% 88.21% 4.6 Mchar/s
    syntok 98.38% 79.16% 87.73% 0.30 Mchar/s
    nltk-punkt 97.38% 69.56% 81.15% 1.91 Mchar/s
    spacy-sentencizer 96.62% 68.14% 79.92% 0.60 Mchar/s
    blingfire 98.79% 60.02% 74.67% 1.50 Mchar/s

    Highest precision and throughput of the six on this evaluation; only pysbd scores a higher F1, trading 3.8 points of precision and two orders of magnitude of throughput for it. Recall is spent on informal lowercase web text on purpose. Timings are medians of 7 passes after a warm-up; the blingfire and syntok figures include offset reconstruction overhead. The corpus commit, its SHA256 and the competitor versions are pinned, so the run is reconstructible: see tools/eval_ewt.py and the benchmark dependency group.

  • The English suite of Wikimedia's sentencex (MIT), pinned at an exact commit and SHA256, with its parsing and per-case multiset-F1 metric replicated verbatim (234 scoreable cases; the 60-case table in their README predates their current suite). Same-day run, August 2026; reproduce with uv run python tools/eval_sentencex.py --competitors:

    system mean F1 perfect cases
    sentencex 99.94 233/234
    tokenizesentences 86.17 195/234
    blingfire 83.08 188/234
    syntok 80.79 180/234
    pysbd 64.33 136/234
    nltk-punkt 62.92 132/234
    spacy-sentencizer 62.52 134/234

    Second only to sentencex itself on sentencex's home suite, first among everything else. The ranking shuffle against the EWT table above (pysbd drops from first to fifth) is exactly why this README carries more than one benchmark.

development

uv sync --group dev
uv run pytest
uv run ruff check .
uv run ruff format --check .
uv run ty check

license

This project is licensed under the MIT License.

Download files

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

Source Distribution

tokenizesentences-0.5.1.tar.gz (26.5 kB view details)

Uploaded Source

Built Distribution

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

tokenizesentences-0.5.1-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file tokenizesentences-0.5.1.tar.gz.

File metadata

  • Download URL: tokenizesentences-0.5.1.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for tokenizesentences-0.5.1.tar.gz
Algorithm Hash digest
SHA256 75c632e97a70eb3383eed54a2a3dfd2815b5e83f1675badafdc641fb3989a1e6
MD5 2bc109ffc145c66c026d1e6ee033f943
BLAKE2b-256 2b3d33804bcab6c687d0f6df6da491ed7a8832a18d5f908f75691e7e237e14b1

See more details on using hashes here.

File details

Details for the file tokenizesentences-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: tokenizesentences-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for tokenizesentences-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 899e0f1b6021ceb96de2e479af3efb092815075a60446f955936951529583b72
MD5 a3161de936a86d7ace38a09abd11a8cc
BLAKE2b-256 55e0b3f748e53fc4cdda8da0bcb0b0e8e851def81955db8f609719ea205941ad

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 files

0.5.0

2 files

0.3

2 files

0.2

1 file

0.1

1 file

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