Getting Started With NLP_Compromise

  Programming

Natural Language Processing, often shortened to just NLP, is becoming increasingly popular these days. Fortunately, there are several JavaScript libraries that facilitate NLP. In this tutorial, I’ll be talking about one such library, called NLP_Compromise. It is fast, light-weight, and has a very intuitive API. It also enjoys a lot popularity among JavaScript developers.

NLP_Compromise is available on CDNJS. Therefore, you can start using it immediately by simply adding the following tag to your HTML document:

<script src="https://cdnjs.cloudflare.com/ajax/libs/nlp_compromise/6.5.3/nlp_compromise.min.js">
</script>

To be able to easily access all the various classes and functions NLP_Compromise has, I suggest you get a reference to the nlp_compromise object by adding the following code inside a <script> tag:

<script>
    var nlp = window.nlp_compromise;
</script>

At this point, you can start experimenting with the library.

Conjugating Verbs

You can’t frame a grammatically correct sentence without conjugating your verbs. To conjugate a verb using NLP_Compromise, you must first initialize your verb using the verb() method, and then call its conjugate() method.

After conjugating the verb, you get an object that has all the conjugations of the verb.

Here’s an example:

console.log(
    "The past tense of make is " +
    nlp.verb("make").conjugate().past
);

// "The past tense of make is made"

Singular/Plural

The English language has a lot of irregular plurals. Therefore, converting a noun to its plural or singular form is not a straight-forward task. That’s the reason why NLP_Compromise has a method called all_forms(), which lets you easily determine the various forms of a noun:

console.log(            
    nlp.noun("lady").all_forms()
);

// Object {singular: "lady", plural: "ladies", normal: "lady"}

The same applies to adjectives and adverbs. However, if you are interested in only a specific form, such as, say the superlative form of an adjective, there are intuitively named methods you can use.

console.log(
    nlp.adjective("funny").to_superlative()      
);

// "funniest" 

Working With Sentences

NLP_Compromise lets you handle an entire sentence at once. For instance, you can convert a statement in the present tense to one in the future tense by using the following code:

console.log(
    nlp.statement("Alice is beautiful")
       .to_future().text()            
);

// "Alice will be beautiful"

I am sure you are beginning to realize how powerful this library is. But wait, there’s a lot more to it.

If you want to replace a specific part-of-speech, or POS, of a sentence with another word, you can use the replace() method. For example, here’s how you can replace an adjective with another:

console.log(
    nlp.sentence("Caleb is tall")
       .replace("[Adjective]", "fat")
       .text()
);

// "Caleb is fat"

If your sentence contains multiple words having the same POS, and you want to replace only one of them, you can include additional patterns in your call to the replace() method. Here’s an example:

console.log(
    nlp.sentence("Caleb is tall and handsome")
       .replace("is [Adjective]", "is fat")
       .text()
);

// "Caleb is fat and handsome"

POS Tagging

If you are interested in simply categorizing all the words that are present in a block of text, you can use the text() method. It converts the text into an array of sentences. Each sentence is further broken down into terms. Each term has a tag field, which tells you its part of speech.

var text = nlp.text("Hello! This is an alien. It seems to have tentacles.");
var sentences = text.sentences;

for(var i = 0; i < sentences.length; i++) {
    var terms = sentences[i].terms;
    for(var j=0; j < terms.length; j++) {
        console.log(terms[j].text + " -> " + terms[j].tag);
    }
}

/*
Hello! -> Expression
This -> Determiner
is -> Copula
an -> Determiner
alien. -> Noun
It -> Pronoun
seems -> PresentTense
to -> Preposition
have -> Infinitive
tentacles. -> Noun
*/

Creating Random Sentences

Let me now show you how you can generate random sentences by simply modifying a single sentence.

All the words that are available in the lexicon of NLP_Compromise can be accessed using its lexicon() method. The structure of the object returned by the method looks like this:

{hope: "Infinitive", hoping: "Gerund", hopes: "PresentTense" ...}

As you can see, it contains a huge number of key-value pairs, where the key is a form of a verb, noun, adjective or adverb, and the value is the POS. Therefore, by using the words in the lexicon along with the replace() method, you can generate random sentences.

For example, here’s how you generate ten random sentences by simply using random adjectives:

var sentence = nlp.sentence("Kelly is weird");
var words = Object.keys(nlp.lexicon()); // Get all words

for(var i = 0; i<10; i++) {
    while(true) {
        // Generate a random number
        var randomNumber = Math.floor(Math.random() * words.length);
        
        // Print sentence only if randomly selected word
        // is an adjective
        if(nlp.lexicon()[words[randomNumber]] == "Adjective") {
            console.log(sentence.replace(
                    "[Adjective]",
                    words[randomNumber]
                ).text()
            );
            break;
        }
    }
}

/*
Kelly is adept
Kelly is high
Kelly is en route
Kelly is able
Kelly is eager
Kelly is petite
Kelly is sideable
Kelly is confirmable
Kelly is for keeps
Kelly is undermineable
*/

Of course, not all sentences make sense. But, I hope you get the idea.

Here’s another example that replaces a noun. It also randomizes the tense of the sentence.

for(var i = 0; i<10; i++) {
    var sentence = nlp.sentence("Kelly wants to become a doctor");
    while(true) {
        var randomNumber = Math.floor(Math.random() * words.length);
        var word = words[randomNumber];
        var wordPOS = nlp.lexicon()[word];
        // Print sentence only if randomly selected word
        // is an actor
        if(wordPOS == "Actor") {                    
            sentence.replace(
                "a [Noun]",
                nlp.noun(word).article() + " " + word
            );

            // Randomly change tense
            if(Math.random() > 0.5) {
                if(Math.random() > 0.5)
                    sentence.to_past();
                else
                    sentence.to_future();
            }

            console.log(sentence.text());
            break;
        }
    }
}

/*
Kelly wants to become a reviewer
Kelly will want to become a runner
Kelly wanted to become a giver
Kelly wanted to become a hairdresser
Kelly wanted to become an influencer
Kelly wants to become a swimmer
Kelly wants to become a security guard
Kelly will want to become a programmer
Kelly will want to become a rewarder
Kelly wanted to become a decliner
*/

Conclusion

You now know how to use NLP_Compromise.js. The sentences it generates are usually correct, although you might encounter minor errors in sentences that have future and past tenses.

If you found this article useful, please share it with your friends and colleagues!