If you work with an under-resourced, regional, or constructed language, you eventually hit the exact same wall: standard NLP tools pretend your language does not exist.
When your language is missing from the usual en/es/fr dropdowns, the temptation is to roll your own sentence splitter from scratch. You write a quick regular expression to split on periods, exclamation marks, and question marks, and call it a day.
Ten minutes later, your pipeline breaks because real-world text is messy.
The Real-World Obstacles: Why Naive Splitters Fail
Punctuation is the most overloaded set of marks in writing. A period does roughly six different jobs, and only one of them is ending a sentence. A naive splitter immediately falls apart when confronted with everyday textual complexity:
- Emails and URLs: Addresses like
support@example.orgor links containing paths likehttps://example.org/path.htmlare peppered with periods that naive regex loops treat as hard stops. - Ellipses and Trailing Thoughts: An ellipsis (
...) can signify an unfinished thought or a formal boundary, confusing blunt parsers. - Quotes and Parentheses: Terminal punctuation nested inside dialogue (
"Wait!" she shouted.) or parenthetical citations ((See Fig. 2.)) frequently gets sheared mid-thought. - Decimal & Abbreviations: Markers like
Dr.,Sr., decimals (3.14), or initials (D. H. Lawrence) trigger false splits. - Noisy Formats (Markdown & OCR): Raw documents often carry HTML tags, markdown formatting, or OCR mojibake—such as collapsed horizontal lists—that disrupt boundary detection. Fortunately,
yasbd-libautomatically detects and handles collapsed OCR lists out of the box.
Fixing one regex lookbehind inevitably breaks three others. Instead of shipping your NLP application, you end up maintaining a brittle state machine.
When Do You Actually Need This?
If your language is already supported out-of-the-box by one of the built-in languages in yasbd-lib and you don't need custom rules, you can skip custom modules entirely and just use BoundaryDetector(lang="xx"). But when you need fine-tuned control over custom or under-resourced idioms, custom rules become essential.
Why Not pysbd or Other Existing Libraries?
If you look around the Python ecosystem for rule-based sentence boundary detection (SBD), you will run into libraries like pysbd, sentencex, blingfire, or spaCy's sentencizer. While they work well enough for major supported languages, extending them to a new language can be rigid.
As highlighted in the Extensibility: Configuration Approaches breakdown, traditional SBD libraries often hardcode language registries internally or tie rule definitions tightly to monolithic codebases. With pysbd, adding a new language typically means forking the repository, wiring up internal factory classes, and wrestling with rigid rule structures. On the yasbd-lib benchmarks (v1.0.2), yasbd runs ~10.8× faster than pysbd (v0.3.4) on large texts like The Adventures of Sherlock Holmes while scoring 99.0% vs. 82.4% on English edge cases.
Enter yasbd-lib and Declarative Language Profiles
yasbd-lib (Yet Another Sentence Boundary Detector) solves this by decoupling matching mechanics from language-specific data.
Because yasbd-lib's built-in set of languages is frozen for the v1.x series (though non-breaking improvements to existing modules remain welcome), new languages are distributed as external language packs. Instead of rewriting an entire parser, you inherit yasbd-lib's powerful two-pass pointer engine, leverage its highly customizable architecture, and override declarative sets or hook methods:
- Declarative Rule Subclasses: Define custom sets (
TITLE_ABBRVS,REFERENCE_ABBRVS, etc.) to filter false positives natively. - Implicit Language Code Derivation: You don't need to manually define a
LANGattribute;yasbd-libautomatically derives the ISO code by stripping suffixes your class name (e.g.,LtRulesmaps to"Lt"). It is case-insensitively. - Dynamic Regex Compilation (
_compile_regex_dynamically): Programmatically build or compile custom finders for regional edge cases that standard rules miss. - Post-Processing Hooks (
post_process_boundaries): Manually inject or clean up boundary offsets using custom match finders before finalizing segmentations. - External Runtime Loading & CLI Support: Custom profiles can be loaded dynamically via Python code or executed directly via dedicated command-line flags.
Step-by-Step: Building Your Language Profile
To add a new language, you create a dedicated module (e.g., lt.py), subclass Rules, and override only the specific sets or methods your language actually needs. The class name itself determines the language code (e.g., naming your class LtRules automatically targets "lt").
1. Install yasbd-lib
First, grab the library from PyPI:
pip install yasbd-lib (Note: If you already have an older version installed, you can use pip install yasbd-lib -U to upgrade.)
2. Study Real-World Examples & Start from the Template
You don't have to guess how to structure your profile. Grab the official _template.py from the repository, or inspect existing implementations depending on your target language structure, such as:
- en.py: Ideal for seeing how to override dynamic compilation (
_compile_regex_dynamically) and implement custom post-processing logic (post_process_boundaries). - zh.py: Perfect reference for CJK and spacing-free languages.
- th.py: Excellent example for handling complex script layouts.
The base Rules class provides explicit sets you can extend using Python's set union operator (|):
-
TITLE_ABBRVS: Honorifics that should not split sentences. -
REFERENCE_ABBRVS: Citation abbreviations (fig, pág). -
INLINE_ONLY_ABBRVS: Abbreviations that don't end sentences (blvd). -
DATE_ABBRVS: Month and weekday abbreviations. -
DOTTED_GEOPOL_ABBRVS: Geo abbreviations like U.S., E.U.. -
TERMINATORS: Extra sentence-ending punctuation. -
COMMON_SENT_STARTERS: Boundary hints likeThe. -
POST_QUOTATIVE_PARTICLESandREPORTING_WORDS: Used for dialogue attribution.
Create your custom rule file (e.g., yasbd_mylang.py). Notice how post_process_boundaries can be used to clean up or difference out erroneous splits. In Lithuanian, g. is standard shorthand for gatvė (street), making it a useful target for custom abbreviation handling:
from yasbd.rules.base import Rules import re class LtRules(Rules): """Sentence boundary rules for Lithuanian (ISO code inferred as 'lt').""" TITLE_ABBRVS = Rules.TITLE_ABBRVS | {"dr", "prof", "sr"} REFERENCE_ABBRVS = Rules.REFERENCE_ABBRVS | {"pav", "tol"} @classmethod def _compile_regex_dynamically(cls) -> None: super()._compile_regex_dynamically() # Compile custom finders specific to your language rules (e.g. Lithuanian 'g.' for gatvė) cls.STREET_ABBRVS_FINDER = re.compile(r'\b(Av|St|g)\.', re.IGNORECASE) def post_process_boundaries( self, sentence_boundaries: set[int], text: str ) -> None: # Example: if an expression incorrectly triggered a boundary, # you can remove it using difference_update: # sentence_boundaries.difference_update(...) # Or add custom offsets if needed by your text pattern: # sentence_boundaries.update(m.end() for m in self.STREET_ABBRVS_FINDER.finditer(text)) PROFILES = [LtRules] The module-level PROFILES list tells yasbd-lib which rule classes to register when this custom module is imported as a lang pack. A single module can expose multiple classes if you want to bundle variant rule sets.
3. Load via Runtime Registration or External Packs
When you are ready to use your custom language pack, you can pass it via external_lang_packs during initialization in Python:
from yasbd import BoundaryDetector detector = BoundaryDetector( lang="lt", external_lang_packs=["yasbd_mylang"] ) # This sample sentence exercises abbreviations, decimals, quotes, and question marks at once: text = "Dr. Smith atvyko 3.14 val. 'Ar pasiruošęs?' jis klausė." sentences = detector.segment(text) for s in sentences: print(s) Alternatively, you can load external multi-profile packs directly from the command line using the --from-pack flag:
# Install and run with an external auxiliary language pack via CLI pip install yasbd-auxlang yasbd segment --from-pack yasbd_auxlang --lang eo "Saluton. Kiel vi fartas?" Security Note:
load_external_lang_packs()imports arbitrary Python modules by name. Only load lang packs from sources you trust, as an untrusted module can execute arbitrary code at import time.
Wrapping Up
Stop fighting regex lookbehinds and edge cases like URLs, emails, and collapsed OCR lists. Grab _template.py, review the repository codebase examples for your language family, configure your declarative profile sets alongside _compile_regex_dynamically and post_process_boundaries, and let yasbd-lib's customizable engine handle the heavy lifting.

