NLP_Compromise is available on CDNJS. Therefore, you can start using it immediately by simply adding the following tag to your HTML document:
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
At this point, you can start experimenting with the library.
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
After conjugating the verb, you get an object that has all the conjugations of the verb.
Here’s an example:
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:
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.
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:
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:
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:
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.
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:
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:
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.
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.