How to Extract Sentences from a String in Python with NLP

NLP can do a lot of powerful things these days, and today we are going to apply it to separating any string into its component sentences. Before you start worrying about how long this is going to take, let me assure you: this is going to be incredibly simple. Allow me to show you.

Install the API client we are using with this command:

pip install git+

Then our function, segmentation_get_sentences, must be called, which you can set up as follows.

from __future__ import print_functionimport timeimport cloudmersive_nlp_api_clientfrom import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_nlp_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_nlp_api_client.SegmentationApi(cloudmersive_nlp_api_client.ApiClient(configuration))input = cloudmersive_nlp_api_client.SentenceSegmentationRequest() # SentenceSegmentationRequest | Input stringtry:# Extract sentences from stringapi_response = api_instance.segmentation_get_sentences(input)pprint(api_response)except ApiException as e:print("Exception when calling SegmentationApi->segmentation_get_sentences: %s\n" % e)

Now let’s input a long string with multiple sentences and let the API work its magic. Our return will include an array containing the constituent sentences.

Image for post

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store