David542 David542 - 1 month ago 5
Python Question

How to break up a paragraph by sentences in Python

I need to parse sentences from a paragraph in Python. Is there an existing package to do this, or should I be trying to use regex here?

Answer

The nltk.tokenize module is designed for this and handles edge cases. For example:

>>> from nltk import tokenize
>>> p = "Good morning Dr. Adams. The patient is waiting for you in room number 3."
>>> tokenize.sent_tokenize(p)
['Good morning Dr. Adams.', 'The patient is waiting for you in room number 3.']