-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trainer.js
444 lines (359 loc) · 17.1 KB
/
trainer.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* Privee is released under the BSD 3-Clause License.
* Copyright (c) 2014, Sebastian Zimmeck and Steven M. Bellovin
* All rights reserved.
*
* trainer.js trains the ML classifier
* this is the multinomial naive Bayes version (commented out below is also the Bernoulli naive Bayes version;
* to use it, completely comment out the current multinomial naive Bayes code and change classifier.js as well)
*/
chrome.extension.onMessage.addListener(function(request, sender) {
if (request.action == "checkForTraining") {
if (localStorage.trainingDone === undefined) {
// set the number of training policies contained in trainingData.js (100 training policies) and the first policy (policy 1) to process
sessionStorage.setItem("TDSIZE", 100);
sessionStorage.setItem("currTrainDocNo", 1);
sessionStorage.setItem("trainPhrases", "start");
// set up classes and complement classes
localStorage.setItem("CSIZE", 6);
localStorage.setItem("classes", "#Collection #Profiling #ThirdPartyTracking #Disclosure #LimitedRetention #Encryption");
localStorage.setItem("complementClasses", "#NoCollection #NoProfiling #NoThirdPartyTracking #NoDisclosure #NoLimitedRetention #NoEncryption");
var classes = localStorage.getItem("classes").split(" ");
var complementClasses = localStorage.getItem("complementClasses").split(" ");
var CLASSIFICATION_SIZE = localStorage.getItem("CSIZE");
// set up storage
for (var j=0; j<CLASSIFICATION_SIZE; j++) {
// counters for how many times a document is assigned a certain class / complement class, for prior probabilities
sessionStorage.setItem("classCounter"+j, 0);
sessionStorage.setItem("complementClassCounter"+j, 0);
// storage for the vocabularies and their sizes in classes and complement classes
sessionStorage.setItem("vocabulary"+j, "");
localStorage.setItem("vocabularySize"+j, 0);
// counters for words in classes and complement classes
localStorage.setItem(classes[j], 0);
localStorage.setItem(complementClasses[j], 0);
}
// announce to the user that training is required
labelGrade.innerText = "On this first run of the automatic classifiers Privee needs to take one minute to analyze training data. Please wait.";
chrome.extension.sendMessage({
action: "trainingRequired",
});
}
else {
chrome.extension.sendMessage({
action: "trainingDone",
});
}
}
if (request.action == "train") {
chrome.extension.sendMessage({
action: "requestTrainingSections",
});
}
if (request.action == "forwardTrainingSections") {
train();
// if not yet all training documents processed, continue training
if (parseInt(sessionStorage.getItem("currTrainDocNo")) != parseInt(sessionStorage.getItem("TDSIZE"))) {
// increment counter to keep track of how many training policies have been processed
temp = parseInt(sessionStorage.getItem("currTrainDocNo")) + 1;
sessionStorage.setItem("currTrainDocNo", temp);
chrome.extension.sendMessage({
action: "trainingRequired",
});
}
// if all training documents have been processed, get probabilities
if (parseInt(sessionStorage.getItem("currTrainDocNo")) == parseInt(sessionStorage.getItem("TDSIZE"))) {
probabilities();
localStorage.trainingDone = "yes";
chrome.extension.sendMessage({
action: "trainingDone",
});
}
}
});
// train on the training privacy policies
function train() {
var classes = localStorage.getItem("classes").split(" ");
var complementClasses = localStorage.getItem("complementClasses").split(" ");
var CLASSIFICATION_SIZE = localStorage.getItem("CSIZE");
var TRAINING_DATA_SIZE = sessionStorage.getItem("TDSIZE");
var temp = 0;
var reg = new RegExp(/\b\S+\b/g);
// for each privacy policy iterate over all classifications
for (var j=0; j<CLASSIFICATION_SIZE; j++) {
// leave the current iteration of the loop if the current training section is already classified by the rule classifier
if ((sessionStorage.getItem("trainingSection"+j) == "class") || (sessionStorage.getItem("trainingSection"+j) == "complementClass")) {
continue;
}
// j+word is count of a word for a class (count(w|c)), format is as follows: key: [class][word]; value: [word count]
var word = "";
while((word = reg.exec(sessionStorage.getItem("trainingSection"+j))) !== null) {
// word count for class
if (sessionStorage.getItem("CLASS").match(classes[j])) {
if (sessionStorage.getItem(j+word) === null) {
// count (add-1) smoothing and first word
sessionStorage.setItem(j+word, 2);
}
else {
temp = parseInt(sessionStorage.getItem(j+word)) + 1;
sessionStorage.setItem(j+word, temp);
}
}
// word count for complement class
else {
if (sessionStorage.getItem("c"+j+word) === null) {
// count (add-1) smoothing and first word
sessionStorage.setItem("c"+j+word, 2);
}
else {
temp = parseInt(sessionStorage.getItem("c"+j+word)) + 1;
sessionStorage.setItem("c"+j+word, temp);
}
}
}
// counter counts all words in a class/complement class including duplicates (count(c))
var counter = sessionStorage.getItem("trainingSection"+j).match(reg).length;
if (sessionStorage.getItem("CLASS").match(classes[j])) {
// add word count to class counter
temp = parseInt(localStorage.getItem(classes[j])) + counter;
localStorage.setItem(classes[j], temp);
// add 1 to class counter (to calculate prior probability)
temp = parseInt(sessionStorage.getItem("classCounter"+j)) + 1;
sessionStorage.setItem("classCounter"+j, temp);
}
else {
// add word count to complement class counter
temp = parseInt(localStorage.getItem(complementClasses[j])) + counter;
localStorage.setItem(complementClasses[j], temp);
// add 1 to complement class counter (to calculate prior probability)
temp = parseInt(sessionStorage.getItem("complementClassCounter"+j)) + 1;
sessionStorage.setItem("complementClassCounter"+j, temp);
}
// fill the vocabulary with unique words
var tempList = sessionStorage.getItem("trainingSection"+j).split(" ").filter(function(item,i,allItems){
return i==allItems.indexOf(item);
}).join(" ");
while((word = reg.exec(tempList)) !== null) {
// if the word is not yet contained in the vocabulary, include it
if ((sessionStorage.getItem("vocabulary"+j).search(word)) == -1) {
temp = sessionStorage.getItem("vocabulary"+j) + word + " ";
sessionStorage.setItem("vocabulary"+j, temp);
}
}
}
};
// get probabilities
function probabilities() {
var classes = localStorage.getItem("classes").split(" ");
var complementClasses = localStorage.getItem("complementClasses").split(" ");
var CLASSIFICATION_SIZE = localStorage.getItem("CSIZE");
var TRAINING_DATA_SIZE = sessionStorage.getItem("TDSIZE");
var temp = 0;
for (var j=0; j<CLASSIFICATION_SIZE; j++) {
// count the words in the vocabulary
// vocabulary size (|V|) of classes and complement classes excluding duplicates
// length returns 1 + integer key, therefore need to subtract 1
temp = sessionStorage.getItem("vocabulary"+j).split(" ").length - 1;
localStorage.setItem("vocabularySize"+j, temp);
// prior probability of a class (P(c)) is the number of documents with that class (N_c) divided by the total number of documents (N)
// (that is, P(c) = N_c / N)
localStorage.setItem("priorClassProb"+j, (parseInt(sessionStorage.getItem("classCounter"+j))/TRAINING_DATA_SIZE));
localStorage.setItem("priorComplClassProb"+j, (parseInt(sessionStorage.getItem("complementClassCounter"+j))/TRAINING_DATA_SIZE));
}
// likelihood of a word given a class (P(w|c)) is the count of the word for that class (count(w,c)) divided by the count of all words in that class (count(c))
// (that is, P(w|c) = count(w,c) + 1 / count(c) + |V|)
// for smoothing 1 is added to the numerator and the vocabulary size (|V|) is added to the denominator
for (var k=0; k<sessionStorage.length; k++) {
// match the beginning of key for word storage
for (var j=0; j<CLASSIFICATION_SIZE; j++) {
var reg2 = new RegExp(j);
var reg3 = new RegExp("c"+j);
// calculate likelihood for a class
if (sessionStorage.key(k).match(reg2) ) {
localStorage.setItem("P" + sessionStorage.key(k), (parseInt(sessionStorage.getItem(sessionStorage.key(k)))/
(parseInt(localStorage.getItem(classes[j])) + parseInt(localStorage.getItem("vocabularySize"+j)))));
}
// calculate likelihood for a complement class
if (sessionStorage.key(k).match(reg3) ) {
localStorage.setItem("P" + sessionStorage.key(k), (parseInt(sessionStorage.getItem(sessionStorage.key(k)))/
(parseInt(localStorage.getItem(complementClasses[j])) + parseInt(localStorage.getItem("vocabularySize"+j)))));
}
}
}
};
/*
// Bernoulli naive Bayes version
chrome.extension.onMessage.addListener(function(request, sender) {
if (request.action == "checkForTraining") {
if (localStorage.trainingDone === undefined) {
// set the number of training documents
sessionStorage.setItem("TDSIZE", 100);
sessionStorage.setItem("currTrainDocNo", 1);
sessionStorage.setItem("trainPhrases", "start");
// set up number of classes and complement classes
localStorage.setItem("CSIZE", 6);
// set up classes and complement classes
localStorage.setItem("classes", "#Collection #Profiling #ThirdPartyTracking #Disclosure #LimitedRetention #Encryption");
localStorage.setItem("complementClasses", "#NoCollection #NoProfiling #NoThirdPartyTracking #NoDisclosure #NoLimitedRetention #NoEncryption");
var classes = localStorage.getItem("classes").split(" ");
var complementClasses = localStorage.getItem("complementClasses").split(" ");
var CLASSIFICATION_SIZE = localStorage.getItem("CSIZE");
// set up storage
for (var j=0; j<CLASSIFICATION_SIZE; j++) {
// counters for how many times a document is assigned a certain class / complement class, for prior probabilities
sessionStorage.setItem("classCounter"+j, 0);
sessionStorage.setItem("complementClassCounter"+j, 0);
// storage for the vocabularies and their sizes in classes and complement classes
sessionStorage.setItem("vocabulary"+j, "");
localStorage.setItem("vocabularySize"+j, 0);
// counters for words in classes and complement classes
localStorage.setItem(classes[j], 0);
localStorage.setItem(complementClasses[j], 0);
}
// announce to the user that training is required
labelGrade.innerText = "On this first run of the automatic classifiers Privee needs to take one minute to analyze training data. Please wait.";
chrome.extension.sendMessage({
action: "trainingRequired",
});
}
else {
chrome.extension.sendMessage({
action: "trainingDone",
});
}
}
if (request.action == "train") {
chrome.extension.sendMessage({
action: "requestTrainingSections",
});
}
if (request.action == "forwardTrainingSections") {
train();
// if not yet all training documents processed, continue training
if (parseInt(sessionStorage.getItem("currTrainDocNo")) != parseInt(sessionStorage.getItem("TDSIZE"))) {
// increment counter to keep track of how many training policies have been processed
temp = parseInt(sessionStorage.getItem("currTrainDocNo")) + 1;
sessionStorage.setItem("currTrainDocNo", temp);
chrome.extension.sendMessage({
action: "trainingRequired",
});
}
// if all training documents processed, get probabilities
if (parseInt(sessionStorage.getItem("currTrainDocNo")) == parseInt(sessionStorage.getItem("TDSIZE"))) {
probabilities();
localStorage.trainingDone = "yes";
chrome.extension.sendMessage({
action: "trainingDone",
});
}
}
});
// train on the training privacy policies
function train() {
var classes = localStorage.getItem("classes").split(" ");
var complementClasses = localStorage.getItem("complementClasses").split(" ");
var CLASSIFICATION_SIZE = localStorage.getItem("CSIZE");
var TRAINING_DATA_SIZE = sessionStorage.getItem("TDSIZE");
var reg = new RegExp(/\b\S+\b/g);
var temp = 0;
var tempArray = new Array();
var tempWordsClass = "";
var tempWordsComplementClass = "";
// for each privacy policy iterates over all classifications
for (var j=0; j<CLASSIFICATION_SIZE; j++) {
// leave the current iteration of the loop if the current training document has already a rule classification
if ((sessionStorage.getItem("trainingSection"+j) == "class") || (sessionStorage.getItem("trainingSection"+j) == "complementClass")) {
continue;
}
// count of how many documents in a class/complement class contain a particular word (count(w,c))
var word = "";
while ((word = reg.exec(sessionStorage.getItem("trainingSection"+j))) !== null) {
// count (add-1) smoothing
if (sessionStorage.getItem("c"+j+word) === null) {
sessionStorage.setItem("c"+j+word, 1);
}
if (sessionStorage.getItem("co"+j+word) === null) {
sessionStorage.setItem("co"+j+word, 1);
}
// count for class
if (sessionStorage.getItem("CLASS").match(classes[j])) {
tempArray = tempWordsClass.split(" ");
for (var k=0; k<tempArray.length; k++) {
if (tempArray[k].match(word)) {
temp = 1;
break;
}
}
if (temp != 1) {
temp = parseInt(sessionStorage.getItem("c"+j+word)) + 1;
sessionStorage.setItem("c"+j+word, temp);
tempWordsClass += word + " ";
}
temp = 0;
}
// count for complement class
if (sessionStorage.getItem("CLASS").match(complementClasses[j])) {
tempArray = tempWordsComplementClass.split(" ");
for (var k=0; k<tempArray.length; k++) {
if (tempArray[k].match(word)) {
temp = 1;
break;
}
}
if (temp != 1) {
temp = parseInt(sessionStorage.getItem("co"+j+word)) + 1;
sessionStorage.setItem("co"+j+word, temp);
tempWordsComplementClass += word + " ";
}
}
}
// count of documents in a class and its complement class (count(c))
if (sessionStorage.getItem("CLASS").match(classes[j])) {
// add 1 to class counter (also to calculate prior probability)
temp = parseInt(sessionStorage.getItem("classCounter"+j)) + 1;
sessionStorage.setItem("classCounter"+j, temp);
}
else {
// add 1 to complement class counter (qlso to calculate prior probability)
temp = parseInt(sessionStorage.getItem("complementClassCounter"+j)) + 1;
sessionStorage.setItem("complementClassCounter"+j, temp);
}
}
};
// get probabilities
function probabilities() {
var classes = localStorage.getItem("classes").split(" ");
var complementClasses = localStorage.getItem("complementClasses").split(" ");
var CLASSIFICATION_SIZE = localStorage.getItem("CSIZE");
var TRAINING_DATA_SIZE = sessionStorage.getItem("TDSIZE");
var temp = 0;
for (var j=0; j<CLASSIFICATION_SIZE; j++) {
// constant for the two possibilities of each term; ocurrence and nonoccurence (C)
localStorage.setItem("vocabularySize"+j, 2);
// prior probability of a class, P(c), is the number of documents with that class, count(c), divided by the total number of documents, N,
// that is, P(c) = count(c) / N
localStorage.setItem("priorClassProb"+j, (parseInt(sessionStorage.getItem("classCounter"+j))/TRAINING_DATA_SIZE));
localStorage.setItem("priorComplClassProb"+j, (parseInt(sessionStorage.getItem("complementClassCounter"+j))/TRAINING_DATA_SIZE));
}
// likelihood of a word given a class, P(w|c), is the occurence of the word for that class, count(w,c), which includes one additional count for smoothing
// divided by the count of all documents in that class, count(c), plus a constant, C = 2, for the two possibilities occurence and nonoccurence of a term
// in a class, that is, P(w|c) = count(w,c) / count(c) + C
for (var k=0; k<sessionStorage.length; k++) {
// match the beginning of key for word storage
for (var j=0; j<CLASSIFICATION_SIZE; j++) {
var reg2 = new RegExp("c"+j);
var reg3 = new RegExp("co"+j);
// calculate likelihood for a class
if (sessionStorage.key(k).match(reg2)) {
localStorage.setItem("P" + sessionStorage.key(k), (parseInt(sessionStorage.getItem(sessionStorage.key(k)))/
(parseInt(sessionStorage.getItem("classCounter"+j)) + parseInt(localStorage.getItem("vocabularySize"+j)))));
}
// calculate likelihood for a complement class
if (sessionStorage.key(k).match(reg3)) {
localStorage.setItem("P" + sessionStorage.key(k), (parseInt(sessionStorage.getItem(sessionStorage.key(k)))/
(parseInt(sessionStorage.getItem("complementClassCounter"+j)) + parseInt(localStorage.getItem("vocabularySize"+j)))));
}
}
}
};
*/