-
Notifications
You must be signed in to change notification settings - Fork 11
/
suggestions.go
98 lines (80 loc) · 2.5 KB
/
suggestions.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
// Copyright 2014 The Too Authors. All rights reserved.
package too
import (
"fmt"
"github.com/garyburd/redigo/redis"
)
type Suggestions struct {
e *Engine
}
// For returns n suggested items for user
func (s Suggestions) For(user User, n int) ([]Item, error) {
results, err := redis.Strings(s.e.c.Do("ZREVRANGE", fmt.Sprintf("%s:%s:%s", s.e.class, user, "suggestions"), 0, n-1))
if err != nil && err != redis.ErrNil {
return nil, err
}
items := []Item{}
for _, item := range results {
items = append(items, Item(item))
}
return items, nil
}
func (s Suggestions) update(user User) error {
similars, err := s.e.Similars.Of(user, 8)
if err != nil {
return err
}
if len(similars) == 0 {
return nil
}
args := []interface{}{fmt.Sprintf("%s:%s:_tmp", s.e.class, user)}
for _, similar := range similars {
args = append(args, fmt.Sprintf("%s:%s:%s", s.e.class, similar, s.e.Likes.kind))
}
_, err = s.e.c.Do("SUNIONSTORE", args...)
if err != nil {
return err
}
defer s.e.c.Do("DEL", fmt.Sprintf("%s:%s:_tmp", s.e.class, user))
items, err := redis.Strings(s.e.c.Do("SDIFF", fmt.Sprintf("%s:%s:_tmp", s.e.class, user), fmt.Sprintf("%s:%s:%s", s.e.class, user, s.e.Likes.kind), fmt.Sprintf("%s:%s:%s", s.e.class, user, s.e.Dislikes.kind)))
if err != nil && err != redis.ErrNil {
return err
}
scores := map[string]float64{}
for _, item := range items {
likers, err := redis.Strings(s.e.c.Do("SMEMBERS", fmt.Sprintf("%s:%s:%s", s.e.class, item, s.e.Likes.kind)))
if err != nil && err != redis.ErrNil {
return err
}
for _, liker := range likers {
score, err := redis.Float64(s.e.c.Do("ZSCORE", fmt.Sprintf("%s:%s:similars", s.e.class, user), liker))
if err != nil && err != redis.ErrNil {
return err
}
scores[item] += score
}
dislikers, err := redis.Strings(s.e.c.Do("SMEMBERS", fmt.Sprintf("%s:%s:%s", s.e.class, item, s.e.Dislikes.kind)))
if err != nil && err != redis.ErrNil {
return err
}
for _, disliker := range dislikers {
score, err := redis.Float64(s.e.c.Do("ZSCORE", fmt.Sprintf("%s:%s:similars", s.e.class, user), disliker))
if err != nil && err != redis.ErrNil {
return err
}
scores[item] -= score
}
total := len(likers) + len(dislikers)
if total > 0 {
scores[item] /= float64(total)
}
}
_, err = s.e.c.Do("DEL", fmt.Sprintf("%s:%s:suggestions", s.e.class, user))
if err != nil {
return err
}
for _, item := range items {
s.e.c.Do("ZADD", fmt.Sprintf("%s:%s:suggestions", s.e.class, user), scores[item], item)
}
return nil
}