Skip to content

Commit

Permalink
🔀 Merge pull request #1413 from jovotech/v4/dev
Browse files Browse the repository at this point in the history
🔖 Prepare latest release
  • Loading branch information
jankoenig authored Sep 1, 2022
2 parents 09a4cef + 24ba755 commit c147a99
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 18 deletions.
54 changes: 41 additions & 13 deletions integrations/plugin-keywordnlu/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ This plugin is a lightweight [NLU integration](https://www.jovo.tech/docs/nlu) t
- It maps common keywords (for example words that show up in [quick replies](https://www.jovo.tech/docs/output-templates#quickreplies) 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:
The plugin uses a `keywordMap` that may look like this for the locales `en` (English) and `de` (German):

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

Expand All @@ -43,6 +50,8 @@ If the input contains `text` that is part of the `keywordMap`, the Keyword NLU a

The Keyword NLU plugin hooks into the `before.interpretation.nlu` [RIDR middleware](https://www.jovo.tech/docs/middlewares#ridr-middlewares), which means it happens one step before other [NLU integrations](https://www.jovo.tech/docs/nlu). If the Keyword NLU is successful, the `interpretation.nlu` step is [skipped](https://www.jovo.tech/docs/middlewares#skip-middlewares), resulting in a faster response, because an external NLU service doesn't need to be called.

You can find the code here: [`KeywordNluPlugin`](https://github.com/jovotech/jovo-framework/blob/v4/latest/integrations/plugin-keywordnlu/src/KeywordNluPlugin.ts).

Learn more in the following sections:
- [Installation](#installation)
- [Configuration](#configuration)
Expand All @@ -66,32 +75,51 @@ const app = new App({
plugins: [
new KeywordNluPlugin({
keywordMap: {
yes: 'YesIntent',
no: 'NoIntent',
'learn more': 'LearnMoreIntent',
en: {
yes: 'YesIntent',
no: 'NoIntent',
'learn more': 'LearnMoreIntent',
},
de: {
ja: 'YesIntent',
nein: 'NoIntent',
mehr: 'LearnMoreIntent',
},
// ...
},
}),
// ...
],
});
```

Learn more about configurations in the [configuration](#configuration) section.
Learn more about config options in the [configuration](#configuration) section.


## Configuration

Currently, the only configuration option for the Keyword NLU plugin is the required `keywordMap` that maps a keyword (key) to an intent (value), for example:
The following configuration can be added to the Keyword NLU plugin:

```typescript
new KeywordNluPlugin({
keywordMap: {
yes: 'YesIntent',
no: 'NoIntent',
'learn more': 'LearnMoreIntent',
en: {
yes: 'YesIntent',
no: 'NoIntent',
'learn more': 'LearnMoreIntent',
},
de: {
ja: 'YesIntent',
nein: 'NoIntent',
mehr: 'LearnMoreIntent',
},
// ...
},
fallbackLocale: 'en',
}),
```

Text input is transformed to lowercase, so make sure that the keywords are in lowercase as well.
- `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`.


33 changes: 28 additions & 5 deletions integrations/plugin-keywordnlu/src/KeywordNluPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,33 @@ import {
PluginConfig,
} from '@jovotech/framework';


/* Example
{
start: 'StartIntent',
'learn more': 'LearnMoreIntent',
en: {
start: 'StartIntent',
'learn more': 'LearnMoreIntent',
},
de: {
start: 'StartIntent',
mehr: 'LearnMoreIntent',
}
}
*/
export interface KeywordMap {
[locale: string]: Record<string,string>;
}

export interface KeywordNluPluginConfig extends PluginConfig {
keywordMap: Record<string, string>;
keywordMap: KeywordMap;
fallbackLocale: string;
}

export class KeywordNluPlugin extends Plugin<KeywordNluPluginConfig> {
getDefaultConfig(): KeywordNluPluginConfig {
return {
keywordMap: {},
fallbackLocale: 'en',
};
}

Expand All @@ -33,13 +46,23 @@ export class KeywordNluPlugin extends Plugin<KeywordNluPluginConfig> {
mount(parent: Extensible): Promise<void> | void {
parent.middlewareCollection.use('before.interpretation.nlu', (jovo: Jovo) => {
const text = jovo.$input.getText()?.toLowerCase();
const locale = this.getLocale(jovo);

if (text && this.config.keywordMap.hasOwnProperty(text)) {
jovo.$input.intent = this.config.keywordMap[text];
if (text && this.config.keywordMap[locale]?.hasOwnProperty(text)) {
jovo.$input.intent = this.config.keywordMap[locale][text];

// If a keyword matches, skip other NLU integrations for better performance
jovo.$handleRequest.skipMiddlewares('interpretation.nlu');
}
});
}

protected getLocale(jovo: Jovo): string {
const locale = jovo.$request.getLocale() || this.config.fallbackLocale;

// Only use generic locales like 'en' instead of e.g. 'en-US'
const genericLocale = locale.split('-')[0];

return genericLocale;
}
}

0 comments on commit c147a99

Please sign in to comment.