• Engineering
    • Sentence splitting
    • Open source

    How ParaWrite splits sentences: abbreviations, quotes and ellipses

    A look inside ParaWrite's sentence splitter: the rules that keep Dr., et al., U.S.A., decimals, file names and quoted dialogue in one piece.

    Udara Nalawansa

    · 3 min read

    A paragraph with scissor cut marks between sentences and abbreviations highlighted

    Splitting text into sentences sounds trivial: cut after every full stop, question mark and exclamation mark. The first version of ParaWrite did exactly that, and it broke on the first academic paper I pasted in. "Dr. Smith et al. published…" became three "sentences". This post explains how the splitter works now. It's open source, so you can read the code in src/utils/textProcessing.ts on GitHub.

    How does the splitter decide where a sentence ends?

    It scans the text left to right. Every ., ! or ? is a candidate boundary. It then runs a set of checks, and a full stop only ends a sentence if none of them object. Question marks and exclamation marks always end a sentence.

    Before checking, it groups punctuation that belongs together. ?! and ... count as one ending, and closing quotes or brackets right after the punctuation stay with the sentence. That is how She asked, 'Are you ready?' stays whole.

    Which full stops are not sentence endings?

    RuleExample kept together
    Decimals3.5 days, p < 0.05
    Multi-dot abbreviationse.g., i.e., et al.
    AcronymsU.S.A., D.C.
    A person's initial before a capitalised wordJohn A. Smith
    Known abbreviationsDr., Prof., Fig., approx., v.
    A full stop glued to the next characterNode.js, package.json, v18.2.0
    Dotted abbreviations before a lowercase wordthe Ph.D. thesis, 5 p.m. on Monday
    Email-like textname@example.com

    Why do some abbreviations sometimes end a sentence?

    Company suffixes are ambiguous. In "Smith v. Jones, Inc. was decided in 2023" the sentence continues after "Inc.". In "The court ruled in favor of XYZ Corp. Section 12.5.3 applies" it ends there. The splitter looks at the next word. Before a lowercase word, "Inc.", "Corp.", "Ltd." and "Co." stay mid-sentence. Before a capitalised word, they end it.

    The same idea applies to dotted abbreviations like "Ph.D." and "p.m.". "She earned her Ph.D. She then left" is two sentences, while "the Ph.D. thesis" is one.

    Why does "98.6°F." end a sentence but "John A." doesn't?

    Single capital letters are treated as initials only when they stand alone. "John A. Smith" keeps the initial, but "98.6°F. Dr. Johnson…" ends after "°F.", because "F" there is a unit, not a name.

    What are the modes?

    The splitter has three modes in code: strict, balanced (the default) and loose. They differ mainly in the minimum sentence length. A fragment shorter than the minimum is never thrown away. It's joined to the next sentence, so no text is lost.

    How is it tested?

    Every rule above has a regression test in src/utils/testFixtures.ts, including the examples on the use cases page. You can run them with npm run test:splitter. When a real document splits in the wrong place, the fix starts with a new test case.

    Where does it still get it wrong?

    Some cases are ambiguous even for people. "Apple Inc. Chief Executive…" looks exactly like a sentence ending after "Inc." followed by a new sentence starting with a capital, so the splitter cuts there. Uncommon abbreviations that aren't in the list can also cause an early split. If you see one, fix the text before you start editing, and please open an issue with the sentence so it can become a test.

    See the splitter in action on the use cases post, or paste your own text into the editor.