How to Separate all Words in a String in Node.JS with NLP

Having the capability to separate out individual words in a text string can be very useful. It can also be surprisingly annoying to set up what should seem at first glance to be a simple task. What do you say we skirt around the headache and head straight for the results instead? I’m going to show you how to do just that.

We shall begin the process with installation of our NLP API client. We can use the following reference snippet in our package.json file to do this.

"dependencies": {
"cloudmersive-nlp-api-client": "^1.1.2"
}

Our next step is to call our function with this bit of code here:

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.WordsApi();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.wordsGetWordsString(input, callback);

We’re already done with our setup. Now to test it out on a sample input. And here are the results:

{
"Words": [
{
"Word": "The",
"WordIndex": 0,
"StartPosition": 0,
"EndPosition": 3
},
{
"Word": "quick",
"WordIndex": 1,
"StartPosition": 4,
"EndPosition": 9
},
{
"Word": "brown",
"WordIndex": 2,
"StartPosition": 10,
"EndPosition": 15
},
{
"Word": "fox",
"WordIndex": 3,
"StartPosition": 16,
"EndPosition": 19
},
{
"Word": "jumps",
"WordIndex": 4,
"StartPosition": 20,
"EndPosition": 25
},
{
"Word": "over",
"WordIndex": 5,
"StartPosition": 26,
"EndPosition": 30
},
{
"Word": "the",
"WordIndex": 6,
"StartPosition": 31,
"EndPosition": 34
},
{
"Word": "lazy",
"WordIndex": 7,
"StartPosition": 35,
"EndPosition": 39
},
{
"Word": "dog",
"WordIndex": 8,
"StartPosition": 40,
"EndPosition": 43
}
]
}
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