forked from PretendoNetwork/Yamamura
-
Notifications
You must be signed in to change notification settings - Fork 11
/
message-processor.js
76 lines (57 loc) · 1.79 KB
/
message-processor.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
const natural = require('natural');
const fs = require('fs-extra');
const cld = require('cld');
const trainingDataDir = `${__dirname}/training-data`;
class AIMessageProcessor {
constructor() {
this.classifierThreshold = 1;
this.cldThreshold = 100;
this.classifiers = {};
this.answers = {};
// TODO - Add an option to make this save to disk and reuse old training models
this.train();
}
train() {
const locales = fs.readdirSync(trainingDataDir);
for (const locale of locales) {
this.classifiers[locale] = new natural.LogisticRegressionClassifier();
this.answers[locale] = {};
const trainingFiles = fs.readdirSync(`${trainingDataDir}/${locale}`);
for (const file of trainingFiles) {
const trainingData = fs.readJSONSync(`${trainingDataDir}/${locale}/${file}`);
this.answers[locale][trainingData.intent] = trainingData.answer;
for (const utterance of trainingData.utterances) {
this.classifiers[locale].addDocument(utterance, trainingData.intent);
}
}
this.classifiers[locale].train();
}
}
async classify(text) {
let locale;
try {
const { languages } = await cld.detect(text);
const language = languages.find(({ percent }) => percent >= this.cldThreshold);
locale = language?.code;
} catch {
return { locale };
}
const classifier = this.classifiers[locale];
if (!classifier) {
return { locale };
}
const classifications = classifier.getClassifications(text);
const classification = classifications.find(({ value }) => value >= this.classifierThreshold)?.label;
return {
locale,
intent: classification
};
}
async getResponseOrNothing(message) {
const { locale, intent } = await this.classify(message);
if (intent) {
return this.answers[locale]?.[intent];
}
}
}
module.exports = AIMessageProcessor;