-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaize.ts
209 lines (191 loc) · 6.57 KB
/
baize.ts
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
/**
* The core javascript of the baize.
*/
import { UrlParse } from './lib/urlparse';
import * as idDict from './domain_id.json';
import * as generalRules from './convert_general.json';
export class Baize {
/**
* Used to save models
*/
forest: any = null;
nEstimators: number = 0;
nClasses: number = 0;
/**
* @description The url is pre-processed into data that meets the prediction criteria.
* @returns [domain,third-party,type,root-domain,path-length,query-count,adwords,sub-domain]
*/
preProcessing(url: string, thirdParty: number, type: string): any {
let dataset = [];
let urlparse = new UrlParse(url);
dataset.push(this.getDomainId(urlparse.getDomain()));
dataset.push(thirdParty);
dataset.push(this.getTypeId(type))
dataset.push(this.getRootDomainId(urlparse.getRootDomain()))
dataset.push(urlparse.getPath().length)
dataset.push(urlparse.getQueryCount())
dataset.push(this.matchingRules(urlparse.getPath() + "?" + urlparse.getQuery()))
if (urlparse.getSub() == "" || urlparse.getSub() == "www" || urlparse.getSub() == undefined) {
dataset.push(0)
} else {
dataset.push(1)
}
return dataset
}
/**
* @description Get the id of the normalized domain id.
* @returns domain id
*/
getDomainId(domain: string): number {
if (idDict.domain[domain]) {
return idDict.domain[domain];
} else {
return 0;
}
}
/**
* @description Get the id of the normalized type id.
* @returns normalized domain id
*/
getTypeId(type: string) {
if (idDict.type[type]) {
return idDict.type[type];
} else {
return 0;
}
}
/**
* @description Get the id of the normalized root-domain id.
* @returns normalized root-domain id
*/
getRootDomainId(domain: string) {
if (idDict["root-domain"][domain]) {
return idDict["root-domain"][domain];
} else {
return 0;
}
}
/**
* @description Determine if it matches a generic path rule.
* @param url Path of the url + parameters of the url request
* @returns If or not it can be matched by general rules, 0 for no match, 1 for match.
*/
matchingRules(url: string): number {
for (let i in generalRules) {
if (generalRules[i].indexOf("*") >= 0 && generalRules[i].split("*").length > 0) {
if (url.indexOf(generalRules[i].split("*")[0].replace("*", "")) >= 0 && url.indexOf(generalRules[i].split("*")[1].replace("*", "")) >= 0) {
return 1;
} else if (generalRules[i].indexOf("*") >= 0) {
if (url.indexOf(generalRules[i].replace("*", ""))) {
return 1;
}
}
} else {
if (url.indexOf(generalRules[i]) >= 0) {
return 1;
}
}
}
return 0;
}
/**
* Used to compare the size of values.
* @param nums
*/
private imax(nums: Array<number>): number {
let index = 0;
for (let i = 0, l = nums.length; i < l; i++) {
index = nums[i] > nums[index] ? i : index;
}
return index;
};
/**
* Iteration tree for prediction.
* @param tree
* @param features
* @param node
* @returns
*/
private predictHandle(tree, features, node) {
if (tree['thresholds'][node] != -2) {
if (features[tree['indices'][node]] <= tree['thresholds'][node]) {
return this.predictHandle(tree, features, tree['childrenLeft'][node]);
} else {
return this.predictHandle(tree, features, tree['childrenRight'][node]);
}
}
return tree['classes'][node].slice();
};
/**
* @description The data to be put in the prediction should be in the order of [domain,third-party,type,root-domain,path-length,query-count,adwords,sub-domain].
* @param features features to be projected, not supported in bulk.
* @returns Predict results
*/
predict(features: any): any {
if(this.forest == null){
throw "The model must be loaded first."
}
let preds = new Array(this.nEstimators).fill(new Array(this.nClasses).fill(0.));
let i:number, j:number;
for (i = 0; i < this.nEstimators; i++) {
preds[i] = this.predictHandle(this.forest[i], features, 0);
}
let classes = new Array(this.nClasses).fill(0.);
for (i = 0; i < this.nEstimators; i++) {
let normalizer = 0.;
for (j = 0; j < this.nClasses; j++) {
normalizer += preds[i][j];
}
if (normalizer == 0.) {
normalizer = 1;
}
for (j = 0; j < this.nClasses; j++) {
preds[i][j] = preds[i][j] / normalizer;
if (preds[i][j] <= 2.2204460492503131e-16) {
preds[i][j] = 2.2204460492503131e-16;
}
preds[i][j] = Math.log(preds[i][j]);
}
let sum = 0.;
for (j = 0; j < this.nClasses; j++) {
sum += preds[i][j];
}
for (j = 0; j < this.nClasses; j++) {
preds[i][j] = (this.nClasses - 1) * (preds[i][j] - (1. / this.nClasses) * sum);
}
for (j = 0; j < this.nClasses; j++) {
classes[j] += preds[i][j];
}
}
return this.imax(classes)
}
/**
* @description Obtaining accuracy through test dataset.
* @param featuresList Data features used for testing.
* @param predictions Marked values
*/
acc(featuresList: any, predictions: any): number {
let results = [];
featuresList.forEach((val:number, idx:number) => {
results.push(this.predict(val))
});
let nSamples = predictions.length;
let nCorrect = 0;
results.forEach((val:number, idx:number) => {
if (val == predictions[idx]) {
nCorrect++;
}
});
return nCorrect / nSamples;
}
/**
* @method
* @description Loading a model from a saved json string.
* @param modelJson String of models
*/
load(modelJson: string): void {
this.forest = JSON.parse(modelJson);
this.nEstimators = this.forest.length;
this.nClasses = this.forest[0]['classes'][0].length;
}
}