How to Extract Sentences from a String in Node.JS with NLP

Natural Language Processing (NLP) is a powerful tool, but one that can be quite difficult to implement. Today I am here to demonstrate an incredibly easy way to begin using NLP immediately. Our task, to separate a string into complete sentences. To do this, we will simply be using an API that cuts all the hard work right out of the equation.

Install our NLP client with this command:

npm install cloudmersive-nlp-api-client --save

Proceed with calling our function, like so:

var CloudmersiveNlpApiClient = require('cloudmersive-nlp-api-client');var defaultClient = CloudmersiveNlpApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveNlpApiClient.SentencesApi();var input = "input_example"; // String | Input stringvar callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.sentencesPost(input, callback);

Now input a string of text to be separated. I chose an excerpt from the Gettysburg Address and got this result:

{
"Successful": true,
"Sentences": [
"Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.",
"Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure.",
"We are met on a great battle-field of that war.",
"We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live.",
"It is altogether fitting and proper that we should do this."
],
"SentenceCount": 5
}

Easy!

Image for post
Image for post

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