-
Notifications
You must be signed in to change notification settings - Fork 11
/
similars.go
84 lines (69 loc) · 2.59 KB
/
similars.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
// Copyright 2014 The Too Authors. All rights reserved.
package too
import (
"fmt"
"github.com/garyburd/redigo/redis"
)
type Similars struct {
e *Engine
}
// Of returns n users similar to user
func (s Similars) Of(user User, n int) ([]User, error) {
results, err := redis.Strings(s.e.c.Do("ZREVRANGE", fmt.Sprintf("%s:%s:%s", s.e.class, user, "similars"), 0, n-1))
if err != nil && err != redis.ErrNil {
return nil, err
}
users := []User{}
for _, user := range results {
users = append(users, User(user))
}
return users, nil
}
// Jaccard returns the Jaccard coefficient between user and other
func (s Similars) Jaccard(user, other User) (float64, error) {
likes, err := redis.Strings(s.e.c.Do("SINTER", fmt.Sprintf("%s:%s:%s", s.e.class, user, s.e.Likes.kind), fmt.Sprintf("%s:%s:%s", s.e.class, other, s.e.Likes.kind)))
if err != nil && err != redis.ErrNil {
return 0, err
}
dislikes, err := redis.Strings(s.e.c.Do("SINTER", fmt.Sprintf("%s:%s:%s", s.e.class, user, s.e.Dislikes.kind), fmt.Sprintf("%s:%s:%s", s.e.class, other, s.e.Dislikes.kind)))
if err != nil && err != redis.ErrNil {
return 0, err
}
antiLikes, err := redis.Strings(s.e.c.Do("SINTER", fmt.Sprintf("%s:%s:%s", s.e.class, user, s.e.Likes.kind), fmt.Sprintf("%s:%s:%s", s.e.class, other, s.e.Dislikes.kind)))
if err != nil && err != redis.ErrNil {
return 0, err
}
antiDislikes, err := redis.Strings(s.e.c.Do("SINTER", fmt.Sprintf("%s:%s:%s", s.e.class, user, s.e.Dislikes.kind), fmt.Sprintf("%s:%s:%s", s.e.class, other, s.e.Likes.kind)))
if err != nil && err != redis.ErrNil {
return 0, err
}
return float64(len(likes)+len(dislikes)-len(antiLikes)-len(antiDislikes)) / float64(len(likes)+len(dislikes)+len(antiLikes)+len(antiDislikes)), nil
}
func (s Similars) update(user User) error {
items, err := redis.Strings(s.e.c.Do("SUNION", 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
}
args := []interface{}{}
for _, item := range items {
args = append(args, fmt.Sprintf("%s:%s:%s", s.e.class, item, s.e.Likes.kind))
args = append(args, fmt.Sprintf("%s:%s:%s", s.e.class, item, s.e.Dislikes.kind))
}
users, err := redis.Strings(s.e.c.Do("SUNION", args...))
if err != nil && err != redis.ErrNil {
return err
}
for _, other := range users {
if other != string(user) {
v, err := s.Jaccard(user, User(other))
if err != nil {
return err
}
_, err = s.e.c.Do("ZADD", fmt.Sprintf("%s:%s:similars", s.e.class, user), v, other)
if err != nil {
return err
}
}
}
return nil
}