Exploring NLP in JavaScript

Amol Kedari
Analytics Vidhya
Published in
3 min readOct 27, 2019

NLP short for Natural Language Processing, is a field of Artificial Intelligence widely used to interpret, understand and process human/natural language. It is commonly used for speech recognition and NLG(Natural Language Generation).

A more common capabilities of NLP are as follows:
1. Content categorization
2. Contextual extraction
3. Sentimental analysis
4. Speech-to-text and text-to-speech
5. Document summarization

Today, in this blog post I am exploring NLP using Javascript. We have lots of NLP libraries available in JS where we have most of the NLP features/task. In this post we will be more concentrated on natural npm library but you can check with other library like compromise, node-npl.
Here, we will see some of its core functionality like tokenization, string operations and Classifier.

First, we have to install and import natural library

Installing and importing library
  1. A term Tokenization means breaking string into array, like
Simple tokenization example

We can achieve this using JS String method but difference is apart from this simple tokenization it also gives us case, regex, sentence and punctuation based tokenization for any language.

2. A String Distance, counts number of different character.

String Distance example

In above example, it simply return number of un-match character. It is ignoring case as we set flag to false, also return -1 if string length is not same.
It also supports for String matching.

3) An Inflector, for singularized or pluralized form of word,

Inflector example

4) Now, we will see most fascinating part, Classifiers, we will train classifier with the sample data and get result from it. (Here, we are just demoing how classifier works in our case rather than its implementation.)

Fig-1 Classifier example
Fig-2 Classifier output

In Fig-1, in simple word, we train classifier with sample data, we provide input and output so in future if classifier get similar context it will return desired output. In above example, we set output buy/sell on some input sample data so after training when we check with different data it outputs buy/sell according to train data.

In this blog post, I more focus on exploring NLP in Javascript to get some context about NLP, its features and implementation.

A natural library gives us more facility than what we have seen so far. Similarly A compromise is also good one, with almost similar facility, you can run compromise on both NodeJS and browser itself.

Thank you.

--

--