Why Do Typical Online Sentence Case Converters Break Text?
Naive converters run text.toLowerCase().split("."), which inserts spaces inside decimals ($9. 99), capitalizes domain extensions (acme. Com), strips trailing periods, and breaks abbreviations.
Most online text tools implement sentence casing using a naive algorithm: they split text whenever they encounter a period (.) and then capitalize the next word. This breaks text in five critical ways:
- Decimals are split:
version 1.5 costs $9.99becomesVersion 1. 5 costs $9. 99. - URLs are corrupted:
visit acme.com todaybecomesVisit acme. Com today. - Trailing periods are lost: The final sentence loses its ending punctuation.
- Ellipses are destroyed:
wait... what happened?becomesWait. What happened?. - Abbreviations break:
u.s.a.becomesU. S. A..
How Does Text Cleaner's Lookahead Sentence Case Algorithm Work?
The parser scans characters in place (`Array.from(text.toLowerCase())`), raising the `capitaliseNext` flag only when a single terminator (`.`, `!`, `?`) is followed by whitespace (`\s`) and is not preceded by a single-letter abbreviation (`/\.[a-z]\./`).
Text Cleaner's sentence casing engine operates in place without string splitting. It inspects character boundaries using lookahead rules:
- A period only triggers sentence capitalization if followed by whitespace or the end of the text.
- Decimals (numbers adjacent to periods) and web domains are skipped.
- Runs of consecutive terminators (
...,?!) are recognized as single punctuation clusters. - Dotted single-letter abbreviations (such as
u.s.a.) are detected and preserved. - Worked Before / After Example: Input
THIS IS ALL CAPS. visit ACME.COM OR pay $9.99 for version 1.5! right? U.S.A. TODAY.(83 characters, 15 words, 4 sentences) → OutputThis is all caps. Visit acme.com or pay $9.99 for version 1.5! Right? U.s.a. today.(83 characters, 15 words;acme.com,$9.99,1.5, andU.s.a.preserved intact).
| Edge-Case Pattern & Lookahead Rule | Input Example (Before) | Naive Split Output (Corrupted) | Text Cleaner Output (After) |
|---|---|---|---|
Decimals & Prices (\d+\.\d+ no whitespace after dot) |
PAY $9.99 FOR V1.5. (19 chars) |
Pay $9. 99 for v1. 5 (broken) |
Pay $9.99 for v1.5. (19 chars intact) |
Web Domains & URLs ([a-z]+\.[a-z]+) |
VISIT ACME.COM TODAY. (21 chars) |
Visit acme. Com today (broken) |
Visit acme.com today. (21 chars intact) |
Ellipsis Cluster (\.{2,} runEnd > i) |
WAIT... REALLY? YES! (20 chars) |
Wait. Really? Yes! (dots lost) |
Wait... really? Yes! (20 chars intact) |
Dotted Abbreviations (endsAbbreviation check) |
MADE IN U.S.A. TODAY. (21 chars) |
Made in u. S. A. Today. (broken) |
Made in u.s.a. today. (21 chars intact) |