Keyword NLU Plugin

Improve natural language understanding (NLU) perfomance by matching common keywords to intents instead of making lengthy NLU API requests

Introduction

This plugin is a lightweight NLU integration that does two things:

  • It maps common keywords (for example words that show up in quick replies that don't necessarily need full fledged NLU) to an intent
  • It saves performance by skipping NLU service calls for common keywords

The plugin uses a keywordMap that may look like this for the locales en (English) and de (German):

{
  en: {
    yes: 'YesIntent',
    no: 'NoIntent',
    'learn more': 'LearnMoreIntent',
  },
  de: {
    ja: 'YesIntent',
    nein: 'NoIntent',
    mehr: 'LearnMoreIntent',
  },
}

If the input contains text that is part of the keywordMap, the Keyword NLU adds the resulting intent to the $input object, which is then used for the routing. Here's example:

// Before Keyword NLU
{
  type: 'TEXT',
  text: 'yes',
}

// After Keyword NLU
{
  type: 'TEXT',
  text: 'yes',
  intent: 'YesIntent',
}

The Keyword NLU plugin hooks into the before.interpretation.nlu RIDR middleware, which means it happens one step before other NLU integrations. If the Keyword NLU is successful, the interpretation.nlu step is skipped, resulting in a faster response, because an external NLU service doesn't need to be called.

You can find the code here: KeywordNluPlugin.

Learn more in the following sections:

Installation

You can install the plugin like this:

$ npm install @jovotech/plugin-keywordnlu

Add it as plugin to your app configuration, e.g. app.ts:

import { App } from '@jovotech/framework';
import { KeywordNluPlugin } from '@jovotech/plugin-keywordnlu';
// ...

const app = new App({
  plugins: [
    new KeywordNluPlugin({
      keywordMap: {
        en: {
          yes: 'YesIntent',
          no: 'NoIntent',
          'learn more': 'LearnMoreIntent',
        },
        de: {
          ja: 'YesIntent',
          nein: 'NoIntent',
          mehr: 'LearnMoreIntent',
        },
        // ...
      },
    }),
    // ...
  ],
});

Learn more about config options in the configuration section.

Configuration

The following configuration can be added to the Keyword NLU plugin:

new KeywordNluPlugin({
  keywordMap: {
    en: {
      yes: 'YesIntent',
      no: 'NoIntent',
      'learn more': 'LearnMoreIntent',
    },
    de: {
      ja: 'YesIntent',
      nein: 'NoIntent',
      mehr: 'LearnMoreIntent',
    },
    // ...
  },
  fallbackLocale: 'en',
}),
  • keywordMap: For each locale (e.g. en, de) it maps a keyword (key) to an intent (value). Text input is transformed to lowercase, so make sure that the keywords are in lowercase as well.
  • fallbackLocale: The locale to be used if the request does not contain one. Default: en.