Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmud Ridwan committed Jun 8, 2014
0 parents commit 0583d75
Show file tree
Hide file tree
Showing 10 changed files with 383 additions and 0 deletions.
27 changes: 27 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2014, The Go-strutils Authors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the {organization} nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Too

Too is a simple recommendation engine built on top of Redis in Go.

## Installation

Install Too using the go get command:

$ go get github.com/hjr265/too

The only dependencies are Go distribution and [Redis](http://redis.io).

## Documentation

- [Reference](http://godoc.org/github.com/hjr265/too)

## Contributing

Contributions are welcome.

## License

Too is available under the [BSD (3-Clause) License](http://opensource.org/licenses/BSD-3-Clause).
4 changes: 4 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright 2014 The Too Authors. All rights reserved.

// Package too provides a recommendation engine implementation built on top of Redis
package too
38 changes: 38 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2014 The Too Authors. All rights reserved.

package too

import (
"net"

"github.com/garyburd/redigo/redis"
)

type Engine struct {
c redis.Conn
class string

Likes Rater
Dislikes Rater

Similars Similars
Suggestions Suggestions
}

// New returns a new engine for given class connected to Redis server at addr.
func New(addr net.Addr, class string) (*Engine, error) {
c, err := redis.Dial(addr.Network(), addr.String())
if err != nil {
return nil, err
}

e := &Engine{
c: c,
class: class,
}
e.Likes = Rater{e, "likes"}
e.Dislikes = Rater{e, "dislikes"}
e.Similars = Similars{e}
e.Suggestions = Suggestions{e}
return e, nil
}
48 changes: 48 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2014 The Too Authors. All rights reserved.

package too_test

import (
"fmt"
"log"
"net"

"github.com/hjr265/too"
)

func ExampleEngine() {
redisAddr, _ := net.ResolveTCPAddr("tcp", ":6379")
te, err := too.New(redisAddr, "movies")
if err != nil {
log.Fatal(err)
}

te.Likes.Add("Sonic", "The Shawshank Redemption")
te.Likes.Add("Sonic", "The Godfather")
te.Likes.Add("Sonic", "The Dark Knight")
te.Likes.Add("Sonic", "Pulp Fiction")

te.Likes.Add("Mario", "The Godfather")
te.Likes.Add("Mario", "The Dark Knight")
te.Likes.Add("Mario", "The Shawshank Redemption")
te.Likes.Add("Mario", "The Prestige")
te.Likes.Add("Mario", "The Matrix")

te.Likes.Add("Peach", "The Godfather")
te.Likes.Add("Peach", "Inception")
te.Likes.Add("Peach", "Fight Club")
te.Likes.Add("Peach", "WALL·E")
te.Likes.Add("Peach", "Princess Mononoke")

te.Likes.Add("Luigi", "The Prestige")
te.Likes.Add("Luigi", "The Dark Knight")

items, _ := te.Suggestions.For("Luigi", 2)
for _, item := range items {
fmt.Println(item)
}

// Output:
// The Shawshank Redemption
// The Matrix
}
5 changes: 5 additions & 0 deletions item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2014 The Too Authors. All rights reserved.

package too

type Item string
51 changes: 51 additions & 0 deletions rater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2014 The Too Authors. All rights reserved.

package too

import (
"fmt"

"github.com/garyburd/redigo/redis"
)

type Rater struct {
e *Engine
kind string
}

// Add adds a rating by user for item
func (r Rater) Add(user User, item Item) error {
yes, err := redis.Bool(r.e.c.Do("SISMEMBER", fmt.Sprintf("%s:%s:%s", r.e.class, item, r.kind), user))
if err != nil {
return err
}

if !yes {
_, err = r.e.c.Do("ZINCRBY", fmt.Sprintf("%s:mosts:%s", r.e.class, r.kind), 1, item)
if err != nil {
return err
}
}

_, err = r.e.c.Do("SADD", fmt.Sprintf("%s:%s:%s", r.e.class, user, r.kind), item)
if err != nil {
return err
}

_, err = r.e.c.Do("SADD", fmt.Sprintf("%s:%s:%s", r.e.class, item, r.kind), user)
if err != nil {
return err
}

err = r.e.Similars.update(user)
if err != nil {
return err
}

err = r.e.Suggestions.update(user)
if err != nil {
return err
}

return nil
}
84 changes: 84 additions & 0 deletions similars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
98 changes: 98 additions & 0 deletions suggestions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.Likes.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
}
5 changes: 5 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2014 The Too Authors. All rights reserved.

package too

type User string

0 comments on commit 0583d75

Please sign in to comment.