-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemotion_classifier.js
More file actions
72 lines (64 loc) · 1.89 KB
/
emotion_classifier.js
File metadata and controls
72 lines (64 loc) · 1.89 KB
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
"use strict";
var emotionClassifier = function() {
var previousParameters = [];
var classifier = {};
var emotions = [];
var coefficient_length;
this.getEmotions = function() {
return emotions;
}
this.init = function(model) {
// load it
for (var m in model) {
emotions.push(m);
classifier[m] = {};
classifier[m]['bias'] = model[m]['bias'];
classifier[m]['coefficients'] = model[m]['coefficients'];
}
coefficient_length = classifier[emotions[0]]['coefficients'].length;
}
this.getBlank = function() {
var prediction = [];
for (var j = 0;j < emotions.length;j++) {
prediction[j] = {"emotion" : emotions[j], "value" : 0.0};
}
return prediction;
}
this.predict = function(parameters) {
var prediction = [];
for (var j = 0;j < emotions.length;j++) {
var e = emotions[j];
var score = classifier[e].bias
for (var i = 0;i < coefficient_length;i++) {
score += classifier[e].coefficients[i]*parameters[i+6];
}
prediction[j] = {"emotion" : e, "value" : 0.0};
prediction[j]['value'] = 1.0/(1.0 + Math.exp(-score));
}
return prediction;
}
this.meanPredict = function (parameters) {
// store to array of 10 previous parameters
previousParameters.splice(0, previousParameters.length == 10 ? 1 : 0);
previousParameters.push(parameters.slice(0));
if (previousParameters.length > 9) {
// calculate mean of parameters?
var meanParameters = []
for (var i = 0;i < parameters.length;i++) {
meanParameters[i] = 0;
}
for (var i = 0;i < previousParameters.length;i++) {
for (var j = 0;j < parameters.length;j++) {
meanParameters[j] += previousParameters[i][j];
}
}
for (var i = 0;i < parameters.length;i++) {
meanParameters[i] /= 10;
}
// calculate logistic regression
return this.predict(meanParameters);
} else {
return false;
}
}
}