-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsibyl.js
285 lines (235 loc) · 7.07 KB
/
sibyl.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
var _ = require('underscore');
/**
* The Set object lets you store unique values of any type,
* whether primitive values or object references.
* @param {Array} initialKeys
*/
function Set(initialValues) {
this.size = 0;
this.values = [];
if (_.isArray(initialValues)) {
for (var i = 0, l = initialValues.length; i < l; i++) {
this.add(initialValues[i]);
}
}
return this;
}
/**
* Compute the intersection between
* an infinite number of sets
* @return {Set}
*/
Set.intersection = function() {
var set = new Set;
var sets = arguments;
sets[0].forEach(function(value) {
var exists = true;
for (var i = 1, l = sets.length; i < l; i++) {
if (!sets[i].has(value)) {
exists = false;
}
}
if (exists) {
set.add(value);
}
});
return set;
};
/**
* Compute the union between
* an infinite number of sets
* @return {Set}
*/
Set.union = function() {
var set = new Set;
var sets = arguments;
for (var i = 1, l = sets.length; i < l; i++) {
sets[i].forEach(function(value) {
set.add(value);
});
}
return set;
};
/**
* Appends a new element with the given value
* to the Set object.
* @param {Object} value
* @return {this}
*/
Set.prototype.add = function(value) {
if (!this.has(value)) {
this.values.push(value);
this.size++;
this.sort();
}
return this;
};
/**
* Removes all elements from the Set object.
* @return {this}
*/
Set.prototype.clear = function() {
this.values = [];
this.size = 0;
return this;
};
/**
* Removes the element associated to the value.
* Set.prototype.has(value) will return false afterwards.
* @param {Object} value
* @return {this}
*/
Set.prototype.delete = function(value) {
var index = this.indexOf(value);
if (index > -1) {
this.values.splice(index, 1);
this.size--;
this.sort();
}
return this;
};
/**
* Check if the key exists within the set
* @param {String} key
* @return {Boolean}
*/
Set.prototype.has = function(value) {
return this.indexOf(value) !== -1;
};
/**
* Perform a fast and sorted lookup
* for the given key
* @param {String} key
* @return {Number}
*/
Set.prototype.indexOf = function(value) {
var index = -1;
this.forEach(function(iValue, i) {
if (this.comparator(iValue) >= this.comparator(value)) {
if (_.isEqual(value, iValue)) {
index = i;
}
return false; // return false to break
}
}, this);
return index;
};
/**
* Perform a fast and sorted lookup
* for the given key
* @param {String} key
* @return {Number}
*/
Set.prototype.forEach = function(callbackFn, thisArg) {
for (var i = 0, l = this.values.length; i < l; i++) {
if (callbackFn.call(thisArg, this.values[i], i) === false) {
break;
}
}
};
/**
* Sort the values of the set
* to optimise querying
* @return {this}
*/
Set.prototype.sort = function() {
var that = this;
this.values.sort(function(a, b) {
var ca = that.comparator(a);
var cb = that.comparator(b);
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
return this;
};
/**
* Comparator used to filter and sort
* the values of the set
* @param {Object} value
* @return {Object}
*/
Set.prototype.comparator = function(value) {
return value;
};
/**
* Return an array of the values
* making up the set
* @return {Array}
*/
Set.prototype.toArray = function() {
return this.values;
};
var Sibyl = function() {
this.users = {};
this.items = {};
};
Sibyl.prototype.recordLike = function(user, item) {
if (!this.users[user]) {
this.users[user] = {};
this.users[user].likes = new Set;
this.users[user].dislikes = new Set;
}
if (!this.items[item]) {
this.items[item] = {};
this.items[item].likedBy = new Set;
this.items[item].dislikedBy = new Set;
}
this.users[user].likes.add(item);
this.users[user].dislikes.delete(item);
this.items[item].likedBy.add(user);
this.items[item].dislikedBy.delete(user);
return this;
};
Sibyl.prototype.recordDislike = function(user, item) {
if (!this.users[user]) {
this.users[user] = {};
this.users[user].likes = new Set;
this.users[user].dislikes = new Set;
}
if (!this.items[item]) {
this.items[item] = {};
this.items[item].likedBy = new Set;
this.items[item].dislikedBy = new Set;
}
this.users[user].dislikes.add(item);
this.users[user].likes.delete(item);
this.items[item].dislikedBy.add(user);
this.items[item].likedBy.delete(user);
return this;
};
Sibyl.prototype.getSimilarityBetween = function(firstUserId, secondUserId) {
var agreements,
disagreements,
total,
firstUser,
secondUser;
firstUser = this.users[firstUserId];
secondUser = this.users[secondUserId];
// number of likes and dislikes in common
agreements = Set.intersection(firstUser.likes, secondUser.likes).size;
agreements += Set.intersection(firstUser.dislikes, secondUser.dislikes).size;
// number of likes and dislikes not in common
disagreements = Set.intersection(firstUser.likes, secondUser.dislikes).size;
disagreements += Set.intersection(firstUser.dislikes, secondUser.likes).size;
total = Set.union(firstUser.likes, firstUser.dislikes, secondUser.likes, secondUser.dislikes).size;
return (agreements - disagreements) / total;
};
Sibyl.prototype.getPrediction = function(userId, itemId) {
var hiveMindSum,
ratedBy,
user,
item;
user = this.users[userId];
item = this.items[itemId];
hiveMindSum = 0.0;
ratedBy = item.likedBy.size + item.dislikedBy.size;
item.likedBy.forEach(function(raterId) {
hiveMindSum += this.getSimilarityBetween(userId, raterId);
}, this);
item.dislikedBy.forEach(function(raterId) {
hiveMindSum -= this.getSimilarityBetween(userId, raterId);
}, this);
return hiveMindSum / ratedBy;
};
module.exports = Sibyl;