-
Notifications
You must be signed in to change notification settings - Fork 0
/
luis-batch-file-generator.js
242 lines (208 loc) · 8.05 KB
/
luis-batch-file-generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const fs = require('fs');
const intentUtteranceGenerator = require("intent-utterance-generator");
const chalk = require("chalk");
const chalkTable = require("chalk-table/src");
const config = require('./modal.json');
const reportDir = "./batch_files";
const dataSetDir = "./data-sets";
let coveredSynonyms = [];
if (!fs.existsSync(dataSetDir)){
throw new Error("missing data-sets folder, please create one and keep all your files in the folder");
};
function computeEntities(utterance, config) {
let entities = [];
let entity = {};
for(let k = 0; k < config.closedLists.length; k++) {
for(let e = 0; e < config.closedLists[k].subLists.length; e++) {
let searchBy = config.closedLists[k].subLists[e].canonicalForm;
let words = utterance.match(/\b(\w+)\b/g);
let searchByWords = searchBy.match(/\b(\w+)\b/g);
let posOfMatchingWord = utterance.indexOf(searchBy);
if ( posOfMatchingWord > -1 && words.some(w => searchByWords.includes(w))) {
Object.assign(entity, {
"entity": config.closedLists[k].name,
"startPos": posOfMatchingWord,
"endPos": posOfMatchingWord + searchBy.length - 1
});
entities.push(entity);
entity = {};
let item = {
"entity": config.closedLists[k].name,
"word": searchBy
};
if (canInsertEntityWord(item)) {
coveredSynonyms.push(item);
};
}
for(let p = 0; p < config.closedLists[k].subLists[e].list.length; p++) {
let pp = config.closedLists[k].subLists[e].list[p];
let words = utterance.match(/\b(\w+)\b/g);
let searchByWords = pp.match(/\b(\w+)\b/g);
let posOfMatchingWord = utterance.indexOf(pp);
if ( posOfMatchingWord > -1 && words.some(w => searchByWords.includes(w))) {
Object.assign(entity, {
"entity": config.closedLists[k].name,
"startPos": posOfMatchingWord,
"endPos": posOfMatchingWord + pp.length - 1
});
entities.push(entity);
entity = {};
let item = {
"entity": config.closedLists[k].name,
"word": pp
};
if (canInsertEntityWord(item)) {
coveredSynonyms.push(item);
};
}
}
}
}
return entities;
}
function canInsertEntityWord(entity) {
for (let i = 0; i < coveredSynonyms.length; i++) {
if(JSON.stringify(coveredSynonyms[i]) === JSON.stringify(entity)) {
return false;
}
}
return true;
}
function publishEntitiesCoverage(config, path) {
let result = config.closedLists.reduce(function(map, obj) {
map[obj.name] = 0;
return map;
}, {});
let coveredEntities = 0;
let uncoveredEntities = [];
fs.readdirSync(path).forEach(file => {
let input = require(`./${path}/${file}`);
for(let i = 0 ; i < input.length ; i++) {
let entities = input[i].entities;
for(let j = 0 ; j < entities.length ; j++) {
result[entities[j].entity] = 1;
}
}
});
console.log(`${chalk.gray("=====================COVERED ENTITIES====================\n\n")}`);
Object.keys(result).forEach( (key) => {
if(result[key] == 1) {
coveredEntities++;
} else {
let intent = {
"title": key
}
uncoveredEntities.push(intent);
}
});
console.log(`
ENTITIES COVERED: ${chalk.green(coveredEntities)}
ENTITIES NOT COVERED: ${chalk.red(Object.keys(result).length - coveredEntities)}
PERCENTAGE COVERAGE: ${chalk.cyan((coveredEntities / Object.keys(result).length) * 100)} ${chalk.cyan('%')}
`);
if (uncoveredEntities.length > 0) {
console.log(`${chalk.gray("\n\n")}`);
const options = {
leftPad: 2,
columns: [
{ field: "title", name: chalk.red("NOT COVERED ENTITIES") }
]
};
const table = chalkTable(options, uncoveredEntities);
console.log(table);
}
}
function publishCoveredEntityWords() {
console.log(`${chalk.gray("\n\n")}`);
console.log(`${chalk.gray("=====================COVERED SYNONYMS====================\n\n")}`);
const options = {
leftPad: 2,
columns: [
{ field: "entity", name: chalk.red("ENTITY NAME") },
{ field: "word", name: chalk.red("WORD/SYNONYM") }
]
};
coveredSynonyms
.sort((a, b) => (a.entity > b.entity) ? 1 : (a.entity === b.entity) ? ((a.word > b.word) ? 1 : -1) : -1 );
const table = chalkTable(options, coveredSynonyms);
console.log(table);
}
function computeIntentCoverage(config) {
let result = config.intents.reduce(function(map, obj) {
map[obj.name] = 0;
return map;
}, {});
let coveredIntents = 0;
let uncoveredintents = [];
fs.readdirSync(dataSetDir).forEach(file => {
let intent_utterances_input = require(`./${dataSetDir}/${file}`);
let utterances = intentUtteranceGenerator(intent_utterances_input).toString().split("\n");
let filtered = utterances.filter(function (el) {
return el != '';
});
for(let i = 0 ; i < filtered.length ; i++) {
let intent = filtered[i].substr(0, filtered[i].indexOf(" "));
if(intent in result) {
result[intent] = 1;
}
}
});
Object.keys(result).forEach( (key) => {
if(result[key] == 1) {
coveredIntents++;
} else {
let intent = {
"title": key
}
uncoveredintents.push(intent);
}
});
console.log(`
INTENTS COVERED: ${chalk.green(coveredIntents)}
INTENTS NOT COVERED: ${chalk.red(Object.keys(result).length - coveredIntents)}
PERCENTAGE COVERAGE: ${chalk.cyan((coveredIntents / Object.keys(result).length) * 100)} ${chalk.cyan('%')}
`);
console.log(`${chalk.gray("========================NOT COVERED INTENTS=========================\n\n")}`);
const options = {
leftPad: 2,
columns: [
{ field: "title", name: chalk.red("NOT COVERED INTENTS") }
]
};
const table = chalkTable(options, uncoveredintents);
console.log(table);
}
let dt = new Date();
dt = `${
(dt.getMonth()+1).toString().padStart(2, '0')}_${
dt.getDate().toString().padStart(2, '0')}_${
dt.getFullYear().toString().padStart(4, '0')}_${
dt.getHours().toString().padStart(2, '0')}_${
dt.getMinutes().toString().padStart(2, '0')}_${
dt.getSeconds().toString().padStart(2, '0')}`;
fs.readdirSync(dataSetDir).forEach(file => {
let intent_utterances_input = require(`./${dataSetDir}/${file}`);
let output = [];
let utterances = intentUtteranceGenerator(intent_utterances_input).toString().split("\n");
let filtered = utterances.filter(function (el) {
return el != '';
});
if(filtered.length > 1000 ) {
throw new Error("LUIS do not allow more then 1000 utterances in one file");
}
for (let i = 0 ; i < filtered.length; i++) {
let testUtt = {
"text": filtered[i].substr(filtered[i].indexOf(" ") + 1),
"intent": filtered[i].substr(0, filtered[i].indexOf(" ")),
"entities": computeEntities(filtered[i].substr(filtered[i].indexOf(" ") + 1), config)
};
if (!fs.existsSync(`${reportDir}_${dt}`)){
fs.mkdirSync(`${reportDir}_${dt}`);
};
output.push(testUtt);
}
fs.writeFileSync(`./${reportDir}_${dt}/${file}`, JSON.stringify(output, null, 2));
});
computeIntentCoverage(config);
publishEntitiesCoverage(config, `${reportDir}_${dt}`);
publishCoveredEntityWords();