-
Notifications
You must be signed in to change notification settings - Fork 1
/
algorithms_rate.go
181 lines (139 loc) · 5.09 KB
/
algorithms_rate.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
package gocommend
import (
"math"
"github.com/garyburd/redigo/redis"
)
// rate type
// Use this type when we collet both like and dislike data,
type algorithmsRate struct {
algorithms
}
func (this *algorithmsRate) updateSimilarityFor(userId string) error {
ratedItemSet, err := redis.Values(redisClient.Do("SUNION", this.cSet.userLiked(userId), this.cSet.userDisliked(userId)))
if err != nil {
return err
}
if len(ratedItemSet) == 0 {
return nil
}
itemLikeDislikeKeys := []string{}
for _, rs := range ratedItemSet {
itemId, _ := redis.String(rs, err)
itemLikeDislikeKeys = append(itemLikeDislikeKeys, this.cSet.itemLiked(itemId))
itemLikeDislikeKeys = append(itemLikeDislikeKeys, this.cSet.itemDisliked(itemId))
}
otherUserIdsWhoRated, err := redis.Values(redisClient.Do("SUNION", redis.Args{}.AddFlat(itemLikeDislikeKeys)...))
if err != nil {
return err
}
if len(otherUserIdsWhoRated) == 1 {
return nil
}
for _, rs := range otherUserIdsWhoRated {
otherUserId, _ := redis.String(rs, err)
if userId == otherUserId {
continue
}
score := this.jaccardCoefficient(userId, otherUserId)
redisClient.Do("ZADD", this.cSet.userSimilarity(userId), score, otherUserId)
}
return err
}
func (this *algorithmsRate) jaccardCoefficient(userId1 string, userId2 string) float64 {
var (
similarity int = 0
rateInCommon int = 0
)
resultBothLike, _ := redis.Values(redisClient.Do("SINTER", this.cSet.userLiked(userId1), this.cSet.userLiked(userId2)))
resultBothDislike, _ := redis.Values(redisClient.Do("SINTER", this.cSet.userDisliked(userId1), this.cSet.userDisliked(userId2)))
resultUser1LikeUser2Dislike, _ := redis.Values(redisClient.Do("SINTER", this.cSet.userLiked(userId1), this.cSet.userDisliked(userId2)))
resultUser1DislikeUser2Like, _ := redis.Values(redisClient.Do("SINTER", this.cSet.userDisliked(userId1), this.cSet.userLiked(userId2)))
len1 := len(resultBothLike)
len2 := len(resultBothDislike)
len3 := len(resultUser1LikeUser2Dislike)
len4 := len(resultUser1DislikeUser2Like)
similarity = len1 + len2 - len3 - len4
rateInCommon = len1 + len2 + len3 + len4
return float64(similarity) / float64(rateInCommon)
}
func (this *algorithmsRate) updateRecommendationFor(userId string) error {
mostSimilarUserIds, err := redis.Values(redisClient.Do("ZREVRANGE", this.cSet.userSimilarity(userId), 0, MAX_NEIGHBORS-1))
if len(mostSimilarUserIds) == 0 {
return err
}
for _, rs := range mostSimilarUserIds {
similarUserId, _ := redis.String(rs, err)
redisClient.Do("SUNIONSTORE", this.cSet.userTemp(userId), this.cSet.userLiked(similarUserId))
}
diffItemIds, err := redis.Values(redisClient.Do("SDIFF", this.cSet.userTemp(userId), this.cSet.userLiked(userId), this.cSet.userDisliked(userId)))
for _, rs := range diffItemIds {
diffItemId, _ := redis.String(rs, err)
score := this.predictFor(userId, diffItemId)
redisClient.Do("ZADD", this.cSet.recommendedItem(userId), score, diffItemId)
}
redisClient.Do("DEL", this.cSet.userTemp(userId))
return err
}
func (this *algorithmsRate) predictFor(userId string, itemId string) float64 {
result1 := this.similaritySum(this.cSet.userSimilarity(userId), this.cSet.itemLiked(itemId))
result2 := this.similaritySum(this.cSet.userSimilarity(userId), this.cSet.itemDisliked(itemId))
sum := result1 - result2
itemLikedCount, _ := redis.Int(redisClient.Do("SCARD", this.cSet.itemLiked(itemId)))
itemDislikedCount, _ := redis.Int(redisClient.Do("SCARD", this.cSet.itemLiked(itemId)))
return float64(sum) / float64(itemLikedCount+itemDislikedCount)
}
// update socre
func (this *algorithms) updateWilsonScore(itemId string) error {
var (
total int
pOS float64
score float64 = 0.0
)
resultLike, _ := redis.Int(redisClient.Do("SCARD", this.cSet.itemLiked(itemId)))
resultDislike, _ := redis.Int(redisClient.Do("SCARD", this.cSet.itemDisliked(itemId)))
total = resultLike + resultDislike
if total > 0 {
pOS = float64(resultLike) / float64(total)
score = this.willsonScore(total, pOS)
}
_, err := redisClient.Do("ZADD", this.cSet.scoreRank, score, itemId)
return err
}
// willson score
func (this *algorithms) willsonScore(total int, pOS float64) float64 {
// 95%
var z float64 = 1.96
n := float64(total)
return math.Abs((pOS + z*z/(2*n) - z*math.Sqrt(pOS*(1-pOS)+z*z/(4*n))) / (1 + z*z/n))
}
func (this *algorithmsRate) updateAllData() error {
userIds, err := redis.Values(redisClient.Do("SMEMBERS", this.cSet.allUser))
for _, rs := range userIds {
userId, _ := redis.String(rs, err)
err = this.updateData(userId, "")
if err != nil {
break
}
}
return err
}
func (this *algorithmsRate) updateData(userId string, itemId string) error {
if err := this.updateSimilarityFor(userId); err != nil {
return err
}
if err := this.updateRecommendationFor(userId); err != nil {
return err
}
if itemId == "" {
ratedItemSet, err := redis.Values(redisClient.Do("SMEMBERS", this.cSet.userLiked(userId)))
for _, rs := range ratedItemSet {
ratedItemId, _ := redis.String(rs, err)
this.updateWilsonScore(ratedItemId)
}
} else {
if err := this.updateWilsonScore(itemId); err != nil {
return err
}
}
return nil
}