• Stars
    star
    123
  • Rank 290,145 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple recommendation engine for Rails/Postgres

simple_recommender

🔍 A quick and easy way to get "Users who liked X also liked Y" recommendations for related items in your Rails/Postgres application.

book = Book.find_by(name: "Harry Potter")

# Find the books most similar to Harry Potter, based on users' likes
book.similar_items
# => [#<Book id: 1840, name: "Twilight">,
      #<Book id: 1231, name: "Redwall">,
      #<Book id: 1455, name: "Lord of the Rings">...]

Why should I used this gem?

Unlike similar gems like predictor and recommendable that require you to track relationships between entities in Redis, simple_recommender uses the associations you already have in your Postgres database.

This means you don't have to maintain a separate copy of your data, and also don't incur the operational complexity of needing to use Redis. Hooray for simplicity!

For more background on the idea behind the gem, see this blog post.

But is it fast enough?

simple_recommender uses fast integer array operations built into Postgres, so it's fast enough to return realtime recommendations for many applications.

As a rough guideline based on benchmarking with a Heroku Standard 0 database: if you have under 100,000 records in your join table (e.g., less than 100,000 Likes in the above example), then simple_recommender can return queries within a couple hundred milliseconds.

If you're not sure if this is fast enough for your use case, I would recommend trying it out on your data; it's just a couple lines of code to get started.

If you know that you have way more data than that, I would recommend looking into one of the Redis-based gems that allow for offline precomputation. I'm also considering adding offline precomputation to this gem to allow for higher scale.

Getting started

Prerequisites

This gem requires:

Installation

Install the gem
  • Add the gem to your Gemfile: gem 'simple_recommender'

  • Run bundle install

Enable intarray

Next you'll need to enable the intarray extension in your Postgres database, which is used to compute recommendations.

  • Run rails g migration EnableIntArrayExtension

  • Replace the contents of the newly created migration file with the code below

class EnableIntArrayExtension < ActiveRecord::Migration
  def change
    enable_extension "intarray"
  end
end
  • Run rake db:migrate

Now you should have everything you need to get started!

Usage

You can add include SimpleRecommender::Recommendable to any ActiveRecord model, and then define an association to use for similarity matching.

For example, let's say you have Books in your app. Books are associated with Users through Likes. We want to say that two books are similar if they are liked by many of the same users.

It's as easy as adding two lines to your model:

class Book < ActiveRecord::Base
  has_many :likes
  has_many :users, through: :likes

  include SimpleRecommender::Recommendable
  similar_by :users
end

Now you can call similar_items to find similar books based on who liked them!

book.similar_items(n_results: 3)
# => [#<Book id: 1840, name: "Twilight">,
      #<Book id: 1231, name: "Redwall">,
      #<Book id: 1455, name: "Lord of the Rings">]

The items are returned in descending order of similarity. Each item also has a similarity method you can call to find out how similar the items were. 1.0 means the two items share exactly the same associations, and 0.0 means that there is no overlap at all.

book.similar_items(n_results: 3).map(&:similarity)
# => [0.5, 0.421, 0.334]

You can also decide how many results to return with the n_results parameter.

Roadmap

This gem is still in early development. Some changes I'm considering:

  • offline precomputation of recommendations for higher-volume datasets
  • similarity based on multiple associations combined with weights
  • "user-item" recommendation: recommend things to a user based on all of their items
  • recommendations based on numerical ratings rather than like/dislike
  • recommendations based on a weighted mix of various associations
  • MySQL support

More Repositories

1

wildcard

A browser extension for customizing web apps with a spreadsheet view
TypeScript
244
star
2

ladybug

🐞 Debug Ruby programs using Chrome Devtools
Ruby
41
star
3

mini-adapton

Typescript Implementation of mini-adapton (https://arxiv.org/pdf/1609.05337.pdf)
TypeScript
24
star
4

json-sheets

Riffs on spreadsheets for manipulating JSON and building GUIs
JavaScript
16
star
5

automerge-slate-playground

Playing with Automerge + Slate
TypeScript
8
star
6

ambsheet

tiny spreadsheet language with ambiguous values
JavaScript
8
star
7

meta-spreadsheet

a meta spreadsheet with pluggable coordinate systems
TypeScript
7
star
8

laser-scanner

Senior project for Yale Univ. B.S. Electrical Engineering & Computer Science (EENG 472)
Ruby
5
star
9

margin-notes-recorder

Ruby instrumentation library for recording function calls
Ruby
5
star
10

classroulette

A simple, serendipity-inducing interface to the Yale Bluebook course catalog
JavaScript
4
star
11

homepage

Code for my personal homepage http://www.geoffreylitt.com
HTML
4
star
12

margin-notes-ui

📝 Automatic code documentation with recorded examples from runtime
Vue
4
star
13

todomvc-vis

visualizing internal state of todomvc. final project for MIT 6.894 data vis class.
TeX
3
star
14

mentorme

TakeFlight - a video mentoring web application for the Arab world developed in 3 days, winner of the NYUAD Hackathon 2013.
Ruby
3
star
15

pastepals-client

the client half of a stupid idea: a universal clipboard, shared with everybody
HTML
3
star
16

terrible-toc

Terrible UIs for terms and conditions
HTML
2
star
17

make-real-experiments

playing with the tldraw "draw a ui" demo
TypeScript
2
star
18

tinysheet

a little spreadsheet in ocaml + bucklescript-tea
OCaml
2
star
19

little-spreadsheet

a little browser-based spreadsheet
JavaScript
1
star
20

miniwob-demo

Playing with creating new demos for OpenAI Mini World of Bits
JavaScript
1
star
21

carrier-bluebird

Prototyping a slower, batch-oriented approach to reading Twitter
Ruby
1
star
22

gdocs-chat-options

show options in gdocs chat
JavaScript
1
star
23

argus-analysis

Scripts for analyzing pedometer data from the Argus iOS app
R
1
star
24

denebre

Code for DenebRE.com (Deneb Renewable Energy), using nanoc
Ruby
1
star
25

advent-of-code-reasonml

Advent of Code 2019, in ReasonML
OCaml
1
star
26

Yale-Formula-Hybrid

Arduino code used in the electric-gas hybrid racecar built by the Yale Bulldogs racing team
C
1
star
27

astroboy-md

An AI chatbot that helps astronauts maintain mental health on long missions. Built for NASA SpaceApps 2016
CSS
1
star
28

automerge-test-app

just a little app to test automerge-repo and automerge-repo-react
TypeScript
1
star