-
Notifications
You must be signed in to change notification settings - Fork 0
/
recommender.go
346 lines (279 loc) · 9.49 KB
/
recommender.go
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package recommender
import (
"fmt"
"math"
"sort"
"strconv"
"strings"
"github.com/anirudhgray/balkan-assignment/infra/database"
"github.com/anirudhgray/balkan-assignment/models"
)
// GetUserLikesDislikes retrieves the book IDs that a user has liked and disliked based on their review ratings.
// It queries the database for reviews associated with the given user ID and categorizes the books into liked and disliked lists.
func GetUserLikesDislikes(userID uint) (liked []uint, notLiked []uint, err error) {
var reviews []models.Review
result := database.DB.Where("user_id = ?", userID).Find(&reviews)
if result.Error != nil {
return nil, nil, result.Error
}
var likedBooks []uint
var notLikedBooks []uint
for _, review := range reviews {
if review.Rating >= 4 {
likedBooks = append(likedBooks, review.BookID)
} else {
notLikedBooks = append(notLikedBooks, review.BookID)
}
}
return likedBooks, notLikedBooks, nil
}
// GetUsersWhoReviewedBook retrieves the user IDs of users who have reviewed a specific book.
// It queries the database for reviews associated with the given book ID and returns the user IDs of users who have reviewed the book.
func GetUsersWhoReviewedBook(bookID uint) (userslist []uint, err error) {
var reviews []models.Review
result := database.DB.Where("book_id = ?", bookID).Find(&reviews)
if result.Error != nil {
return nil, result.Error
}
var users []uint
for _, review := range reviews {
users = append(users, review.UserID)
}
return users, nil
}
// GetUsersWithSimilarInteractions retrieves user IDs who have interacted with books that are similar to the given user's liked and disliked books.
// It queries the database for users who have reviewed books that match the liked and disliked books of the given user.
func GetUsersWithSimilarInteractions(likedBooks []uint, dislikedBooks []uint, userID uint) (userslist []uint, err error) {
var users []uint
for _, bookID := range likedBooks {
reviewedUsers, err := GetUsersWhoReviewedBook(bookID)
if err != nil {
return nil, err
}
users = append(users, reviewedUsers...)
}
for _, bookID := range dislikedBooks {
reviewedUsers, err := GetUsersWhoReviewedBook(bookID)
if err != nil {
return nil, err
}
users = append(users, reviewedUsers...)
}
// remove duplicates and remove the current user's ID
currentUserID := userID
uniqueUsers := make(map[uint]bool)
var resultUsers []uint
for _, userID := range users {
if userID != currentUserID && !uniqueUsers[userID] {
uniqueUsers[userID] = true
resultUsers = append(resultUsers, userID)
}
}
return resultUsers, nil
}
func formatFloat(num float64, prc int) string {
var (
zero, dot = "0", "."
str = fmt.Sprintf("%."+strconv.Itoa(prc)+"f", num)
)
return strings.TrimRight(strings.TrimRight(str, zero), dot)
}
// CalculateUserSimilarity of two users via modified Jaccard Coefficient.
//
// S(U1, U2) = (|L1 intersec L2| + |D1 intersect D2| - |L1 intersect D2| - |L2 intersect D1|) / |L1 union L2 union D1 union D2|
func CalculateUserSimilarity(currentUserLiked, currentUserDisliked, otherUserLiked, otherUserDisliked []uint) (similarityCoefficient float64) {
L1, L2, D1, D2 := currentUserLiked, otherUserLiked, currentUserDisliked, otherUserDisliked
L1IntersectL2Size := intersectionSize(currentUserLiked, otherUserLiked)
D1IntersectD2Size := intersectionSize(currentUserDisliked, otherUserDisliked)
L1IntersectD2Size := intersectionSize(currentUserLiked, otherUserDisliked)
L2IntersectD1Size := intersectionSize(otherUserLiked, currentUserDisliked)
numerator := float64(L1IntersectL2Size + D1IntersectD2Size - L1IntersectD2Size - L2IntersectD1Size)
denominator := float64(unionSize4(L1, L2, D1, D2))
if numerator == 0 {
return 0
}
similarity := numerator / denominator
res, _ := strconv.ParseFloat(formatFloat(similarity, 3), 64)
return res
}
func intersectionSize(set1, set2 []uint) int {
count := 0
set2Map := make(map[uint]bool)
for _, item := range set2 {
set2Map[item] = true
}
for _, item := range set1 {
if set2Map[item] {
count++
}
}
return count
}
func unionSize4(arr1, arr2, arr3, arr4 []uint) int {
unionSet := make(map[uint]bool)
for _, item := range arr1 {
unionSet[item] = true
}
for _, item := range arr2 {
unionSet[item] = true
}
for _, item := range arr3 {
unionSet[item] = true
}
for _, item := range arr4 {
unionSet[item] = true
}
return len(unionSet)
}
// CalculateSimilaritiesWithOtherUsers returns a mapping of similar users (returned by GetUsersWithSimilarInteractions) to their similarity coefficient with current user.
func CalculateSimilaritiesWithOtherUsers(currentUserID uint, similarUsers []uint, currentUserLiked, currentUserDisliked []uint) (sims map[uint]float64, err error) {
userSimilarities := make(map[uint]float64)
for _, otherUserID := range similarUsers {
otherUserLiked, otherUserDisliked, err := GetUserLikesDislikes(otherUserID)
if err != nil {
return nil, err
}
similarity := CalculateUserSimilarity(currentUserLiked, currentUserDisliked, otherUserLiked, otherUserDisliked)
userSimilarities[otherUserID] = similarity
}
return userSimilarities, nil
}
// GetUnreviewedBooks fetches books which the current user has not yet reviewed (will select recommendations from among these)
func GetUnreviewedBooks(currentUserID uint) ([]uint, error) {
userLikedBooks, userDislikedBooks, err := GetUserLikesDislikes(currentUserID)
if err != nil {
return nil, err
}
userReviewedBookIDs := append(userLikedBooks, userDislikedBooks...)
var unreviewedBooks []uint
var allBookIDs []uint
result := database.DB.Model(&models.Book{}).Pluck("id", &allBookIDs)
if result.Error != nil {
return nil, result.Error
}
// Filter out the books that the user has reviewed
for _, bookID := range allBookIDs {
if !contains(userReviewedBookIDs, bookID) {
unreviewedBooks = append(unreviewedBooks, bookID)
}
}
return unreviewedBooks, nil
}
func contains(arr []uint, item uint) bool {
for _, val := range arr {
if val == item {
return true
}
}
return false
}
// CalculateRecommendationProbabilities returns mapping on user's unreviewed books to the "probability" of them liking it (i.e., order of recommendation).
//
// ZL = sum of similarity coefficients of other similar users who have liked a particular book.
//
// ML = number of such users as above
//
// ZD = sum of similarity coefficients of other similar users who have disliked a particular book.
//
// MD = number of such users as above
//
// P(U, B) = (ZL - ZD) / (|ML| + |MD|)
//
// Produces a value between -1 and 1
func CalculateRecommendationProbabilities(currentUserID uint, unreviewedBooks []uint, similarUsers map[uint]float64) map[uint]float64 {
recommendationProbabilities := make(map[uint]float64)
for _, bookID := range unreviewedBooks {
ZL := 0.0
ZD := 0.0
ML := 0
MD := 0
likedByUsers, dislikedByUsers, err := GetLikersDislikersForBook(bookID)
if err != nil {
continue
}
for _, userID := range likedByUsers {
if similarity, ok := similarUsers[userID]; ok {
ZL += similarity
ML++
}
}
for _, userID := range dislikedByUsers {
if similarity, ok := similarUsers[userID]; ok {
ZD += similarity
MD++
}
}
if ML+MD == 0 {
continue
}
probability := (ZL - ZD) / float64(ML+MD)
recommendationProbabilities[bookID] = probability
}
return recommendationProbabilities
}
func GetLikersDislikersForBook(bookID uint) ([]uint, []uint, error) {
var likedByUsers []uint
var dislikedByUsers []uint
usersWhoLiked, err := GetUsersWhoLikedBook(bookID)
if err != nil {
return nil, nil, err
}
likedByUsers = append(likedByUsers, usersWhoLiked...)
// Fetch users who disliked the book
usersWhoDisliked, err := GetUsersWhoDislikedBook(bookID)
if err != nil {
return nil, nil, err
}
dislikedByUsers = append(dislikedByUsers, usersWhoDisliked...)
return likedByUsers, dislikedByUsers, nil
}
func GetUsersWhoLikedBook(bookID uint) ([]uint, error) {
var likedByUsers []uint
var reviews []models.Review
result := database.DB.Where("book_id = ? AND rating >= 4", bookID).Find(&reviews)
if result.Error != nil {
return nil, result.Error
}
for _, review := range reviews {
likedByUsers = append(likedByUsers, review.UserID)
}
return likedByUsers, nil
}
func GetUsersWhoDislikedBook(bookID uint) ([]uint, error) {
var dislikedByUsers []uint
var reviews []models.Review
result := database.DB.Where("book_id = ? AND rating < 4", bookID).Find(&reviews)
if result.Error != nil {
return nil, result.Error
}
for _, review := range reviews {
dislikedByUsers = append(dislikedByUsers, review.UserID)
}
return dislikedByUsers, nil
}
type Recommendation struct {
Book models.SafeBook
Probability float64
}
func GetRecommendedBooksSortedAndPaginated(recommendationProbabilities map[uint]float64, page int, perPage int) []Recommendation {
var recommendations []Recommendation
for bookID, probability := range recommendationProbabilities {
var book models.SafeBook
database.DB.Model(&models.Book{}).First(&book, bookID)
recommendations = append(recommendations, Recommendation{
Book: book,
Probability: probability,
})
}
// Sort recommendations by probability in descending order
sort.Slice(recommendations, func(i, j int) bool {
return recommendations[i].Probability > recommendations[j].Probability
})
// Paginate the recommendations
startIdx := (page - 1) * perPage
endIdx := int(math.Min(float64(startIdx+perPage), float64(len(recommendations))))
if startIdx < len(recommendations) {
return recommendations[startIdx:endIdx]
}
return []Recommendation{}
}