-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtag.js
154 lines (124 loc) · 5.22 KB
/
tag.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
// var HTML_TAG_WEIGHTS = {};
function Tag(str, tasksList, correctOccurences, incorrectOccurences) {
this.text = str;
this.textLowerCase = str.toLowerCase();
this.tasks = tasksList || {};
this.frequency = 0;
this.correctOccurences = correctOccurences || 0;
this.incorrectOccurences = incorrectOccurences ||0;
this.positiveFactor = 0.5;
this.negativeFactor = -0.5;
// Use this if separate frequency of html tags is not needed. Using this for now as we are extracting "nes" from a page.
this.increaseFrequency = function (url, taskid) {
this.frequency++;
if (taskid != 0) {
if (!this.tasks[taskid]) {
this.tasks[taskid] = {};
}
if (!this.tasks[taskid][url]) {
this.tasks[taskid][url] = 1;
} else {
this.tasks[taskid][url]++;
}
}
};
this.getTaskWeightsNew = function (taskURLs) {
const taskScores = {};
const taskFrequencies = {};
let maxTaskFrequency = 0;
for (var taskid in this.tasks) {
const urls = taskURLs[taskid];
let totalTagFrequencyInTask = 0;
for (let i = 0; i < urls.length; i++) {
if (urls[i] != null) {
if (urls[i].indexOf("chrome-extension://") < 0 && urls[i].indexOf("chrome://") < 0) {
if (typeof (this.tasks[taskid][urls[i]]) == typeof (3)) {
totalTagFrequencyInTask = totalTagFrequencyInTask + this.tasks[taskid][urls[i]];
}
}
}
}
taskFrequencies[taskid] = totalTagFrequencyInTask;
if (totalTagFrequencyInTask > maxTaskFrequency) {
maxTaskFrequency = totalTagFrequencyInTask;
}
}
for (var taskid in taskFrequencies) {
taskScores[taskid] = (taskFrequencies[taskid] - maxTaskFrequency) / maxTaskFrequency;
}
return taskScores;
};
this.getTaskWeight = function (taskid, taskURLs) {
if (this.tasks.hasOwnProperty(taskid)) {
const urls = taskURLs[taskid];
let totalTagFrequencyInTask = 0;
for (let i = 0; i < urls.length; i++) {
if (urls[i] != null) {
if (urls[i].indexOf("chrome-extension://") < 0 && urls[i].indexOf("chrome://") < 0) {
if (typeof (this.tasks[taskid][urls[i]]) === typeof (3)) {
totalTagFrequencyInTask = totalTagFrequencyInTask + this.tasks[taskid][urls[i]];
}
}
}
}
let weight = totalTagFrequencyInTask / Object.keys(this.tasks).length;
if (this.correctOccurences != null) {
weight = weight + (this.correctOccurences * this.positiveFactor);
}
if (this.incorrectOccurences != null) {
weight = weight + (this.incorrectOccurences * this.negativeFactor); // negative factor is negative so the weight will get decreased.
}
return weight;
}
return 0;
};
this.getTaskWeights = function (taskURLs) {
const taskScores = {};
for (let tid in taskURLs) {
taskScores[tid] = 0;
}
for (let taskid in taskURLs) {
if (this.tasks.hasOwnProperty(taskid)) {
const urls = taskURLs[taskid];
let totalTagFrequencyInTask = 0;
for (let i = 0; i < urls.length; i++) {
if (urls[i] !== null) {
if (urls[i].indexOf("chrome-extension://") < 0 && urls[i].indexOf("chrome://") < 0) {
if (typeof (this.tasks[taskid][urls[i]]) === typeof (3)) {
totalTagFrequencyInTask = totalTagFrequencyInTask + this.tasks[taskid][urls[i]];
}
}
}
}
let weight = totalTagFrequencyInTask / Object.keys(this.tasks).length;
if (this.correctOccurences !== null) {
weight = weight + (this.correctOccurences * this.positiveFactor);
}
if (this.incorrectOccurences !== null) {
weight = weight + (this.incorrectOccurences * this.negativeFactor); // negative factor is negative so the weight will get decreased.
}
taskScores[taskid] = weight;
}
}
return taskScores;
};
}
function getMatchScore(tag1, tag2) {
return tag1.positiveWeight + tag2.positiveWeight;
}
// Tag1 should be the smaller one that needs to be merged into the bigger Tag2.
function mergeTags(tag1, tag2) {
for (let taskid in tag1["tasks"]) {
const taskurls = tag1["tasks"][taskid];
for (let url in taskurls) {
const urlFrequency = taskurls[url];
if (!tag2["tasks"].hasOwnProperty(taskid)) {
tag2["tasks"][taskid] = {};
}
if (!tag2["tasks"][taskid].hasOwnProperty(url)) {
tag2["tasks"][taskid][url] = urlFrequency;
}
}
}
return tag2;
}