recommender

package module
v0.0.0-...-060295a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2016 License: MIT Imports: 6 Imported by: 0

README

Recommender

Recommendation engine written in Go.

This repository is intended to be an exercise, not a package for any production code.

Why?

I've built this project to learn about, among other things, recommendation basics, concurrency patterns, testing techniques, and key/value persistent storage. Should you browse the code, you'll find a few examples of each.

Examples

Although the best examples are documented in the test package, here are some brief examples of how the system works:

// Create a user
niko := recommender.NewUser("Niko Kovacevic")

// Create items
flagstaff := recommender.NewItem("Flagstaff, Arizona")
losAngeles := recommender.NewItem("Los Angeles, California")

// Create a recommender
r := recommender.NewRecommender()

// Rate items
r.Like(niko, flagstaff)
r.Dislike(niko, losAngeles)

// Updating happens automatically upon rating an item.
// Get some suggestions!
suggestions, err = r.GetSuggestions(niko)
if err != nil {
  return err
}

If other people rated your items, as well as some other items, have a look at the suggestions, scored from -1 to 1.

{Portland, Oregon 0.38888893}
{San Francisco, California -0.1111111}
{Sacramento, California -0.11111113}
{Princeton, New Jersey -1}
{Austin, Texas 0.5}
{Philadelphia, Pennsylvania -1}
{Ashland, Oregon 0.44444445}
{Tacoma, Washington 0.3333333}
{Houston, Texas -0.5}
{Tucson, Arizona 1}
{New York, New York 0}
{Santa Fe, New Mexico 0.8333334}
{Portland, Maine -0.3333333}

Next

I haven't determined whether or not to extend the project by building a front-end. Were I to go that direction, I'd likely build a React application with OAuth (perhaps leveraging Auth0) to let users sign in and rate cities or movies via a Go API, which would leverage this package.

Acknowlegements

License

The MIT License (MIT)
Copyright (c) 2016 Niko Kovacevic

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	Id   string `json:"id"`
	Name string `json:"name"`
}

func NewItem

func NewItem(name string) *Item

NewItem creates and returns an Item

func (Item) String

func (i Item) String() string

String represents an Item as a string

type Rating

type Rating struct {
	Item  Item  `json:"item"`
	Score Score `json:"score"`
}

func (Rating) String

func (r Rating) String() string

String represents an Item as a string

type Recommender

type Recommender struct {
	// contains filtered or unexported fields
}

func NewRecommender

func NewRecommender() (*Recommender, error)

NewRecommender returns a new Recommender. The database is opened and buckets are created.

func (*Recommender) Close

func (r *Recommender) Close()

Close closes the Recommender's store connection. Deferring a call to this method is recommended on creation of a Recommender.

func (*Recommender) Dislike

func (r *Recommender) Dislike(user *User, item *Item) error

Dislike records a user disliking an item. If the user already dislikes the item, nothing happens. If the user likes the item, the like is removed first. Only if the recording fails will this return an error.

func (*Recommender) GetDislikedItems

func (r *Recommender) GetDislikedItems(user *User) (map[string]Item, error)

GetDislikedItems gets Items disliked by the given User.

func (*Recommender) GetItems

func (r *Recommender) GetItems(startAt int, count int) ([]Item, error)

GetItems retrieves a collection of Items.

func (*Recommender) GetLikedItems

func (r *Recommender) GetLikedItems(user *User) (map[string]Item, error)

GetLikedItems gets Items liked by the given User.

func (*Recommender) GetRatingNeighbors

func (r *Recommender) GetRatingNeighbors(user *User) (map[string]User, error)

GetRatingNeighbors returns a set of users, indexed by user ID, who rated the same items that the given user rated.

func (*Recommender) GetRatings

func (r *Recommender) GetRatings(user *User) (map[string]Rating, error)

GetRatings retrieves all items a user has rated and returns a map of item ID to Rating, which includes the item and the score the user gave.

func (*Recommender) GetSimilarity

func (r *Recommender) GetSimilarity(user *User) (map[string]Similarity, error)

GetSimilarity returns a map the given user's similarities, keyed by their similar user's ID

func (*Recommender) GetSuggestions

func (r *Recommender) GetSuggestions(user *User) (map[string]Suggestion, error)

GetSuggestions retrieves the set of Suggestions for the given user.

func (*Recommender) GetUsers

func (r *Recommender) GetUsers(startAt int, count int) ([]User, error)

GetUsers retrieves a collection of Users.

func (*Recommender) GetUsersWhoDislike

func (r *Recommender) GetUsersWhoDislike(item *Item) (map[string]User, error)

GetUsersWhoDislike retrieves the collection of users who dislike the given Item.

func (*Recommender) GetUsersWhoLike

func (r *Recommender) GetUsersWhoLike(item *Item) (map[string]User, error)

GetUsersWhoLike retrieves the collection of users who like the given Item.

func (*Recommender) GetUsersWhoRated

func (r *Recommender) GetUsersWhoRated(item *Item) (map[string]User, error)

GetUsersWhoRated retrieves the collection of users who rated the given Item.

func (*Recommender) Like

func (r *Recommender) Like(user *User, item *Item) error

Like records a user liking an item. If the user already likes the item, nothing happens. Only if the recording fails will this return an error.

func (*Recommender) UpdateSimilarity

func (r *Recommender) UpdateSimilarity(user *User) error

UpdateSimilarity calculates the similarity index for each user with which the given user has overlapping rated items.

func (*Recommender) UpdateSuggestions

func (r *Recommender) UpdateSuggestions(user *User) error

UpdateSuggestions generates a set of Suggestions (items with corresponding suggestion index) for the given user.

type Score

type Score int

type Similarity

type Similarity struct {
	User  User            `json:"user"`
	Index SimilarityIndex `json:"index"`
}

type SimilarityIndex

type SimilarityIndex float32

type Suggestion

type Suggestion struct {
	Item  Item            `json:"item"`
	Index SuggestionIndex `json:"index"`
}

type SuggestionIndex

type SuggestionIndex float32

type User

type User struct {
	Id      string            `json:"id"`
	Name    string            `json:"name"`
	Ratings map[string]Rating `json:"ratings"`
}

func NewUser

func NewUser(name string) *User

NewUser creates and returns a new User

func (User) String

func (u User) String() string

String represents a User as a string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL