-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalgorithm.js
More file actions
90 lines (77 loc) · 2.49 KB
/
algorithm.js
File metadata and controls
90 lines (77 loc) · 2.49 KB
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
// Sample student data (you should replace this with your own dataset)
const students = [
{
name: 'Shaurya',
hobbies: ['Reading', 'Painting'],
major: 'Computer Science',
gender: 'Female'
},
{
name: 'Karan',
hobbies: ['Painting', 'Music'],
major: 'IT Engineering',
gender: 'Male'
},
{
name: 'Arun',
hobbies: ['Reading', 'Sports'],
major: 'Frontend',
gender: 'Male'
},
{
name: 'Naman',
hobbies: ['Music', 'Sports'],
major: 'Computer Science',
gender: 'Male'
},
{
name: 'Sonali',
hobbies: ['Reading', 'Sports'],
major: 'Computer Science',
gender: 'Female'
},
];
// Define matching criteria and weights
const criteria = [
{ name: 'Common Hobbies', weight: 0.7, attribute: 'hobbies' },
{ name: 'Academic Background', weight: 0.3, attribute: 'major' },
{ name: 'Gender', weight: 0.1, attribute: 'gender' },
];
// Matching threshold ---> defines strictness of matching
const matchingThreshold = 0.6;
// Function to calculate the similarity score between two students
function calculateSimilarityScore(studentA, studentB) {
let totalWeightedScore = 0;
let totalWeight = 0;
for (const criterion of criteria) {
const attribute = criterion.attribute;
if (studentA[attribute] === studentB[attribute]) {
const normalizedScore = 1; // Perfect match
totalWeightedScore += criterion.weight * normalizedScore;
totalWeight += criterion.weight;
}
}
if (totalWeight === 0) return 0; // No match in any criteria
return totalWeightedScore / totalWeight;
}
// Function to find potential friends for a given student
function findPotentialFriends(student, threshold) {
const potentialFriends = [];
for (const otherStudent of students) {
if (otherStudent === student) continue; // Skip comparing the student to themselves
const similarityScore = calculateSimilarityScore(student, otherStudent);
if (similarityScore >= threshold) {
potentialFriends.push({ name: otherStudent.name, similarityScore });
}
}
return potentialFriends;
}
const studentToMatch = students[0];
const potentialFriends = findPotentialFriends(studentToMatch, matchingThreshold);
// for (const friend of potentialFriends) {
// console.log(`${friend.name} (Similarity Score: ${friend.similarityScore})`);
// }
console.log(`Potential Friends for ${studentToMatch.name}:`);
for (const friend of potentialFriends) {
console.log(`${friend.name} (Similarity Score: ${friend.similarityScore})`);
}