-
Notifications
You must be signed in to change notification settings - Fork 3
/
score.js
133 lines (116 loc) · 3.28 KB
/
score.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
// import { calculateSentimentScore } from "./sentimentScore.js";
export function calculateScore(productInfo) {
console.log("inside calculateScore function: ", productInfo);
let totalWeight = 0; // Track total weight for scaling
let score = 0;
// Rating (weight = 40)
const rating = parseFloat(productInfo.rating);
if (!isNaN(rating)) {
// Check if rating is available and valid
const numberOfRatings = productInfo.hasOwnProperty("numberOfRatings")
? productInfo.numberOfRatings
: 1; // Handle potential missing data
const normalizedRating = Math.min(rating / 5, 1); // Normalize rating to 0-1
// Adjust weight based on number of ratings (higher weight for more ratings)
const ratingWeight =
40 * (Math.log10(numberOfRatings + 1) / Math.log10(1001)); // Logarithmic weighting for balance
score += normalizedRating * ratingWeight;
totalWeight += ratingWeight;
}
console.log(
"score after rating: ",
score,
"totalWeight after rating: ",
totalWeight
);
// Number of sales (weight = 20)
if (productInfo.numSales) {
score += Math.min(productInfo.numSales / 100, 1) * 20;
totalWeight += 20;
}
console.log(
"score after numSales: ",
score,
"totalWeight after numSales: ",
totalWeight
);
// Number of reviews (weight = 10)
if (productInfo.numReviews) {
score += Math.min(productInfo.numReviews / 100, 1) * 10;
totalWeight += 10;
}
console.log(
"score after numReviews: ",
score,
"totalWeight after numReviews: ",
totalWeight
);
// Sentiment of reviews (weight = 20)
// if (productInfo.reviewArr) {
// let sentimentScore = 0;
// try {
// sentimentScore = calculateSentimentScore(productInfo.reviewArr);
// } catch (e) {
// console.log(e);
// sentimentScore = 0;
// }
// score += 10 * sentimentScore;
// totalWeight += 10;
// }
// Warranty (weight = 10)
if (productInfo.warranty) {
score += productInfo.warranty;
totalWeight += 10;
}
console.log(
"score after warranty: ",
score,
"totalWeight after warranty: ",
totalWeight
);
// Return policy (weight = 5)
if (productInfo.returnPolicy > 0) {
score += 5;
}
totalWeight += 5;
console.log(
"score after returnPolicy: ",
score,
"totalWeight after returnPolicy: ",
totalWeight
);
// Delivery charges (weight = 5)
if (productInfo.deliveryCharges) {
if (productInfo.deliveryCharges === 0) {
score += 5;
} else if (productInfo.deliveryCharges < 200) {
score += 2;
}
totalWeight += 5;
}
console.log(
"score after deliveryCharges: ",
score,
"totalWeight after deliveryCharges: ",
totalWeight
);
// Delivery time (weight = 5)
if (productInfo.deliveryTime) {
if (productInfo.deliveryTime === 1) {
score += 5;
} else if (productInfo.deliveryTime === 2) {
score += 3;
}
totalWeight += 5;
}
console.log(
"score after deliveryTime: ",
score,
"totalWeight after deliveryTime: ",
totalWeight
);
// Scale score to 100 based on total weight (avoid division by zero)
// const offset = Math.floor(Math.random() * 11) - 5;
const scaledScore = totalWeight === 0 ? 0 : (score / totalWeight) * 100;
return scaledScore;
}