Sentence segmenter
A sentence segmentation library written in Rust language with wide language support optimized for speed and utility.
Bindings
Besides native Rust, bindings for the following programming languages are available:
Approach
- A sentence-terminating punctuation mark (
.,?,!, plus language specific terminators like।or။) ends a sentence by default. Some languages use different terminators so a list of known terminator symbols is maintained for supported languages. - Hand-compiled abbreviation lists, exclamation words, numbered references (
See [1]. Next sentence.), and quote-aware rules (see below) suppress or relocate boundaries where the default rule would over-split. We collect a list of known, popular abbreviations in supported languages. - List-item starts (e.g., bullets
*/+/-/•, numeric1./1)/(1), lettereda)/(a), romanii.) emit sentence boundaries so each item segments cleanly, even when items are written inline on one line. A sibling rule (≥2 matches of the same marker family per paragraph, or a single Tier-1 line-start) keeps prose with stray(1894)ore. e. cummingsfrom being mis-split. - Multi-character punctuation runs (
. . .,! ?,? ? ?, glued or space-separated) are treated as a single terminator. This generalises the ellipsis (…/...) case: any mix of.,!,?- repeated, spaced, or interleaved - collapses into one boundary candidate instead of several. Continuation heuristics then decide whether the following token starts a new sentence: uppercase non-Isplits, while lowercase, digits, or glued continuations (e.g.mean...see) keep the sentence intact.
Sometimes, it is very hard to get the segmentation correct. In such cases this library is opinionated and prefer not segmenting than wrong segmentation. If two sentences are accidentally together, that is ok. It is better than sentence being split in middle. Avoid over engineering to get everything linguistically 100% accurate.
This approach would be suitable for applications like text to speech, machine translation.
Trade-offs
The opinionated don't-over-split stance coexists with several rules that do recover real boundaries (numbered references, quote-aware handling, abbreviation/exclamation suppression). The aim is to be conservative where context is ambiguous, while still picking up structural signals that make a split safe.
Consider this example: We make a good team, you and I. Did you see Albert I. Jones yesterday?
The accurate splitting of this sentence is
["We make a good team, you and I.", "Did you see Albert I. Jones yesterday?"]
However, to achieve this level precision, complex rules need to be added and it could create side effects. Instead, if we just don't segment between I. Did, it is ok for most of downstream applications.
List-item detection follows the same conservative posture. Ambiguous inline shapes that collide with prose (bare 1. / a. / ii. closers inline, single-letter a. patterns that look like initials, parenthesised numbers like (1894) that read as years) deliberately do not trigger list segmentation. A sibling rule (≥2 matches of the same marker family per paragraph, or a single Tier-1 line-start) further suppresses one-off occurrences. The result: real lists segment per item, but prose containing list-shaped fragments stays intact.
Several other small heuristics follow the same "recover when the signal is clear, otherwise leave it joined" posture:
- Stray punctuation around terminators: a period immediately followed by a comma (
…ice cream. , It was…) is treated as stray punctuation and the sentence continues through it. Whitespace between an abbreviation and its terminator (U.S .) is tolerated when looking up the abbreviation, so the boundary is still suppressed. - Dot-coded tokens like chess notation: tokens of the shape
<digit>.<letter…>(e.g.7.Bg5,1.e4) do not emit a boundary, so move codes and similar dot-coded identifiers stay inside their sentence. - Slash-joined abbreviations: tokens like
171/U.S.are split on/when extracting the trailing word, so the abbreviation on the right-hand side is still recognised.
The sentence segmentation in this library is non-destructive. This means, if the sentences are combined together, you can reconstruct the original text. Line breaks, punctuations and whitespaces are preserved in the output.
Usage
Rust
Install the library using
Then, any text can be segmented as follows.
use segment;
The first argument is language code, second argument is text to segment. The segment method returns an array of identified sentences.
Python
Install from PyPI:
=
# Segment text into sentences
=
# Get sentence boundaries with indices
=
See bindings/python/example.py for more examples.
Node.js
Install from npm:
import from 'sentencex';
const text = "The James Webb Space Telescope (JWST) is a space telescope specifically designed to conduct infrared astronomy. The U.S. National Aeronautics and Space Administration (NASA) led Webb's design and development.";
// Segment text into sentences
const sentences = ;
sentences.;
// Get sentence boundaries with indices
const boundaries = ;
boundaries.;
For CommonJS usage:
const = require;
See bindings/nodejs/example.js for more examples.
WebAssembly (Browser)
Install from npm:
or use a CDN like https://esm.sh/sentencex-wasm
import init from 'https://esm.sh/sentencex-wasm;
;
C# / .NET 10+
Install from nuget:
using Sentencex;
string language = "en";
string inputText = "The James Webb Space Telescope (JWST) is a space telescope specifically designed to conduct infrared astronomy. The U.S. National Aeronautics and Space Administration (NASA) led Webb's design and development.";
string[] sentences = Segmenter.Segment("en", inputText);
// Segment text into sentences
foreach (string sentence in sentences)
Console.WriteLine($"Sentence: {sentence}");
// Get sentence boundaries with indices and text
SentenceBoundary[] boundaries = Segmenter.GetSentenceBoundaries(language, inputText);
foreach (SentenceBoundary boundary in boundaries)
Console.WriteLine($"Sentence: '{boundary.Text}' (indices: {boundary.StartIndex}-{boundary.EndIndex})");
// Get sentence boundary indices without text
SentenceBoundarySlim[] boundariesSlim = Segmenter.GetSentenceBoundariesSlim(language, inputText);
foreach (SentenceBoundarySlim boundary in boundariesSlim)
Console.WriteLine($"Sentence indices: {boundary.StartIndex}-{boundary.EndIndex}");
Language support
The aim is to support all languages where there is a wikipedia. Instead of falling back on English for languages not defined in the library, a fallback chain is used. The closest language which is defined in the library will be used. Fallbacks for ~244 languages are defined.
Performance
Following is a sample output of sentence segmenting The Complete Works of William Shakespeare. This file is 5.29MB. As you can see below, it took 178 milli second.
|
)
Measured on English Golden Rule Set (GRS) using mean F1 score across 60 test cases.
The benchmark script is at benchmarks/compare.py and can be run with uv run benchmarks/compare.py.
The following libraries are compared:
- mwtokenizer — Wikimedia rule-based tokenizer
- blingfire — Microsoft's fast tokenizer (C library)
- nltk — Punkt sentence tokenizer
- pysbd — Python port of pragmatic segmenter
- spacy — dependency-parse based sentence segmentation
- syntok — rule-based tokenizer
| Tokenizer | English GRS F1 Score |
|---|---|
| sentencex | 100.00 |
| pysbd | 93.00 |
| blingfire | 91.67 |
| syntok | 85.67 |
| spacy | 81.67 |
| mwtokenizer | 78.00 |
| nltk | 72.33 |
Thanks
- https://github.com/diasks2/pragmatic_segmenter for test cases. The English golden rule set is also sourced from it.
- https://github.com/mush42/tqsm for an earlier Rust port of this library.
License
MIT license. See License.txt