This repository has been archived by the owner on Aug 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rec.js
68 lines (62 loc) · 2.21 KB
/
rec.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
const users = require('./users');
// Manhattan method
function manhattan(user1, user2) {
let distance = 0;
for (let track of Object.keys(user1))
if (track in user2)
distance += Math.abs(user1[track] - user2[track])
return distance;
}
// Euclidean method
function euclidean(user1, user2) {
let distance = 0;
for (let track of Object.keys(user1))
if (track in user2)
distance += ((user1[track] - user2[track]) ** 2);
return Math.sqrt(distance)
}
// Implementing Minkwoski Distance
function minkowski(userRating1, userRating2, lambda) {
let distance = 0, commonRatingStatu = false;
for (let track of Object.keys(userRating1)) {
if (userRating2.hasOwnProperty(track)) {
distance += Math.abs(((userRating1[track] - userRating2[track]) ** lambda));
}
}
commonRatingStatu = true;
if (commonRatingStatu)
return (distance ** 1 / lambda);
return 0;
}
// Computes nearest neighbor
function computeNearestNeighbor(username, users) {
let distances = [],distance, userDistanceCombination;
for (let user of Object.keys(users))
if (user !== username) {
distance=euclidean(users[user],users[username]);
// distance = manhattan(users[user], users[username]);
userDistanceCombination = { "distance": distance, "user": user };
distances.push(userDistanceCombination);
}
distances.sort(function (a, b) { return a.distance - b.distance });
return distances;
}
// Getting the tracks of the specified user
function getTracks(user) { let tracks = users[user]; return tracks; }
// Giving a recommendation for a specific user
function giveRecommendation(user) {
let neighborRating = getTracks(computeNearestNeighbor(user, users)[0].user)
let userRating = getTracks(user), recommendation = [];
for (let key of Object.keys(neighborRating))
if (neighborRating.hasOwnProperty(key) && !userRating.hasOwnProperty(key))
recommendation.push(neighborRating[key] + ' ' + key);
return recommendation.sort().reverse();
}
module.exports = {
euclidean,
manhattan,
minkowski,
giveRecommendation,
computeNearestNeighbor,
getTracks
}