All Projects → geoffreylitt → Simple_recommender

geoffreylitt / Simple_recommender

Licence: mit
A simple recommendation engine for Rails/Postgres

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Simple recommender

Reactive record
Generate ActiveRecord models for a pre-existing Postgres db
Stars: ✭ 132 (+30.69%)
Mutual labels:  postgres, rails
Pg party
ActiveRecord PostgreSQL Partitioning
Stars: ✭ 294 (+191.09%)
Mutual labels:  postgres, rails
Flipper
🐬 Beautiful, performant feature flags for Ruby.
Stars: ✭ 2,732 (+2604.95%)
Mutual labels:  postgres, rails
Calculate All
calculate_all method for aggregate functions in Active Record
Stars: ✭ 118 (+16.83%)
Mutual labels:  postgres, rails
Activerecordextended
Adds additional postgres functionality to an ActiveRecord / Rails application
Stars: ✭ 830 (+721.78%)
Mutual labels:  postgres, rails
Activerecord Postgres enum
Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.
Stars: ✭ 227 (+124.75%)
Mutual labels:  postgres, rails
Scenic
Scenic is maintained by Derek Prior, Caleb Hearth, and you, our contributors.
Stars: ✭ 2,856 (+2727.72%)
Mutual labels:  postgres, rails
Doctor
Doctor is a documentation server for your docs in github
Stars: ✭ 391 (+287.13%)
Mutual labels:  postgres, rails
Rein
Database constraints made easy for ActiveRecord.
Stars: ✭ 657 (+550.5%)
Mutual labels:  postgres, rails
Zero downtime migrations
Zero downtime migrations with ActiveRecord 3+ and PostgreSQL
Stars: ✭ 513 (+407.92%)
Mutual labels:  postgres, rails
Ar Uuid
Override migration methods to support UUID columns without having to be explicit about it.
Stars: ✭ 41 (-59.41%)
Mutual labels:  postgres, rails
Niklick
Rails Versioned API solution template for hipsters! (Ruby, Ruby on Rails, REST API, GraphQL, Docker, RSpec, Devise, Postgress DB)
Stars: ✭ 39 (-61.39%)
Mutual labels:  postgres, rails
Activerecord Clean Db Structure
Automatic cleanup for the Rails db/structure.sql file (ActiveRecord/PostgreSQL)
Stars: ✭ 101 (+0%)
Mutual labels:  postgres, rails
Teleport
Certificate authority and access plane for SSH, Kubernetes, web apps, databases and desktops
Stars: ✭ 10,602 (+10397.03%)
Mutual labels:  postgres
Instagram api gem
A Ruby wrapper for the Instagram API
Stars: ✭ 100 (-0.99%)
Mutual labels:  rails
Lol dba
lol_dba is a small package of rake tasks that scan your application models and displays a list of columns that probably should be indexed. Also, it can generate .sql migration scripts.
Stars: ✭ 1,363 (+1249.5%)
Mutual labels:  rails
Yoke
Postgres high-availability cluster with auto-failover and automated cluster recovery.
Stars: ✭ 1,360 (+1246.53%)
Mutual labels:  postgres
Go Structured Query
Type safe SQL query builder and struct mapper for Go
Stars: ✭ 101 (+0%)
Mutual labels:  postgres
Nancy
Fully automated database experiments. THIS IS A MIRROR OF https://gitlab.com/postgres.ai/nancy
Stars: ✭ 100 (-0.99%)
Mutual labels:  postgres
Yabeda Rails
Yabeda plugin to collect basic metrics for Rails applications
Stars: ✭ 99 (-1.98%)
Mutual labels:  rails

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!

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].