This repository has been archived by the owner on Aug 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
144 lines (129 loc) · 4.38 KB
/
index.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
const BrainJSClassifier = require('natural-brain');
const Datastore = require('nedb');
const _ = require('underscore')
const fs = require('fs');
const request = require('request');
function random(items) {return items[Math.floor(Math.random()*items.length)]};
class Lazy {
constructor(obj) {
// obj => db = 'data', selfTrain = true, slient = false
typeof obj !== 'object' ? obj = { db: 'data', slient: false, selfTrain: true } : ''
this.db = obj.db || 'data';
this.slient = obj.slient || false;
this.selfTrain = obj.selfTrain || true;
this.classifier = new BrainJSClassifier();
this.categories = new Datastore({ filename: `${this.db}.db`, autoload: true });
this.categories.ensureIndex({ fieldName: 'name', unique: true }, function (err) {
if (err) console.log(err);
});
}
learn(obj) {
return new Promise(resolve => {
this.classifier.addDocument(obj.phrase, obj.category);
this.classifier.retrain();
this.categories.insert({name:obj.category, responses: [], actions: []}, function(err, doc) {
resolve(doc)
});
});
}
getCategories() {
return new Promise((resolve, reject) => {
this.categories.find({}, (err, docs) => {
err ? reject(err) : resolve(docs);
});
});
}
quiet() {
return new Promise(resolve => {
this.slient = !this.slient;
resolve(slient);
});
}
getResponses(obj) {
return new Promise((resolve, reject) => {
this.categories.findOne({name: obj.category}, (err, docs) => {
err ? reject(err) : resolve(docs.responses)
});
});
}
getActions(obj) {
return new Promise((resolve, reject) => {
this.categories.findOne({name: obj.category}, (err, docs) => {
err ? reject(err) : resolve(docs.actions)
});
});
}
addResponse(obj) {
return new Promise((resolve, reject) => {
this.categories.update({ name: obj.category }, { $push: { responses: obj.response } }, {}, (err) => {
err ? reject(err) : resolve(true)
});
});
}
addAction(obj) {
return new Promise((resolve, reject) => {
this.categories.update({ name: obj.category }, { $push: { actions: obj.actions } }, {}, (err) => {
err ? reject(err) : resolve(true)
});
});
}
query(obj) {
var categories = this.categories;
let classified = this.classifier.getClassifications(obj.phrase);
classified = _.sortBy(classified, 'value')
classified.reverse();
let possibility = 0;
let value = classified[0].value;
if (value > 0.8) {
possibility = 1;
} else if (value > 0.3 && value < 0.8) {
possibility = 0.5;
} else {
possibility = 0;
}
const category = this.classifier.classify(obj.phrase)
return new Promise((resolve, reject) => {
this.categories.findOne({name: category}, (err, docs) => {
var response = "";
if (docs.actions && docs.actions.length > 0) {
request.post(random(docs.actions), {form:{input:obj.phrase, category:category, details:classified, possibility}}, (err, res, body) => {
if (!err) {
response = body;
resolve({status: true, possibility, response, details: classified})
} else {
response = random(docs.responses);
resolve({status: true, possibility, response, details: classified})
}
})
} else {
if (!this.slient) {
response = random(docs.responses);
resolve({status: true, possibility, response, details: classified})
}
}
if (this.selfTrain) {
this.classifier.addDocument(obj.phrase, category); // Add document
this.classifier.retrain(); // Re train! :)
}
});
});
}
loadTrainedData() {
const db = this.db;
this.classifier = BrainJSClassifier.restore(JSON.parse(fs.readFileSync(`./${db}.json`, 'utf8')));
}
save() {
let {classifier, db} = this;
fs.writeFileSync(`${db}.json`, JSON.stringify(classifier), 'utf8');
}
removeDocument(obj) {
return new Promise((resolve, reject) => {
this.classifier.removeDocument(obj.phrase, obj.category);
this.classifier.retrain();
this.classifier.remove({ name:obj.category }, {}, function (err, numRemoved) {
err ? reject(err) : resolve(true)
});
});
}
}
module.exports = Lazy;