-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocessor.js
432 lines (351 loc) · 12 KB
/
preprocessor.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
/*
* Privee is released under the BSD 3-Clause License.
* Copyright (c) 2014, Sebastian Zimmeck and Steven M. Bellovin
* All rights reserved.
*
* preprocessor.js prepares the machine learning (ML) classification and also attempts a rule and crowdsourcing classification of the current policy
*/
chrome.extension.onMessage.addListener(function(request, sender) {
var CLASSIFICATION_SIZE = 6;
var currentPolicy, webServiceName, training;
currentPolicy = request.source;
// if this is not training, preprocess the current policy
if (request.action == "getSections") {
// get current Web service name
webServiceName = getWebServiceName(currentPolicy);
// access online repository ToS;DR and check whether there are crowdsourcing results for current policy
$.getJSON("http://tosdr.org/index/services.json").done(function(data) {
// if crowdsourcing results are available on ToS;DR, retrieve those
if (data[webServiceName]) {
// get grade from ToS;DR
var grade = data[webServiceName].class;
sessionStorage.setItem("crowdGrade", grade);
// get classifications from ToS;DR
var classifications;
var name = data[webServiceName].points;
classifications = crowdPreprocessor(name);
chrome.extension.sendMessage({
action: "returnClassifications",
source: classifications
});
}
// if no crowdsourcing results are available on ToS;DR, prepare automatic classification
else {
training = "no";
for (var j=0; j<CLASSIFICATION_SIZE; j++) {
// the jth section in the current privacy policy
sessionStorage.setItem("section"+j, mlPreprocessor(currentPolicy, j, training));
}
chrome.extension.sendMessage({
action: "returnSections",
});
}
});
}
// if this is training, preprocess the training policies
if (request.action == "getTrainingSections") {
currentPolicy = sessionStorage.getItem("TRAIND");
training = "yes";
for (var j=0; j<CLASSIFICATION_SIZE; j++) {
// the jth section in the current privacy policy
sessionStorage.setItem("trainingSection"+j, mlPreprocessor(currentPolicy, j, training));
}
chrome.extension.sendMessage({
action: "returnTrainingSections",
});
}
});
// extract Web service name from current website URL
function getWebServiceName(policy) {
var temp, tempArray, name;
tempArray = policy.split(" ");
temp = tempArray[0];
tempArray = temp.split(".");
if (temp.match(/www/g)) {
name = tempArray[1];
}
else {
if (tempArray.length == 2) {
name = tempArray[0];
tempArray = name.split("/");
name = tempArray[2];
}
else {
name = tempArray[1];
}
}
return name;
}
// retrieve the crowdsourcing classifications from ToS;DR
// pass in points (at http://tosdr.org/index/services.json) for current Web service name to retrieve its titles (e.g., at http://tosdr.org/points/ozS1etNH7ZA.json)
function crowdPreprocessor(name) {
var grade;
var allPoints = "";
var allTitles = "";
var tempArray = JSON.stringify(name).replace(/["\[\]]/g,"").split(",");
for (var i=0; i<tempArray.length; i++) {
getPoint(tempArray[i], function(pointString) {allPoints += pointString + " "});
getTitle(tempArray[i], function(titleString) {allTitles += titleString + "~"});
}
sessionStorage.setItem("crowdClassificationPoints", allPoints.replace(/["]/g,""));
sessionStorage.setItem("crowdClassificationTitles", allTitles.replace(/["]/g,""));
sessionStorage.setItem("CROWD_CLASSIFICATION_SIZE", allPoints.length);
classifications = "crowdsourcing";
return classifications;
}
// retrieve the crowdsourcing classifications from ToS;DR
// pass in current point to retrieve corresponding icon (called point as well) (e.g., from http://tosdr.org/points/ozS1etNH7ZA.json)
function getPoint(point, callback) {
var currentPoint, pointString;
$.ajax({
dataType: "json",
url: "https://tosdr.org/points/" + point + ".json",
async: false,
success: function(data) {
currentPoint = data.tosdr.point;
pointString = JSON.stringify(currentPoint);
}
});
callback(pointString);
}
// retrieve the crowdsourcing classifications from ToS;DR
// pass in current point to retrieve corresponding title (e.g., from http://tosdr.org/points/ozS1etNH7ZA.json)
function getTitle(point, callback) {
var title, titleString;
$.ajax({
dataType: "json",
url: "https://tosdr.org/points/" + point + ".json",
async: false,
success: function(data) {
title = data.title;
titleString = JSON.stringify(title);
}
});
callback(titleString);
}
// make rule classification and preprocess the policy for the machine learning classification
function mlPreprocessor (privacyPolicy, currentSection, training) {
// regular expressions for rule classification (selection of bigram features from policy text)
var ruleRegExArray = new Array(
// collection
"noRule",
// profiling
"(\\bother\\b|\\boutside\\b) (\\bsourc.*)",
// ad tracking
"(\\bad\\b|\\badvertis.*) (\\bcompan.*|\\bnetwork.*|\\bprovider.*|\\bservin.*|\\bserve.*|\\bvendor.*)|(\\bcontext.*|\\bnetwork.*|\\bparti.*|\\bserv.*) (\\bad\\b|\\badvertis.*)",
// ad disclosure
"noRule",
// limited retention
"noRule",
// encryption
"(.*) (\\bencrypt.*|\\bssl.*)|(\\bsecur\\b) (\\bsocket.*|\\bprotocol.*|\\bweb.*)");
// regular expressions for ML preprocessing (selection of bigram features from policy text)
var mlRegExArray = new Array(
// collection
"(\\bcollect\\b|\\bobtain\\b|\\bgather\\b|\\breceiv\\b) (.+)|(.+) (\\bcollect\\b|\\bobtain\\b|\\bgather\\b|\\breceiv\\b)",
// profiling
"(\\bappend\\b|\\bcombin\\b|\\bsupplement\\b|\\bsourc\\b) (.+)|(.+) (\\bappend\\b|\\bcombin\\b|\\bsupplement\\b|\\bsourc\\b)",
// ad tracking
"(\\bad\\b|\\badvertis\\b|\\bmarket\\b) (.+)|(.+) (\\bad\\b|\\badvertis\\b|\\bmarket\\b)",
// ad disclosure
"(\\bselect\\b|\\btrust\\b|\\breput\\b|\\bcertain\\b) (\\bcompani\\b|\\bthird\\b|\\bparti\\b|\\bmarket\\b|\\bpartner\\b|\\borgan)|(\\bmarket\\b|\\bsend\\b|\\boffer\\b) (\\bto\\b|\\bproduct\\b|\\bservic\\b|\\bgood\\b)",
// limited retention
"(\\bretain\\b|\\bretent\\b|\\bbackup\\b|\\barchiv\\b|\\bresidu\\b|\\bindefinit\\b|\\blong\\b|\\blonger\\b|\\bobligation\\b) (.+)|(.+) (\\bretain\\b|\\bretent\\b|\\bbackup\\b|\\barchiv\\b|\\bresidu\\b|\\bindefinit\\b|\\blong\\b|\\blonger\\b|\\bobligation\\b)",
// encryption
"noNaiveBayes");
// replacing punctuation with one whitespace allows for phrase recognition via the regex taking into account the sentence structure
var policyText = privacyPolicy.replace(/[^a-zA-Z ]/g," ").toLowerCase().split(" ");
var section, currentPhrase, currentWord1, currentWord2;
// rule classification (not available for training documents, which will be all classified by ML, i.e. naive Bayes)
if ((training == "no") && (ruleRegExArray[currentSection] != "noRule")) {
for (var i in policyText) {
currentPhrase = policyText[i==0?policyText.length-1:i-1] + " " + policyText[i];
if (currentPhrase.match(ruleRegExArray[currentSection])) {
section = "class";
break;
}
}
}
// prepare naive Bayes classification, use stemming
if (section == null) {
section = " ";
for (var i in policyText) {
currentWord1 = stemmer(policyText[i==0?policyText.length-1:i-1]);
currentWord2 = stemmer(policyText[i]);
if ((currentWord1 + " " + currentWord2).match(mlRegExArray[currentSection])) {
section += currentWord1 + currentWord2 + " ";
}
}
}
// if no features have been extracted, it can be concluded that information is not collected, disclosed, etc.
if (section == " ") {
section = "complementClass";
}
return section;
}
// Porter stemmer in Javascript. Few comments, but it's easy to follow against the rules in the original
// paper, in
//
// Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,
// no. 3, pp 130-137,
//
// see also http://www.tartarus.org/~martin/PorterStemmer
// Release 1 be 'andargor', Jul 2004
// Release 2 (substantially revised) by Christopher McKenzie, Aug 2009
var stemmer = (function(){
var step2list = {
"ational" : "ate",
"tional" : "tion",
"enci" : "ence",
"anci" : "ance",
"izer" : "ize",
"bli" : "ble",
"alli" : "al",
"entli" : "ent",
"eli" : "e",
"ousli" : "ous",
"ization" : "ize",
"ation" : "ate",
"ator" : "ate",
"alism" : "al",
"iveness" : "ive",
"fulness" : "ful",
"ousness" : "ous",
"aliti" : "al",
"iviti" : "ive",
"biliti" : "ble",
"logi" : "log"
},
step3list = {
"icate" : "ic",
"ative" : "",
"alize" : "al",
"iciti" : "ic",
"ical" : "ic",
"ful" : "",
"ness" : ""
},
c = "[^aeiou]", // consonant
v = "[aeiouy]", // vowel
C = c + "[^aeiouy]*", // consonant sequence
V = v + "[aeiou]*", // vowel sequence
mgr0 = "^(" + C + ")?" + V + C, // [C]VC... is m>0
meq1 = "^(" + C + ")?" + V + C + "(" + V + ")?$", // [C]VC[V] is m=1
mgr1 = "^(" + C + ")?" + V + C + V + C, // [C]VCVC... is m>1
s_v = "^(" + C + ")?" + v; // vowel in stem
return function (w) {
var stem,
suffix,
firstch,
re,
re2,
re3,
re4,
origword = w;
if (w.length < 3) { return w; }
firstch = w.substr(0,1);
if (firstch == "y") {
w = firstch.toUpperCase() + w.substr(1);
}
// Step 1a
re = /^(.+?)(ss|i)es$/;
re2 = /^(.+?)([^s])s$/;
if (re.test(w)) { w = w.replace(re,"$1$2"); }
else if (re2.test(w)) { w = w.replace(re2,"$1$2"); }
// Step 1b
re = /^(.+?)eed$/;
re2 = /^(.+?)(ed|ing)$/;
if (re.test(w)) {
var fp = re.exec(w);
re = new RegExp(mgr0);
if (re.test(fp[1])) {
re = /.$/;
w = w.replace(re,"");
}
} else if (re2.test(w)) {
var fp = re2.exec(w);
stem = fp[1];
re2 = new RegExp(s_v);
if (re2.test(stem)) {
w = stem;
re2 = /(at|bl|iz)$/;
re3 = new RegExp("([^aeiouylsz])\\1$");
re4 = new RegExp("^" + C + v + "[^aeiouwxy]$");
if (re2.test(w)) { w = w + "e"; }
else if (re3.test(w)) { re = /.$/; w = w.replace(re,""); }
else if (re4.test(w)) { w = w + "e"; }
}
}
// Step 1c
re = /^(.+?)y$/;
if (re.test(w)) {
var fp = re.exec(w);
stem = fp[1];
re = new RegExp(s_v);
if (re.test(stem)) { w = stem + "i"; }
}
// Step 2
re = /^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/;
if (re.test(w)) {
var fp = re.exec(w);
stem = fp[1];
suffix = fp[2];
re = new RegExp(mgr0);
if (re.test(stem)) {
w = stem + step2list[suffix];
}
}
// Step 3
re = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/;
if (re.test(w)) {
var fp = re.exec(w);
stem = fp[1];
suffix = fp[2];
re = new RegExp(mgr0);
if (re.test(stem)) {
w = stem + step3list[suffix];
}
}
// Step 4
re = /^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/;
re2 = /^(.+?)(s|t)(ion)$/;
if (re.test(w)) {
var fp = re.exec(w);
stem = fp[1];
re = new RegExp(mgr1);
if (re.test(stem)) {
w = stem;
}
} else if (re2.test(w)) {
var fp = re2.exec(w);
stem = fp[1] + fp[2];
re2 = new RegExp(mgr1);
if (re2.test(stem)) {
w = stem;
}
}
// Step 5
re = /^(.+?)e$/;
if (re.test(w)) {
var fp = re.exec(w);
stem = fp[1];
re = new RegExp(mgr1);
re2 = new RegExp(meq1);
re3 = new RegExp("^" + C + v + "[^aeiouwxy]$");
if (re.test(stem) || (re2.test(stem) && !(re3.test(stem)))) {
w = stem;
}
}
re = /ll$/;
re2 = new RegExp(mgr1);
if (re.test(w) && re2.test(w)) {
re = /.$/;
w = w.replace(re,"");
}
// and turn initial Y back to y
if (firstch == "y") {
w = firstch.toLowerCase() + w.substr(1);
}
return w;
}
})();