-
Notifications
You must be signed in to change notification settings - Fork 1
/
output.go
72 lines (63 loc) · 2.02 KB
/
output.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
package gocommend
import "github.com/garyburd/redigo/redis"
// output, get data what you want
type Output struct {
recNum int
cSet collectionSet
}
// init the params and set the cSet
func (this *Output) Init(collection string, recNum int) error {
if collection == "" {
return gocommendError{emptyCollection}
}
this.recNum = recNum - 1
this.cSet = collectionSet{}
this.cSet.init(collection)
return nil
}
// convert interface slice to string slice
func (this *Output) toStrings(arrayInterface []interface{}) (strings []string) {
for _, rs := range arrayInterface {
s, _ := redis.String(rs, nil)
strings = append(strings, s)
}
return
}
// get recommend items for user
func (this *Output) RecommendItemForUser(userId string) ([]string, error) {
arrayInterface, err := redis.Values(redisClient.Do("ZREVRANGE", this.cSet.recommendedItem(userId), 0, this.recNum))
if err != nil {
return nil, err
}
return this.toStrings(arrayInterface), err
}
// get recommend items by item similarty
func (this *Output) RecommendItemForItem(itemId string) ([]string, error) {
arrayInterface, err := redis.Values(redisClient.Do("ZREVRANGE", this.cSet.itemSimilarity(itemId), 0, this.recNum))
if err != nil {
return nil, err
}
return this.toStrings(arrayInterface), err
}
// get the best rated items
func (this *Output) BestRated() ([]string, error) {
arrayInterface, err := redis.Values(redisClient.Do("ZREVRANGE", this.cSet.scoreRank, 0, this.recNum))
if err != nil {
return nil, err
}
return this.toStrings(arrayInterface), err
}
func (this *Output) MostLiked() ([]string, error) {
arrayInterface, err := redis.Values(redisClient.Do("ZREVRANGE", this.cSet.mostLiked, 0, this.recNum))
if err != nil {
return nil, err
}
return this.toStrings(arrayInterface), err
}
func (this *Output) MostSimilarUsers(userId string) ([]string, error) {
arrayInterface, err := redis.Values(redisClient.Do("ZREVRANGE", this.cSet.userSimilarity(userId), 0, this.recNum))
if err != nil {
return nil, err
}
return this.toStrings(arrayInterface), err
}