1 | const fallbackIntent = require('./fallbackIntent');
|
2 | const Util = require('../util');
|
3 |
|
4 | class IntentResolver {
|
5 | constructor(intents, companyId, text) {
|
6 | this.intents = intents;
|
7 | this.companyId = companyId;
|
8 | this.text = text ? text : '';
|
9 | }
|
10 |
|
11 | findDialogByIntent(intent) {
|
12 | let intentBot =
|
13 | this.intents.find(this.isTheSame(intent)) ||
|
14 | this.intents.find(this.isIntentForIDidntUnderstand);
|
15 |
|
16 | if (!intentBot || this.isIntentForIDidntUnderstand(intentBot)) {
|
17 | let msg = Util.replaceIfIsPhrase(this.text);
|
18 | msg = !msg ? this.text.toLowerCase() : msg;
|
19 |
|
20 | const notUnderstandable =
|
21 | (msg.length <= 2 && !Util.isNumeric(msg)) || Util.existsletterCount(msg, 3);
|
22 | if (notUnderstandable) {
|
23 | intentBot = this.intents.find(this.isIntentForNotUnderstandable);
|
24 | } else if (!intentBot || !intentBot.dialog) {
|
25 |
|
26 | intentBot = {};
|
27 | intentBot.intent = fallbackIntent.normal.intent;
|
28 | intentBot.dialog = this.companyId + fallbackIntent.normal.dialogSuffix;
|
29 | }
|
30 | }
|
31 |
|
32 | return intentBot;
|
33 | }
|
34 |
|
35 | isTheSame(intent) {
|
36 | return item => {
|
37 | return item && intent && item.intent === intent;
|
38 | };
|
39 | }
|
40 |
|
41 | isIntentForIDidntUnderstand(item) {
|
42 | return (
|
43 | item && [fallbackIntent.normal.intent, fallbackIntent.grande.intent].includes(item.intent)
|
44 | );
|
45 | }
|
46 |
|
47 | isIntentForNotUnderstandable(item) {
|
48 | return item && item.intent === fallbackIntent.naoentendivel.intent;
|
49 | }
|
50 |
|
51 | static queryTextMaxLengthExceededDialog(companyId) {
|
52 | const intent = {};
|
53 | intent.intent = fallbackIntent.grande.intent;
|
54 | intent.dialog = companyId + fallbackIntent.grande.dialogSuffix;
|
55 | return intent;
|
56 | }
|
57 |
|
58 | static textNotDefinedDialog(companyId) {
|
59 | const intent = {};
|
60 | intent.intent = fallbackIntent.normal.intent;
|
61 | intent.dialog = companyId + fallbackIntent.normal.dialogSuffix;
|
62 | return intent;
|
63 | }
|
64 | }
|
65 |
|
66 | module.exports = IntentResolver;
|