Build a rule-based sentence splitter that copes with abbreviations, decimals and quotes.
What to build
A program that splits a line of text into numbered sentences.
Requirements
- Line 1 lists abbreviations (such as
Dr.ande.g.), separated by spaces. Line 2 is the text; split it into words on whitespace. - A word ends a sentence when, after removing any closing
",'or)from its end, it ends with.,!or?and is not one of the abbreviations, and either it is the last word or the next word, after removing any opening",'or(, starts with a capital letter. - Words left over at the end form a final sentence even without a closing mark.
- Print each sentence as
N: sentence, numbered from 1, with its words joined by single spaces.
Input
Line 1: abbreviations (at least one). Line 2: the text.
Output
One line per sentence.
Constraints
The text has at most 2,000 characters.
Example
Input:
Dr. Mr.
Dr. Smith arrived at 3.30 today. He was late! Was Mr. Jones there?
Output:
1: Dr. Smith arrived at 3.30 today.
2: He was late!
3: Was Mr. Jones there?
Why it works
Real sentence splitters combine exactly these clues: a closing mark, a following capital letter, and a list of known abbreviations that do not end sentences.
Examples
Dr. Mr. Dr. Smith arrived at 3.30 today. He was late! Was Mr. Jones there?
1: Dr. Smith arrived at 3.30 today. 2: He was late! 3: Was Mr. Jones there?
e.g. Wait... what? I said "Stop." Then I left
1: Wait... what? 2: I said "Stop." 3: Then I left
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Work word by word: add each word to the current sentence, then decide whether that word ends it.
Hint 2
`word.rstrip("\"')")` removes closing quotes and brackets; `words[i + 1].lstrip("\"'(")` removes opening ones from the next word.
Solution
Show one correct solution (try it yourself first)
abbreviations = input().split()
words = input().split()
sentences, current = [], []
for i, word in enumerate(words):
current.append(word)
core = word.rstrip("\"')")
if not core or core[-1] not in ".!?" or core in abbreviations:
continue
if i + 1 < len(words):
following = words[i + 1].lstrip("\"'(")
if not following[:1].isupper():
continue
sentences.append(" ".join(current))
current = []
if current:
sentences.append(" ".join(current))
for n, sentence in enumerate(sentences, 1):
print(f"{n}: {sentence}")