-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier.js
220 lines (201 loc) · 7.46 KB
/
classifier.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
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
// classifier.js
function Classifier(options) {
options = options || {};
this.applyInverse = options.applyInverse || false;
this.probabilityThreshold = options.probabilityThreshold || 0.5;
this.defaultCategory = options.defaultCategory || null;
this.tokens = {};
this.categoryCounts = {};
this.probabilities = {};
}
Classifier.prototype = {
train: function (trainingSet) {
var categories = Object.keys(trainingSet.categorizedItems);
var i = 0, j = 0, k = 0, category = "";
// Iterate over each category in the training set
for (i = 0; i < categories.length; i++) {
category = categories[i];
var subSet = trainingSet.categorizedItems[category];
this.categoryCounts[category] = subSet.length;
// Iterate over each data item in the category
for (j = 0; j < subSet.length; j++) {
var item = subSet[j];
// for each token in the data item, increment the token:category counter
var tokenlist = item.tokens;
for (k = 0; k < tokenlist.length; k++) {
var token = tokenlist[k];
if (!this.tokens[token]) {
this.tokens[token] = {};
}
if (!this.tokens[token][category]) {
this.tokens[token][category] = 1;
} else {
this.tokens[token][category] = 1 + this.tokens[token][category];
}
}
}
}
/*
After counting occurences of tokens, calculate probabilities.
This results in something like the following:
probabilities : {
category1: {
token1: 0.97,
token2: 0.63, ...
},
category2: { ...
}
*/
for (i = 0; i < categories.length; i++) {
category = categories[i];
for (k in this.tokens) {
if (this.tokens.hasOwnProperty(k)) {
var count = this.tokens[k][category] || 0;
var total = this.categoryCounts[category];
var percentage = count / total;
if (!this.probabilities[category]) {
this.probabilities[category] = {};
}
this.probabilities[category][k] = percentage;
}
}
}
},
validate: function (testSet) {
var total = 0;
var correctGuesses = 0;
var wrongGuesses = 0;
var wrongCategories = {};
var wrongItems = [];
var categories = testSet.categorizedItems;
var category;
for (category in categories) {
if (categories.hasOwnProperty(category)) {
var items = categories[category];
var item;
for (item in items) {
if (items.hasOwnProperty(item)) {
total += 1;
item = items[item];
var result = this.classify(item);
// if certainty is below probabilityThreshold, go with the default
if (result.probability <= this.probabilityThreshold) {
result.category = this.defaultCategory || result.category;
}
if (result.category === category) {
correctGuesses++;
} else {
if (!wrongCategories[result.category]) {
wrongCategories[result.category] = 1;
} else {
wrongCategories[result.category]++;
}
wrongItems.push(item.id);
wrongGuesses++;
}
}
}
}
}
return {
'total': total,
'correct': correctGuesses,
'wrong': wrongGuesses,
'accuracy': (correctGuesses / (correctGuesses + wrongGuesses)),
'wrongCategories': wrongCategories,
'wrongItems': wrongItems
};
},
classify: function (item) {
// for each category
var category;
var learnedProbabilities = this.probabilities;
var itemProbabilities = {};
var itemTokens = item.tokens;
for (category in learnedProbabilities) {
if (learnedProbabilities.hasOwnProperty(category)) {
itemProbabilities[category] = 1;
var t;
var probs = learnedProbabilities[category];
for (t in probs) {
// iterate over the tokens
if (probs.hasOwnProperty(t)) {
// and take the product of all probabilities
if (itemTokens.indexOf(t) !== -1) {
itemProbabilities[category] = itemProbabilities[category] * probs[t];
} else if (this.applyInverse) {
itemProbabilities[category] = itemProbabilities[category] * (1 - probs[t]);
}
}
}
}
}
// Pick the highest two probabilities
function compareCategories(a, b) {
if (a.probability > b.probability) {
return -1;
}
if (a.probability < b.probability) {
return 1;
}
return 0;
}
var categoryScores = [];
var sumOfProbabilities = 0;
var k;
for (k in itemProbabilities) {
if (itemProbabilities.hasOwnProperty(k)) {
categoryScores.push({
category: k,
probability: itemProbabilities[k]
});
sumOfProbabilities += itemProbabilities[k];
}
}
categoryScores = categoryScores.sort(compareCategories);
var firstPlace = categoryScores[0];
var secondPlace = categoryScores[1];
var timesMoreLikely = firstPlace.probability / secondPlace.probability;
var probability = firstPlace.probability / sumOfProbabilities;
return ({
'category': firstPlace.category,
'probability': probability,
'timesMoreLikely': timesMoreLikely,
'secondCategory': secondPlace.category,
'probabilities': categoryScores
});
}
};
function DataSet() {
this.categorizedItems = {};
}
DataSet.prototype = {
add: function (label, items) {
var originalItems = this.categorizedItems[label] || [];
this.categorizedItems[label] = originalItems.concat(items);
}
};
function Document(id, tokens) {
if (!id) {
console.log('Document(id, tokens) requires an id string');
}
this.id = id;
this.tokens = tokens || [];
}
Document.prototype = {
add: function (token) {
if (typeof token === 'string') {
if (this.tokens.indexOf(token) === -1) {
this.tokens.push(token);
}
} else if (typeof token === 'object' && token.length) { // array of tokens
var i;
for (i = 0; i < token.length; i++) {
this.add(token[i]);
}
}
}
};
exports.Classifier = Classifier;
exports.DataSet = DataSet;
exports.Document = Document;