Sentence Boundary Splitter For Plain Text

Split plain text into sentences while keeping common abbreviations, decimals, and closing quotes attached to the right sentence.

PythonData Integrity

Code

pythonsnippet
66 lines
ABBREVIATIONS = frozenset(
    {
        "mr",
        "mrs",
        "ms",
        "dr",
        "prof",
        "sr",
        "jr",
        "vs",
        "etc",
        "e.g",
        "i.e",
        "fig",
        "no",
        "inc",
        "ltd",
    }
)
CLOSERS = "\"')]}"


def _is_abbreviation(text: str, dot_index: int) -> bool:
    window = text[max(0, dot_index - 16) : dot_index]
    word = window.rsplit(" ", 1)[-1].lower().lstrip("(\"'")
    return word in ABBREVIATIONS or (len(word) == 1 and word.isalpha())


def split_sentences(text: str) -> list[str]:
    normalized = " ".join(text.split())
    if not normalized:
        return []
    sentences: list[str] = []
    start = 0
    index = 0
    while index < len(normalized):
        char = normalized[index]
        if char in ".!?":
            cursor = index + 1
            if cursor < len(normalized) and normalized[cursor] in CLOSERS:
                cursor += 1
            if cursor < len(normalized) and normalized[cursor] == " ":
                if char == "." and _is_abbreviation(normalized, index):
                    index += 1
                    continue
                sentences.append(normalized[start:cursor].strip())
                start = cursor + 1
                index = cursor
        index += 1
    tail = normalized[start:].strip()
    if tail:
        sentences.append(tail)
    return [sentence for sentence in sentences if sentence]


def main() -> None:
    paragraph = (
        "Dr. Smith joined Acme Inc. in 2019. Revenue grew 12.5% that year! "
        "Was it sustainable? The board said \"yes.\" Next year told another story."
    )
    for number, sentence in enumerate(split_sentences(paragraph), start=1):
        print(f"{number}. {sentence}")


if __name__ == "__main__":
    main()

Output

Captured from a sandboxed run of this snippet in the build pipeline.

1. Dr. Smith joined Acme Inc. in 2019.
2. Revenue grew 12.5% that year!
3. Was it sustainable?
4. The board said "yes."
5. Next year told another story.

How the snippet works

Sentence Boundary Splitter For Plain Text keeps everything in the functions _is_abbreviation, split_sentences and main.

Repetition is confined to while index < len(normalized) — the rest of the file is straight-line.

Returns a newly built value, leaving the inputs as they were.

Applying it

The primary signature is def split_sentences(text: str) -> list[str]

Treat main as the orchestrator calling split_sentences in a fixed order.

Statements that matter

def main() -> None:
    paragraph = (
        "Dr. Smith joined Acme Inc. in 2019. Revenue grew 12.5% that year! "
        "Was it sustainable? The board said \"yes.\" Next year told another story."
    )
    for number, sentence in enumerate(split_sentences(paragraph), start=1):
        print(f"{number}. {sentence}")
def _is_abbreviation(text: str, dot_index: int) -> bool:
    window = text[max(0, dot_index - 16) : dot_index]
    word = window.rsplit(" ", 1)[-1].lower().lstrip("(\"'")
    return word in ABBREVIATIONS or (len(word) == 1 and word.isalpha())

Interactive Tools for This Topic

FAQ

Does `split_sentences` modify its input?
No. Nothing passed in is written to, which makes the call safe on shared data.
How do you run Sentence Boundary Splitter For Plain Text?
This is a complete program, not a library excerpt — execution starts at the top.