liveBook · Manning

Get_sentences

As explained in Learn AI-Assisted Python Programming, Second Edition

get_sentences is a book-specific Python function that produces sentences from text by delegating string-splitting work to another function.

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

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Defines the interface, delimiters, delegation, edge-case behavior, and doctest while deferring robust natural language processing.
Examples
Demonstrates punctuation-separated prose and the false boundary created by the abbreviation “Mrs.”.
What you can do after
Build it from scratch, delegate splitting to split_string, and recognize punctuation-based segmentation errors.
How it connects
Connects sentence segmentation with split_string, average sentence length, authorship identification, and natural language processing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.