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
- If it's a period, it ends a sentence.
- If the preceding token is in the hand-compiled list of abbreviations, then it doesn't end a sentence.
However, it is not 'period' for many languages. So we will use a list of known punctuations that can cause a sentence break in as many languages as possible.
We also collect a list of known, popular abbreviations in as many languages as possible.
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.
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.
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;
;
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. List cases are excluded.
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