get_sentences turns a text string into a list of sentences by treating periods, question marks, and exclamation points as separators. It delegates the splitting work to split_string, which also removes surrounding spaces and empty results. section 11.3.1 section 11.5.6
The function follows a small, practical contract:
- Accept a text string as its parameter. section 11.3.1
- Return a list containing the separated sentences. section 11.3.1
- Use split_string rather than implementing splitting logic again. section 11.5.6
- Accept occasional mistakes, such as splitting after “Mrs.”, because punctuation alone does not reliably identify every sentence boundary. section 11.3.1
A compact implementation
def get_sentences(text): return split_string(text, '.?!') get_sentences('A pearl! Pearl! Lustrous pearl! Rare. What a nice find.') # ['A pearl', 'Pearl', 'Lustrous pearl', 'Rare', 'What a nice find'] section 11.5.6
Listing 11.8 Return list of sentences in text
def get_sentences(text):
'''
text is a string of text.
Return a list of the sentences from text.
Sentences are separated by a '.', '?' or '!'.
>>> get_sentences('A pearl! Pearl! Lustrous pearl! Rare. \
What a nice find.')
['A pearl', 'Pearl', 'Lustrous pearl', 'Rare', \
'What a nice find']
'''
return split_string(text, '.?!') #1
View the complete function listing, including its docstring and example output. section 11.5.6
