UNPKG

3.96 kBJavaScriptView Raw
1const KeywordUtil = require('./keyword-util');
2const intents = require('./intents');
3const PromiseBlueBird = require('bluebird');
4
5module.exports = function(config) {
6 const apiai = require('./apiai')(config.API_AI_DEVELOPER);
7
8 const KeywordMatcher = {};
9
10 function isDialogFlow(operation) {
11 return operation.entityType == 'dialogFlow';
12 }
13
14 function isSystemOperation(operation) {
15 return operation.entityType == 'system';
16 }
17
18 function isDate(operation) {
19 return operation.entity == 'date';
20 }
21
22 function isNumber(operation) {
23 return operation.entity == 'number';
24 }
25
26 KeywordMatcher.refreshCache = function() {
27 return new Promise((resolve, reject) => {
28 const cacheForSearch = {};
29
30 apiai
31 .getEntities()
32 .then(async () => {
33 PromiseBlueBird.map(
34 async item => {
35 return await apiai.getKeywordsFromEntity(item);
36 },
37 { concurrency: 1 },
38 )
39 .then(data => {
40 data.forEach(item => {
41 if (item != null) {
42 cacheForSearch[item.key.name] = item.data;
43 }
44 });
45
46 resolve(cacheForSearch);
47 })
48 .catch(error => {
49 reject(error);
50 });
51 })
52 .catch(error => {
53 reject(error);
54 });
55 });
56 };
57
58 function evaluate(string, expression, cache) {
59 string = string.toLowerCase();
60 const isNegative = expression.operation.indexOf('not_') >= 0;
61 let value = false;
62
63 if (expression.operation.indexOf('contains') >= 0) {
64 if (isDialogFlow(expression)) {
65 // console.log("key"+expression.entity)
66 value = KeywordUtil.hasWordInString(string, cache[expression.entity]);
67
68 // console.log(string, cache[expression.entity])
69 // console.log(cache[expression.entity]);
70 // console.log("contem key"+expression.entity, value);
71 } else if (isSystemOperation(expression)) {
72 if (isDate(expression)) {
73 value = KeywordUtil.hasDate(string);
74 // console.log("contem data", value);
75 } else if (isNumber(expression)) {
76 value = KeywordUtil.hasNumber(string);
77 }
78 }
79 }
80
81 return isNegative ? !value : value;
82 }
83
84 function execute(str, intent, cache) {
85 return new Promise(resolve => {
86 const result = intent.expressions.map(item => {
87 return evaluate(str, item, cache);
88 });
89
90 if (result.indexOf(false) >= 0) {
91 resolve({ match: false, item: intent });
92 } else {
93 resolve({ match: true, item: intent });
94 }
95 });
96 }
97
98 async function evaluateExpressions(string, expressions, cache) {
99 const promises = [];
100
101 expressions.forEach(item => {
102 promises.push(execute(string, item, cache));
103 });
104
105 const data = await Promise.all(promises);
106
107 return data.filter(item => {
108 return item.match;
109 });
110 }
111
112 KeywordMatcher.getIntent = async function(string, expressions, globalCache) {
113 const newStr = KeywordUtil.removeSpecial(string);
114 console.log('vou testar a string: ' + newStr);
115
116 const retruno = await evaluateExpressions(newStr, expressions, globalCache);
117
118 return retruno;
119 };
120
121 KeywordMatcher.matchBase = async function(expressions, globalCache) {
122 return new Promise(resolve => {
123 const listResult = [];
124
125 const listClassify = intents.filter(item => {
126 return item.intentResult === 'naoentendi' || item.intentResult === 'naoentendi_grande';
127 });
128
129 listClassify.forEach(async (item, index) => {
130 const match = await KeywordMatcher.getIntent(item.textRequest, expressions, globalCache);
131
132 if (match && match.length > 0) {
133 listResult.push(item.textRequest);
134 }
135
136 if (index == listClassify.length - 1) {
137 resolve(listResult);
138 }
139 });
140
141 return listResult;
142 });
143 };
144
145 return KeywordMatcher;
146};