-
Notifications
You must be signed in to change notification settings - Fork 65
/
classify.js
97 lines (81 loc) · 2.24 KB
/
classify.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
import Q from 'q';
import debug from 'debug';
import BrainJSClassifier from 'natural-brain';
import pos from 'pos';
import crypto from 'crypto';
import _ from 'lodash';
import Extractor from '../extractor';
const log = debug('mysam:classifier');
const lexer = new pos.Lexer();
const tagger = new pos.Tagger();
export function addPos(hook, next) {
let data = hook.result;
let tokens = lexer.lex(data.input);
let tags = tagger.tag(tokens).map(function(touple) {
return touple[1];
});
data.pos = { tokens, tags };
next();
}
export function extract(hook, next) {
let data = hook.result;
let e = new Extractor(lexer.lex(data.text), data.tags || []);
data.extracted = e.extract(data.pos.tokens);
next();
}
export default {
after: {
create: [addPos, extract]
},
create(data, params, callback) {
log('Classifying', data);
if(this.retrain) {
log('Retraining Neural network');
this.classifier.retrain();
this.retrain = false;
}
let { input } = data;
let id = crypto.createHash('md5').update(input).digest('hex');
let action = this.classifier.classify(input);
let classifications = this.classifier.getClassifications(input);
Q.ninvoke(this.actions, 'get', action)
.then(action => {
return _.extend(action, { id, input, classifications });
})
.then(result => {
log(result);
return result;
})
.fail(err => log(err.stack))
.nodeify(callback);
},
add(action) {
log('Adding action to training', action);
this.classifier.addDocument(action.text, '' + action._id);
this.retrain = true;
},
setup(app) {
let add = this.add.bind(this);
// We want to classify every word
BrainJSClassifier.disableStopWords();
this.classifier = new BrainJSClassifier({
iterations: 2000
});
this.actions = app.service('actions');
this.actions.on('created', add);
//this.actions.on('removed', action => {
//
//});
Q.ninvoke(this.actions, 'find').then(list => {
list.forEach(add);
return list;
})
.then(list => {
if(list.length) {
this.classifier.train();
this.retrain = false;
}
})
.fail(error => log('ERROR: ', error.stack));
}
};