UNPKG

asksuite-core

Version:
147 lines (117 loc) 3.96 kB
const KeywordUtil = require('./keyword-util'); const intents = require('./intents'); const PromiseBlueBird = require('bluebird'); module.exports = function(config) { const apiai = require('./apiai')(config.API_AI_DEVELOPER); const KeywordMatcher = {}; function isDialogFlow(operation) { return operation.entityType == 'dialogFlow'; } function isSystemOperation(operation) { return operation.entityType == 'system'; } function isDate(operation) { return operation.entity == 'date'; } function isNumber(operation) { return operation.entity == 'number'; } KeywordMatcher.refreshCache = function() { return new Promise((resolve, reject) => { const cacheForSearch = {}; apiai .getEntities() .then(async () => { PromiseBlueBird.map( async item => { return await apiai.getKeywordsFromEntity(item); }, { concurrency: 1 }, ) .then(data => { data.forEach(item => { if (item != null) { cacheForSearch[item.key.name] = item.data; } }); resolve(cacheForSearch); }) .catch(error => { reject(error); }); }) .catch(error => { reject(error); }); }); }; function evaluate(string, expression, cache) { string = string.toLowerCase(); const isNegative = expression.operation.indexOf('not_') >= 0; let value = false; if (expression.operation.indexOf('contains') >= 0) { if (isDialogFlow(expression)) { // console.log("key"+expression.entity) value = KeywordUtil.hasWordInString(string, cache[expression.entity]); // console.log(string, cache[expression.entity]) // console.log(cache[expression.entity]); // console.log("contem key"+expression.entity, value); } else if (isSystemOperation(expression)) { if (isDate(expression)) { value = KeywordUtil.hasDate(string); // console.log("contem data", value); } else if (isNumber(expression)) { value = KeywordUtil.hasNumber(string); } } } return isNegative ? !value : value; } function execute(str, intent, cache) { return new Promise(resolve => { const result = intent.expressions.map(item => { return evaluate(str, item, cache); }); if (result.indexOf(false) >= 0) { resolve({ match: false, item: intent }); } else { resolve({ match: true, item: intent }); } }); } async function evaluateExpressions(string, expressions, cache) { const promises = []; expressions.forEach(item => { promises.push(execute(string, item, cache)); }); const data = await Promise.all(promises); return data.filter(item => { return item.match; }); } KeywordMatcher.getIntent = async function(string, expressions, globalCache) { const newStr = KeywordUtil.removeSpecial(string); console.log('vou testar a string: ' + newStr); const retruno = await evaluateExpressions(newStr, expressions, globalCache); return retruno; }; KeywordMatcher.matchBase = async function(expressions, globalCache) { return new Promise(resolve => { const listResult = []; const listClassify = intents.filter(item => { return item.intentResult === 'naoentendi' || item.intentResult === 'naoentendi_grande'; }); listClassify.forEach(async (item, index) => { const match = await KeywordMatcher.getIntent(item.textRequest, expressions, globalCache); if (match && match.length > 0) { listResult.push(item.textRequest); } if (index == listClassify.length - 1) { resolve(listResult); } }); return listResult; }); }; return KeywordMatcher; };