Skip to content

NLPjs Train

Guido edited this page Jun 9, 2020 · 2 revisions

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, NLP.js Intent and NLP.js 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 NLP.js 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 NLP.js 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

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 NLP.js Intent node looks like

Intent

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

Entity

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

If, for example, the user writes "Turn on lights in the kitchen", the output of the NLP.js Process node will be

{
  payload: {
    type: 'intent,
    intent: 'switch.on',
    isFallback: false,
	  score: 0.7,
    language: 'en',
    variables: {
    room: 'kitchen'
   }
 }
}

The output of the NLP.js Process node can be chained directly to a Context node to store the extracted variables in the chat context.

If not specified, the name of the module will be set to "default", this is useful only if the chatbots needs more than one models in the same flow.

Clone this wiki locally