| 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661 |
30x
30x
30x
30x
30x
30x
30x
30x
30x
30x
137x
137x
137x
137x
137x
137x
137x
137x
137x
54x
137x
136x
137x
136x
137x
137x
137x
302x
137x
133x
133x
159x
159x
145x
159x
156x
303x
285x
18x
18x
210x
14x
14x
12x
2x
14x
11x
1x
111x
301x
1x
1x
1x
3x
3x
3x
2x
3x
1x
272x
272x
2x
272x
1x
271x
271x
1x
270x
270x
104x
270x
270x
4x
4x
2x
4x
1x
3x
3x
1x
2x
323x
1x
26x
2x
24x
26x
39x
39x
39x
38x
619x
619x
619x
6x
6x
619x
619x
619x
1x
618x
307x
307x
307x
1x
1x
307x
307x
305x
305x
18x
287x
36x
100x
29x
7x
3x
3x
3x
1x
1x
3x
1x
3x
3x
3x
305x
305x
305x
305x
287x
287x
287x
305x
305x
305x
305x
305x
305x
305x
305x
287x
292x
292x
16x
16x
16x
287x
287x
292x
292x
16x
3x
3x
287x
18x
18x
18x
9x
9x
3x
305x
277x
277x
277x
28x
28x
28x
305x
305x
305x
475x
305x
305x
305x
18x
18x
305x
9x
20x
9x
9x
305x
7x
7x
7x
7x
7x
11x
11x
11x
11x
11x
11x
11x
11x
11x
11x
11x
11x
11x
11x
11x
2x
2x
2x
5x
5x
11x
11x
11x
11x
518x
518x
2914x
2914x
683179x
2914x
11x
11x
11x
11x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
6x
6x
6x
6x
6x
6x
6x
6x
6x
6x
6x
6x
6x
6x
6x
17x
6x
6x
17x
21x
235x
235x
6x
6x
6x
6x
6x
4x
3x
3x
10x
10x
10x
7x
7x
7x
7x
30x
| /*
* Copyright (c) AXA Shared Services Spain S.A.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const fs = require('fs');
const Handlebars = require('handlebars');
const { Language } = require('../language');
const { NerManager } = require('../ner');
const { SentimentManager } = require('../sentiment');
const NlpUtil = require('./nlp-util');
const NlpClassifier = require('./nlp-classifier');
const NlgManager = require('../nlg/nlg-manager');
const NlpExcelReader = require('./nlp-excel-reader');
const { SlotManager } = require('../slot');
/**
* Class for the NLP Manager.
* The NLP manager is the one that is able to manage several classifiers,
* to have multilanguage, and also is the responsible of the NER (Named Entity
* Recognition).
*
* Understanding NER:
*
* You can have several entities defined, each one with multilanguage and
* several texts for each option. Example
* Entity Option English Spanish
* FOOD Burguer Burguer, Hamburguer Hamburguesa
* FOOD Salad Salad Ensalada
* FOOD Pizza Pizza Pizza
*
*/
class NlpManager {
/**
* Constructor of the class.
* @param {Object} settings Settings for the NLP Manager.
*/
constructor(settings) {
this.settings = settings || {};
this.guesser = new Language();
this.nerManager = new NerManager(this.settings.ner);
this.sentiment = new SentimentManager();
this.languages = [];
this.classifiers = {};
this.slotManager = new SlotManager();
this.intentDomains = {};
if (this.settings.languages) {
this.addLanguage(this.settings.languages);
}
if (this.settings.fullSearchWhenGuessed === undefined) {
this.settings.fullSearchWhenGuessed = true;
}
if (this.settings.useNlg === undefined) {
this.settings.useNlg = true;
}
Eif (this.settings.useNeural === undefined) {
this.settings.useNeural = true;
}
this.processTransformer =
typeof this.settings.processTransformer === 'function'
? this.settings.processTransformer
: _ => _;
this.nlgManager = new NlgManager();
}
/**
* Adds a language or several languages to the NLP Manager.
* @param {String[]} srcLocales Locales to be added.
*/
addLanguage(srcLocales) {
const locales = Array.isArray(srcLocales) ? srcLocales : [srcLocales];
locales.forEach(locale => {
const truncated = NlpUtil.getTruncatedLocale(locale);
if (!this.languages.includes(truncated)) {
this.languages.push(truncated);
}
if (!this.classifiers[truncated]) {
this.classifiers[truncated] = new NlpClassifier({
language: truncated,
useNeural: this.settings.useNeural,
});
}
});
}
/**
* Given a text, try to guess the language, over the languages used for the NLP.
* @param {String} utterance Text to be guessed.
* @returns {String} ISO2 locale of the language, or undefined if not found.
*/
guessLanguage(utterance) {
if (this.languages.length === 1) {
return this.languages[0];
}
const guess = this.guesser.guess(utterance, this.languages, 1);
return guess && guess.length > 0 ? guess[0].alpha2 : undefined;
}
/**
* Add new texts for an option of an entity for the given languages.
* @param {String} entityName Name of the entity.
* @param {String} optionName Name of the option.
* @param {String[]} languages Languages for adding the texts.
* @param {String[]} texts Texts to be added.
*/
addNamedEntityText(entityName, optionName, languages, texts) {
return this.nerManager.addNamedEntityText(
entityName,
optionName,
languages,
texts
);
}
/**
* Adds a new regex named entity
* @param {String} entityName Name of the entity.
* @param {RegEx} regex Regular expression
*/
addRegexEntity(entityName, languages, regex) {
const entity = this.nerManager.addNamedEntity(entityName, 'regex');
if (typeof regex === 'string') {
entity.addStrRegex(languages, regex);
} else {
entity.addRegex(languages, regex);
}
return entity;
}
/**
* Adds a new trim named entity.
* @param {String} entityName Name of the entity.
* @returns {Object} New Trim Named Entity instance.
*/
addTrimEntity(entityName) {
return this.nerManager.addNamedEntity(entityName, 'trim');
}
/**
* Remove texts from an option of an entity for the given languages.
* @param {String} entityName Name of the entity.
* @param {String} optionName Name of the option.
* @param {String[]} languages Languages for adding the texts.
* @param {String[]} texts Texts tobe added.
*/
removeNamedEntityText(entityName, optionName, languages, texts) {
return this.nerManager.removeNamedEntityText(
entityName,
optionName,
languages,
texts
);
}
/**
* Assign an intent to a domain.
* @param {String} intent Intent to be assigned.
* @param {String} domain Domain to include the intent.
*/
assignDomain(intent, domain) {
this.intentDomains[intent] = domain;
}
/**
* Returns the domain of a given intent.
* @param {String} intent Intent name.
* @returns {String} Domain of the intent.
*/
getIntentDomain(intent) {
return this.intentDomains[intent];
}
/**
* Get an object with the intents of each domain.
*/
getDomains() {
const keys = Object.keys(this.intentDomains);
const result = {};
for (let i = 0, l = keys.length; i < l; i += 1) {
const intent = keys[i];
const domain = this.intentDomains[intent];
if (!result[domain]) {
result[domain] = [];
}
result[domain].push(intent);
}
return result;
}
/**
* Adds a new utterance associated to an intent for the given locale.
* @param {String} srcLocale Locale of the language.
* @param {String} utterance Text of the utterance.
* @param {String} intent Intent name.
*/
addDocument(srcLocale, utterance, intent) {
let locale = NlpUtil.getTruncatedLocale(srcLocale);
if (!locale) {
locale = this.guessLanguage(utterance);
}
if (!locale) {
throw new Error('Locale must be defined');
}
const classifier = this.classifiers[locale];
if (!classifier) {
throw new Error(`Classifier not found for locale ${locale}`);
}
classifier.add(utterance, intent);
if (this.getIntentDomain(intent) === undefined) {
this.assignDomain(intent, 'default');
}
const entities = this.nerManager.getEntitiesFromUtterance(utterance);
this.slotManager.addBatch(intent, entities);
}
/**
* Removes an utterance associated to an intent for the given locale.
* @param {String} srcLocale Locale of the language.
* @param {String} utterance Text of the utterance.
* @param {String} intent Intent name.
*/
removeDocument(srcLocale, utterance, intent) {
let locale = NlpUtil.getTruncatedLocale(srcLocale);
if (!locale) {
locale = this.guessLanguage(utterance);
}
if (!locale) {
throw new Error('Locale must be defined');
}
const classifier = this.classifiers[locale];
if (!classifier) {
throw new Error(`Classifier not found for locale ${locale}`);
}
classifier.remove(utterance, intent);
}
/**
* Adds an answer for a locale and intent.
* @param {String} locale Locale of the intent.
* @param {String} intent Intent name.
* @param {String} answer Text of the answer.
* @param {String} condition Condition to be evaluated.
*/
addAnswer(locale, intent, answer, condition) {
this.nlgManager.addAnswer(locale, intent, answer, condition);
}
/**
* Remove and answer from a locale and intent.
* @param {String} locale Locale of the intent.
* @param {String} intent Intent name.
* @param {String} answer Text of the answer.
* @param {String} condition Condition to be evaluated.
*/
removeAnswer(locale, intent, answer, condition) {
this.nlgManager.removeAnswer(locale, intent, answer, condition);
}
/**
* Train the classifiers for the provided locales. If no locale is
* provided, then retrain all the classifiers.
* @param {String[]} locale List of locales for being retrained.
*/
async train(locale) {
let languages;
if (locale) {
languages = Array.isArray(locale) ? locale : [locale];
} else {
({ languages } = this);
}
await Promise.all(
languages.map(async language => {
const truncated = NlpUtil.getTruncatedLocale(language);
const classifier = this.classifiers[truncated];
if (classifier) {
await classifier.train();
}
})
);
}
/**
* Given an utterance and a locale, try to classify the utterance into one intent.
* @param {String} srcLocale Locale of the text. If not provided,
* the locale is guessed.
* @param {String} srcUtterance Text to be classified
*/
classify(srcLocale, srcUtterance) {
let utterance = srcUtterance;
let locale = srcLocale;
if (!utterance) {
utterance = srcLocale;
locale = this.guessLanguage(utterance);
}
const truncated = NlpUtil.getTruncatedLocale(locale);
const classifier = this.classifiers[truncated];
if (!classifier) {
return undefined;
}
return classifier.getClassifications(utterance);
}
/**
* Gets the sentiment of an utterance.
* @param {String} srcLocale Locale of the text. If not provided, is guessed.
* @param {Promise.String} srcUtterance Texto to analyze the sentiment.
*/
getSentiment(srcLocale, srcUtterance) {
let utterance = srcUtterance;
let locale = srcLocale;
if (!utterance) {
utterance = srcLocale;
locale = this.guessLanguage(utterance);
}
const truncated = NlpUtil.getTruncatedLocale(locale);
return this.sentiment.process(truncated, utterance);
}
getAnswer(locale, intent, context) {
const answer = this.nlgManager.findAnswer(locale, intent, context);
if (answer && answer.response) {
return answer.response;
}
return undefined;
}
/**
* Indicates if all the classifications has exactly 0.5 score.
* @param {Object[]} classifications Array of classifications.
* @returns {boolean} True if all classifications score is 0.5.
*/
isEqualClassification(classifications) {
for (let i = 0; i < classifications.length; i += 1) {
if (classifications[i].value !== 0.5) {
return false;
}
}
return true;
}
/**
* Process to extract entities from an utterance.
* @param {string} srcLocale Locale of the utterance, optional.
* @param {string} srcUtterance Text of the utterance.
* @param {string[]} whitelist Optional whitelist of entity names.
* @returns {Object[]} Array of entities.
*/
async extractEntities(srcLocale, srcUtterance, whitelist) {
let utterance = srcUtterance;
let locale = srcLocale;
if (!utterance) {
utterance = locale;
locale = this.guessLanguage(utterance);
}
if (!this.languages.includes(NlpUtil.getTruncatedLocale(locale))) {
locale = this.guessLanguage(utterance);
}
const truncated = NlpUtil.getTruncatedLocale(locale);
const result = await this.nerManager.findEntities(
utterance,
truncated,
whitelist
);
return result;
}
/**
* Process an utterance for full classify and analyze. If the locale is
* not provided, then it will be guessed.
* Classify the utterance and extract entities from it, returning an
* object with all the information available.
* Also calculates the sentiment of the utterance, if possible.
* @param {String} srcLocale Language locale of the utterance.
* @param {String} srcUtterance Text of the utterance.
* @param {Promise.Object} Promise srcContext Context for finding answers.
*/
async process(srcLocale, srcUtterance, srcContext) {
let utterance = srcUtterance;
let locale = srcLocale;
let languageGuessed = false;
if (!utterance) {
utterance = locale;
locale = this.guessLanguage(utterance);
languageGuessed = true;
}
Iif (!this.languages.includes(NlpUtil.getTruncatedLocale(locale))) {
locale = this.guessLanguage(utterance);
languageGuessed = true;
if (!locale) {
[locale] = this.languages;
}
}
const truncated = NlpUtil.getTruncatedLocale(locale);
const result = {};
result.locale = locale;
result.localeIso2 = truncated;
result.language = (
this.guesser.languagesAlpha2[result.localeIso2] || {}
).name;
result.utterance = utterance;
if (languageGuessed && this.settings.fullSearchWhenGuessed) {
let bestScore;
let bestClassification;
this.languages.forEach(language => {
const classification = this.classify(language, utterance);
if (classification && classification.length > 0) {
Eif (bestScore === undefined || classification[0].value > bestScore) {
bestScore = classification[0].value;
bestClassification = classification;
}
}
});
const optionalUtterance = await this.nerManager.generateEntityUtterance(
utterance,
truncated
);
this.languages.forEach(language => {
const classification = this.classify(language, optionalUtterance);
if (classification && classification.length > 0) {
if (bestScore === undefined || classification[0].value > bestScore) {
bestScore = classification[0].value;
bestClassification = classification;
}
}
});
result.classification = bestClassification;
} else {
result.classification = this.classify(truncated, utterance);
const optionalUtterance = await this.nerManager.generateEntityUtterance(
utterance,
truncated
);
if (optionalUtterance !== utterance) {
const optionalClassification = this.classify(
truncated,
optionalUtterance
);
if (
optionalClassification &&
optionalClassification.length > 0 &&
optionalClassification[0].value > result.classification[0].value
) {
result.classification = optionalClassification;
}
}
}
if (
!result.classification ||
result.classification.length === 0 ||
this.isEqualClassification(result.classification)
) {
result.intent = 'None';
result.domain = 'default';
result.score = 1;
} else {
result.intent = result.classification[0].label;
result.domain = this.getIntentDomain(result.intent);
result.score = result.classification[0].value;
}
result.entities = await this.nerManager.findEntities(
utterance,
truncated,
this.slotManager.getIntentEntityNames(result.intent)
);
const context = srcContext || {};
result.entities.forEach(entity => {
context[entity.entity] = entity.option || entity.utteranceText;
});
result.sentiment = await this.getSentiment(truncated, utterance);
const answer = this.getAnswer(truncated, result.intent, context);
if (answer) {
result.srcAnswer = answer;
result.answer = Handlebars.compile(answer)(context);
}
if (this.slotManager.process(result, context)) {
result.entities.forEach(entity => {
context[entity.entity] = entity.option || entity.utteranceText;
});
Eif (result.srcAnswer) {
result.answer = Handlebars.compile(result.srcAnswer)(context);
}
}
return this.processTransformer(result);
}
/**
* Clear the NLP Manger.
*/
clear() {
this.nerManager = new NerManager(this.settings.ner);
this.languages = [];
this.classifiers = {};
this.slotManager.clear();
this.nlgManager = new NlgManager();
}
/**
* Load NLP manager information from a string.
* @param {String|Object} data JSON string or object to load NLP manager information from.
*/
import(data) {
const clone = typeof data === 'string' ? JSON.parse(data) : data;
this.settings = clone.settings;
this.languages = clone.languages;
this.nerManager.load(clone.nerManager);
this.slotManager.load(clone.slotManager);
this.intentDomains = clone.intentDomains || {};
this.nlgManager.responses = clone.responses;
for (let i = 0, l = clone.classifiers.length; i < l; i += 1) {
const classifierClone = clone.classifiers[i];
this.addLanguage(classifierClone.language);
const classifier = this.classifiers[classifierClone.language];
classifier.docs = classifierClone.docs;
classifier.features = classifierClone.features;
classifier.settings.useNeural = !!classifierClone.useNeural;
if (classifierClone.neuralClassifier) {
const { neuralClassifier } = classifier.settings;
neuralClassifier.settings = classifierClone.neuralClassifier.settings;
Object.keys(classifierClone.neuralClassifier.classifierMap).forEach(
label => {
neuralClassifier.addTrainer(label);
neuralClassifier.classifierMap[label].fromJSON(
classifierClone.neuralClassifier.classifierMap[label]
);
}
);
}
const lrc = classifier.settings.classifier;
const { logistic } = classifierClone;
lrc.observations = {};
Object.entries(logistic.observations).forEach(([label, matrix]) => {
lrc.observations[label] = [];
matrix.forEach((row, rowIndex) => {
// Create a array filled with as many zeros as features
const features = Array(classifier.features.length).fill(0);
// Set the features for the positions stored with 1. The others remains as zero.
row.forEach(featPosition => {
features[featPosition] = 1;
});
lrc.observations[label][rowIndex] = features;
});
});
lrc.labels = logistic.labels;
lrc.classifications = logistic.classifications;
lrc.observationCount = logistic.observationCount;
lrc.theta = logistic.theta;
}
}
/**
* Export NLP manager information as a string.
* @param {Boolean} minified If true, the returned JSON will have no spacing or indentation.
* @returns {String} NLP manager information as a JSON string.
*/
export(minified = false) {
const clone = {};
clone.settings = this.settings;
clone.languages = this.languages;
clone.intentDomains = this.intentDomains;
clone.nerManager = this.nerManager.save();
clone.slotManager = this.slotManager.save();
clone.classifiers = [];
clone.responses = this.nlgManager.responses;
Eif (this.languages && this.languages.length > 0) {
this.languages.forEach(language => {
const classifier = this.classifiers[language];
const classifierClone = {};
classifierClone.language = classifier.settings.language;
classifierClone.docs = classifier.docs;
classifierClone.features = classifier.features;
classifierClone.logistic = {};
const { logistic } = classifierClone;
const lrc = classifier.settings.classifier;
classifierClone.useNeural = classifier.settings.useNeural;
Eif (classifier.settings.neuralClassifier) {
const { neuralClassifier } = classifier.settings;
classifierClone.neuralClassifier = {};
classifierClone.neuralClassifier.settings = neuralClassifier.settings;
classifierClone.neuralClassifier.classifierMap = {};
Object.keys(neuralClassifier.classifierMap).forEach(key => {
classifierClone.neuralClassifier.classifierMap[
key
] = neuralClassifier.classifierMap[key].toJSON();
});
}
logistic.observations = lrc.observations;
Object.entries(lrc.observations).forEach(([label, matrix]) => {
matrix.forEach((features, row) => {
// Store array of positions for the features equals to 1.
logistic.observations[label][row] = features
.map((feat, index) => (feat === 1 ? index : undefined))
.filter(n => n);
});
});
logistic.labels = lrc.labels;
logistic.classifications = lrc.classifications;
logistic.observationCount = lrc.observationCount;
logistic.theta = lrc.theta;
clone.classifiers.push(classifierClone);
});
}
return minified ? JSON.stringify(clone) : JSON.stringify(clone, null, 2);
}
/**
* Save the NLP manager information into a file.
* @param {String} srcFileName Filename for saving the NLP manager.
*/
save(srcFileName) {
const fileName = srcFileName || 'model.nlp';
fs.writeFileSync(fileName, this.export(), 'utf8');
}
/**
* Load the NLP manager information from a file.
* @param {String} srcFilename Filename for loading the NLP manager.
*/
load(srcFileName) {
const fileName = srcFileName || 'model.nlp';
const data = fs.readFileSync(fileName, 'utf8');
this.import(data);
}
/**
* Load the NLP manager information from an excel file.
* @param {Sting} srcFileName File name of the excel.
*/
loadExcel(srcFileName) {
this.clear();
const fileName = srcFileName || 'model.xls';
const reader = new NlpExcelReader(this);
reader.load(fileName);
}
}
module.exports = NlpManager;
|