The NLP.js nodes uses NLP.js for Natural Language Processing.

In order to work it requires a properly trained model using NLP.js Train, NLPjs Intent node and NLPjs Entity . An intent is a string representation of the abstract meaning of the user sentence, it requires an arbitrary name (that will be likely used in a Rules node to control the chatbot flow) and one or more utterances (a set of all different way of expressing the same actions).

An entity is the variable part of the utterance, it can be detected and stored in a context variable and used later in the flow. Some entities are predefined (like numbers, dates, etc) while other can be defined, for example a chatbot that is able to understand a sentence like I want to buy 3 apples should have an intent with the utterance I want to by %number% %fruit%. In this example the entity number is built-it, while the entity fruit should be defined with the NLPjs Entity and should include all kind of fruits with optional aliases (apple, orange, grape, etc).

All this intents and entities nodes must be chained to a NLP.js Train node which creates a precompiled model used to detect the sentences, it’s generally stored in a JSON file using the NLPjs Save and loaded when the flow is started (the train process could be long and can be avoided if the intents and entities are not changed).

The typical use in a flow is

Train model

Train model

The scenario is a chatbot able to understand sentences like “switch on the lights in the kitchen”, “turn on lights in dining room”. The first Inject node is used to train the NLP.js node and store locally the trained model every time an intent or entity is changed. The second Inject node runs automatically at startup and loads the trained model.

The NLPjs Intent node looks like

https://raw.githubusercontent.com/guidone/node-red-contrib-chatbot/master/docs/images/nlp-intent.png

Where the %room% placeholder is an entity defined in the NLP.js Entity node, if there’s a match the NLPjs Process will return the intent (in this case switch.on) and the extract variables for each placeholder (like %room%).

https://raw.githubusercontent.com/guidone/node-red-contrib-chatbot/master/docs/images/nlp-entity.png

When the model is trained and loaded into the flow, NLP.js Process can be used to analyse the sentences from the users

Train model

Train model