Natural Language Understanding


View on Github


Objective

You can go a long way capturing user inputs using regular expressions, but it obviously has its limitations. As you find yourself adding more and more functionality to your bot, you get to a point where you need Natural Language Understanding (NLU) capabilities.

NLU lets you capture user inputs by "intent" instead of parsing its raw text. An intent represents all the different ways users can express a unit of meaning that is valid for your bot. For example, you can group the sentences "What's the weather in California?" and "Do you know if it's sunny today in California?" to the intent GetWeather and the parameter city=California. You can then map that intent to an action using a route.

Botonic has its own NLU module which covers intent and entity recognition tasks.

Example

1. Install the Botonic NLU Plugin

If you are using the nlu template, you should already have Botonic NLU set up. If not, you can use the following command from within your bot's project to install it:

npm install @botonic/plugin-nlu

Note: Windows users should first use the command: npm install --global --production windows-build-tools --vs2015

Followed by: npm install @botonic/plugin-nlu

2. Add Utterances and Intents

A user can express an intent in different ways. For example the Utterances "Hello", "Hi", and "Good morning" are all examples of a Greeting intent. To create an intent, simply add a new text file under src/nlu/utterances/en/, for example src/nlu/utterances/en/Greetings.txt. Within the Greetings.txt file, you can add possible ways a user may greet the bot. Then you can reference the intent 'Greetings' by adding it in routes:

3. Add Routes for Intents

You can add routes that capture different intents and their corresponding actions. For example, in your routes.js file:

import Start from './actions/start'
import NotFound from './actions/not-found'
export const routes = [
{ input: i => i.confidence < 0.7, action: NotFound },
{ intent: 'Greetings', action: Start },
]
  • { input: i => i.confidence < 0.7, action: NotFound } i.confidence references the confidence value of the input. The confidence value is between 0 and 1 and indicates the likelihood that an input has a certain intent. This route is used if the input doesn't match an intent with enough confidence.
  • { intent: 'Greetings', action: Start } will trigger the action Start when the user inputs a greeting.

Note: Routes are checked in order. To function correctly, you'll want to put the more specific ones first and the more generic at the end.

Next, you must create a couple of actions that respond to these intents in src/actions.

4. Run botonic train

Once you've added utterances to your intents, run botonic train in your command line. This will train your bot with the utterances in your directory.

5. Run botonic serve

You can run botonic serve to test your bot. Experiment with adding more routes/actions for different intents.

When you're ready, you must deploy your bot in production and publish it to messaging platforms like Facebook Messenger.

The fallback content to display on prerendering