UNPKG

asksuite-core

Version:
67 lines (55 loc) 2 kB
const fallbackIntent = require('./fallbackIntent'); const Util = require('../util'); class IntentResolver { constructor(intents, companyId, text) { this.intents = intents; this.companyId = companyId; this.text = text ? text : ''; } findDialogByIntent(intent) { let intentBot = this.intents.find(this.isTheSame(intent)) || this.intents.find(this.isIntentForIDidntUnderstand); if (!intentBot || this.isIntentForIDidntUnderstand(intentBot)) { let msg = Util.replaceIfIsPhrase(this.text); msg = !msg ? this.text.toLowerCase() : msg; const notUnderstandable = (msg.length <= 2 && !Util.isNumeric(msg)) || Util.existsletterCount(msg, 3); if (notUnderstandable) { intentBot = this.intents.find(this.isIntentForNotUnderstandable); } else if (!intentBot || !intentBot.dialog) { // Fallback intentBot = {}; intentBot.intent = fallbackIntent.normal.intent; intentBot.dialog = this.companyId + fallbackIntent.normal.dialogSuffix; } } return intentBot; } isTheSame(intent) { return item => { return item && intent && item.intent === intent; }; } isIntentForIDidntUnderstand(item) { return ( item && [fallbackIntent.normal.intent, fallbackIntent.grande.intent].includes(item.intent) ); } isIntentForNotUnderstandable(item) { return item && item.intent === fallbackIntent.naoentendivel.intent; } static queryTextMaxLengthExceededDialog(companyId) { const intent = {}; intent.intent = fallbackIntent.grande.intent; intent.dialog = companyId + fallbackIntent.grande.dialogSuffix; return intent; } static textNotDefinedDialog(companyId) { const intent = {}; intent.intent = fallbackIntent.normal.intent; intent.dialog = companyId + fallbackIntent.normal.dialogSuffix; return intent; } } module.exports = IntentResolver;