A high-accuracy, from-scratch Sentence Boundary Detector (SBD) for production pipelines. Features a drop-in adapter for pysbd to fix edges cases without heavy refactoring.
Project description
"Even a pair of scissors deserves to be smart. Welcome to cybernetic boundary shearing."
[!WARNING] This project is currently in alpha.
Table of Contents generated with DocToc
- 🎬 Manifesto
- 🌐 Supported Languages
- 🏁 Benchmarks
- 📦 Installation
- 📟 Usage
- 🗺 Features & Roadmap
- 🤝 Contributors
- 📜 Last note
🎬 Manifesto
Yet Another Sentence Boundary Detector is a pair of smart scissors for text. Pointer-based, from-scratch SBD for production NLP pipelines. Features a drop-in adapter for pysbd to fix edge cases without heavy refactoring.
✂ Why do I need a pair of "smart scissors" for text?
Running re.split(r'\.\s+[A-Z]') and praying. This blunt tool instantly shears titles like Mr. Smith or French corporate markers like Sté. Générale in half, scattering semantic fragments across the pipeline.
Punctuation is the most overloaded glyph set in text. A period alone does six jobs and only one is "sentence end." Generic split-on-punctuation fails on:
Dr.Inc.U.S.A.(abbreviation markers, not boundaries. ~47% of periods in news text are these)3.5M3.14(decimal points, not sentence ends)D. H. Lawrence(initials. Two periods, zero boundaries)...(ellipsis. Trailing off or sentence end? ambiguous)1.a.at line start (inline list markers impersonating sentence ends)?!inside quotes (punctuation nesting across boundaries)
And multilingual quirks a naive splitter never saw coming.
🔪 Are these shears just a rusty regex loop spray-painted in carbon fiber?
Regex is how I cut. Not what I am. My brain is a two-pass pipeline:
Pass 1 — Naive boundary finder. Finds every position that could plausibly end a sentence: periods, question marks, exclamation points — anything followed by whitespace, uppercase, or a newline. Deliberately over-inclusive. Better to catch a false positive than miss a real boundary.
Pass 2 — Cross-references 9+ mid-sentence patterns to surgically excise false positives:
- Newline inside sentence
- Title/initialism protection
- Abbreviation lists
- Geopolitical + case markers
- Quote/parenthesis span filtering
- TOC leader suppression
- List marker re-alignmen
- Contiguous terminator collapsing
- Language specifical final fixups
🌐 Supported Languages
10 languages supported today. Target is 22+.
| Code | Language | |
|---|---|---|
| 🇸🇦 | ar | Arabic |
| 🇩🇪 | de | German |
| 🇬🇧 | en | English |
| 🇪🇸 | es | Spanish |
| 🇫🇷 | fr | French |
| 🇭🇹 | ht | Haitian Creole |
| 🇯🇵 | ja | Japanese |
| 🇵🇹 | pt | Portuguese |
| 🇷🇺 | ru | Russian |
| 🇨🇳 | zh | Chinese |
🏁 Benchmarks
Tested against 6 competitors (pysbd, sentencex, sentsplit, nupunkt, blingfire, sentence-splitter) across 5 languages and 7 edge cases: compound abbreviations, CJK quotes, newline wrapping, chat logs, URLs, decimals, and nested punctuation.
TL;DR: yasbd ranked #1 in accuracy across almost every test, while staying competitive on speed as pure Python. blingfire is faster but brittle. pysbd and sentencex shred French abbreviations.
On our golden benchmark (85 English edge cases adapted from pysbd's official golden rule set with fixes and additions): yasbd scores 100.0%, pysbd 84.7%.
Full results, terminal output, and a performance graph can be found in benchmarks/
📦 Installation
Ready to do some cybernetic boundary shearing? Let's get you set up quickly and painlessly.
The Quick & Easy Way
The simplest way to get started is with pip:
pip install yasbd-lib -U
[!TIP] Termux (Android)
No Rust toolchain? Install pydantic-core pre-built wheels first, then retry:
pip install typing-extensions pip install pydantic-core --index-url https://termux-user-repository.github.io/pypi/ pip install "pydantic>=2.12.4,<2.13"
That's it! Blade is armed.
The From-Source Way
Prefer building from source? Clone and install manually for full control:
git clone https://github.com/speedyk-005/yasbd-lib.git
cd yasbd
pip install .
(But honestly, the pip way is way easier.)
Want to Help Make yasbd Even Better?
That's awesome. See Contributing Guide.
📟 Usage
[!TIP] Looking for the pysbd drop-in replacement? Jump straight to the Adapter section.
Initialization
from yasbd.boundary_detector import BoundaryDetector
# Or from yasbd import BoundaryDetector
# Basic setup
detector = BoundaryDetector(lang="en")
# With all options (so far.)
detector = BoundaryDetector(
# ISO 639 code (e.g., en, fr, es, ...). Defaults to `en`.
# https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes
lang="fr",
# Don't split inside them. (It won't protect block quotes) Defaults to `True`.
# https://en.wikipedia.org/wiki/Block_quotation
preserve_quote_and_paren=True,
# Enable verbose logging. Defaults to `False`.
verbose=True,
)
Switching languages at runtime is a property set:
detector.lang = "es"
The rule module loads lazily on first access. Switching mid-stream reimports the module and rebinds the pattern cache. Zero config, no restarts needed.
Core Methods
The two primary APIs are detect() and segment().
Both methods accept plain strings, open text streams (TextIOBase), or a StreamCleaner instance. Inputs are processed lazily as a stream of paragraphs, allowing large documents to be handled without loading everything into memory at once.
detect()yields sentence boundary offsets.segment()yields sentence strings.
Boundary detection
detect() tells you where each sentence stops. Integer offsets into the original input stream.
Two detection modes:
- absolute: (default) offsets count from the start of the entire input stream.
- relative: offsets reset at each paragraph boundary. A
ParagraphEOFsentinel signals the gap between paragraphs.
# absolute mode (default)
res= list(detector.detect('She turned to him, "This is great." She held the book out to show him.'))
print(res)
# [35, 70]
# relative mode with paragraph break
detector.lang = "es"
res = list(detector.detect(
"El Sr. García llegó ayer. La Sra. López también.\n\nVéase la pág. 55 del libro.",
relative=True,
))
print(res)
# [25, 48, ParagraphEOF, 27]
Segmentation
If you do not want to manage boundary offsets yourself (and who would?), segment() slices text for you.
detector.lang = "en"
# Basic sentence splitting
res = list(detector.segment("Hello world. How are you? I am fine."))
print(res)
# ['Hello world.', 'How are you?', 'I am fine.']
# Multi-paragraph with whitespace preserved
res = list(detector.segment(
"First para.\nStill first.\n\nSecond para.\nFinished.",
preserve_whitespace=True,
))
print(res)
# ['First para.', '\nStill first.', '\n\n', 'Second para.', '\nFinished.']
[!TIP] ParagraphStream — yasbd uses
ParagraphStreaminternally to split text into paragraph blocks. You can import it directly if you need paragraph-level processing in your own code:from yasbd.utils.paragraph_stream import ParagraphStream for para in ParagraphStream(text): # or an opened file print(para) # each paragraph blockYou can also skip empty lines with
skip_empty_lines=True
Cleaner
OCR'd a PDF, parsed a DOCX, or scraped noisy HTML? "StreamCleaner" normalizes text before it reaches the language detector or sentence segmenter. StreamCleaner accepts either a string or an open text stream and yields cleaned paragraphs lazily.
from yasbd.utils.cleaner import StreamCleaner
cleaner = StreamCleaner("Hello world. This is messy.")
list(cleaner)
# ['Hello world. This is messy.']
"StreamCleaner" implements the iterator protocol and yields cleaned paragraphs one at a time. It can consume plain strings, open text files, and other text streams.
from yasbd.utils.cleaner import StreamCleaner
with open("document.txt", encoding="utf-8") as f:
for paragraph in StreamCleaner(f):
print(paragraph)
Common cleanup operations include:
- Fixing mojibake and OCR artifacts
- Removing HTML tags
- Normalizing whitespace and repeated slashes
- Rejoining hyphenated words split across lines
- Merging vertically stacked characters
Individual cleanup steps can be disabled by provided a collection of steps to skip:
cleaner = StreamCleaner(
text,
steps_to_skip=[
"fix_ocr_text",
"normalize_spaces",
],
)
Available steps:
- "fix_mojibake"
- "fix_ocr_text"
- "unwrap_htmls"
- "normalize_slashes"
- "normalize_spaces"
You can pass a "StreamCleaner" instance directly to "detect()" or "segment()" to clean text as it is processed.
Adapter
Migrating from pysbd? Swap the import and keep your pipeline:
# Before: from pysbd import Segmenter
from yasbd.utils.pysbd_adapter import Segmenter
seg = Segmenter(language="ja")
res = seg.segment('田中さんは「準備は完了しました」そう言って部屋を出た。U.S.A.の経済政策は非常に複雑です。')
print(res)
# ['田中さんは「準備は完了しました」そう言って部屋を出た。', 'U.S.A.の経済政策は非常に複雑です。']
Same API surface. Same Segmenter class. Same segment() method signature.
🗺 Features & Roadmap
- Regex caching (compile once per language class)
- Drop-in pysbd adapter (same API, no pipeline changes)
- StreamCleaner for OCR'd and noisy text
- spaCy integration
- 22+ language targets
- CLI tool
- REST API for remote boundary detection
🤝 Contributors
A massive thank you to the open source community helping make yasbd more accurate and scalable:
| Name | Role |
|---|---|
| @speedyk-005 | Maintainer & Creator |
| @JheanLL | Trie prototype design & Spanish rule contributions |
| @Jah-yee | Community contributor |
| @Rajesh270712 | Base + English rule contributions |
Interested in contributing? See the Contributing Guide to get started!
📜 Last note
yasbd is maintained by speedyk-005. Licensed under Mozilla Public License 2.0 — you can use it freely in commercial and private work.
Star us on GitHub if you dig it. Tell your NLP pipeline we said hi. 🚀
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file yasbd_lib-0.3.0.tar.gz.
File metadata
- Download URL: yasbd_lib-0.3.0.tar.gz
- Upload date:
- Size: 45.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2246df5f3c89886a9df6fbd61c3afb98b80827724ee57ba56ac673f5f476b01f
|
|
| MD5 |
dcc30f4904827af1c75dad6eb7ca4ce6
|
|
| BLAKE2b-256 |
f3d0d036de6971fffddffc2ce797aa24811bd8f31275650036bfa9f0cb082c9e
|
Provenance
The following attestation bundles were made for yasbd_lib-0.3.0.tar.gz:
Publisher:
publish.yml on speedyk-005/yasbd-lib
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
yasbd_lib-0.3.0.tar.gz -
Subject digest:
2246df5f3c89886a9df6fbd61c3afb98b80827724ee57ba56ac673f5f476b01f - Sigstore transparency entry: 1757754470
- Sigstore integration time:
-
Permalink:
speedyk-005/yasbd-lib@12fb3260ecd7ffd60def92134658f3685b0e7e28 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/speedyk-005
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@12fb3260ecd7ffd60def92134658f3685b0e7e28 -
Trigger Event:
push
-
Statement type:
File details
Details for the file yasbd_lib-0.3.0-py3-none-any.whl.
File metadata
- Download URL: yasbd_lib-0.3.0-py3-none-any.whl
- Upload date:
- Size: 43.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75f575dd750b88f6bb43a53e290f2f8470c4cfe85c277f001112173909a0bcf8
|
|
| MD5 |
3060e6a52ec6692a78260362fc615435
|
|
| BLAKE2b-256 |
40b15e9dc6f1b800a9dd12f52742febec30fe205d8a5323c073aa6a145977e16
|
Provenance
The following attestation bundles were made for yasbd_lib-0.3.0-py3-none-any.whl:
Publisher:
publish.yml on speedyk-005/yasbd-lib
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
yasbd_lib-0.3.0-py3-none-any.whl -
Subject digest:
75f575dd750b88f6bb43a53e290f2f8470c4cfe85c277f001112173909a0bcf8 - Sigstore transparency entry: 1757754852
- Sigstore integration time:
-
Permalink:
speedyk-005/yasbd-lib@12fb3260ecd7ffd60def92134658f3685b0e7e28 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/speedyk-005
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@12fb3260ecd7ffd60def92134658f3685b0e7e28 -
Trigger Event:
push
-
Statement type: