simple_recommender 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +34 -0
- data/lib/simple_recommender.rb +5 -0
- data/lib/simple_recommender/recommendable.rb +91 -0
- data/lib/simple_recommender/version.rb +3 -0
- data/lib/tasks/simple_recommender_tasks.rake +4 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/book.rb +10 -0
- data/test/dummy/app/models/like.rb +4 -0
- data/test/dummy/app/models/tag.rb +2 -0
- data/test/dummy/app/models/user.rb +3 -0
- data/test/dummy/app/views/layouts/application.html.erb +13 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/bin/setup +29 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +32 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +85 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +41 -0
- data/test/dummy/config/environments/production.rb +79 -0
- data/test/dummy/config/environments/test.rb +42 -0
- data/test/dummy/config/initializers/assets.rb +11 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/db/migrate/20170123001813_create_books.rb +9 -0
- data/test/dummy/db/migrate/20170123001819_create_users.rb +9 -0
- data/test/dummy/db/migrate/20170123001909_create_books_users.rb +11 -0
- data/test/dummy/db/migrate/20170123010745_enable_int_array_extension.rb +5 -0
- data/test/dummy/db/migrate/20170125030342_create_tags.rb +9 -0
- data/test/dummy/db/migrate/20170125030446_create_likes.rb +10 -0
- data/test/dummy/db/migrate/20170125032813_add_author_to_books.rb +5 -0
- data/test/dummy/db/migrate/20170125034331_create_books_tags.rb +8 -0
- data/test/dummy/db/migrate/20170125034341_remove_books_users.rb +5 -0
- data/test/dummy/db/schema.rb +63 -0
- data/test/dummy/log/development.log +511 -0
- data/test/dummy/log/test.log +5624 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/test/models/book_test.rb +77 -0
- data/test/dummy/test/models/like_test.rb +7 -0
- data/test/dummy/test/models/tag_test.rb +7 -0
- data/test/dummy/test/models/user_test.rb +7 -0
- data/test/fixtures/books.yml +29 -0
- data/test/fixtures/likes.yml +57 -0
- data/test/fixtures/tags.yml +19 -0
- data/test/fixtures/users.yml +19 -0
- data/test/simple_recommender_test.rb +7 -0
- data/test/test_helper.rb +20 -0
- metadata +199 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: de68a178eb1100f0d5245a71d0d2af980c462068
|
4
|
+
data.tar.gz: 7a79d4fc80f4926e1a255ac3d9b77bc0f1c9e910
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c31df3ffca819945788ff30a808413caf7ffc858b514748982dbb932c0a9077f73b91d30fc78b736cbd5b9f6bf69506f93cb6809f5d4b5be893ff3bcd3324e35
|
7
|
+
data.tar.gz: 481725920d711a3e1784fbf5e59fa90f54b205af08c7c0f87a835086394fe1481b82b6fd55999cba344c9b0f389e4c6cf24b2280f39d4062d62772d4adf35be1
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 Geoffrey Litt
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'SimpleRecommender'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'lib'
|
28
|
+
t.libs << 'test'
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
30
|
+
t.verbose = false
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
task default: :test
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module SimpleRecommender
|
2
|
+
module Recommendable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
SIMILARITY_KEY = "similarity" # todo: allow renaming to avoid conflicts
|
7
|
+
DEFAULT_N_RESULTS = 10
|
8
|
+
|
9
|
+
def similar_by(association_name, n_results: DEFAULT_N_RESULTS)
|
10
|
+
query = similar_query(
|
11
|
+
association_name: association_name,
|
12
|
+
n_results: n_results
|
13
|
+
)
|
14
|
+
|
15
|
+
self.class.find_by_sql(query)
|
16
|
+
end
|
17
|
+
|
18
|
+
# For each applicable association, add a dynamically named shortcut method
|
19
|
+
# e.g. when basing similarity on users, define :similar_by_users
|
20
|
+
self.reflect_on_all_associations.each do |association|
|
21
|
+
association_name = association.name
|
22
|
+
|
23
|
+
define_method "similar_by_#{association_name}" do |n_results: DEFAULT_N_RESULTS|
|
24
|
+
similar_by(association_name, n_results: n_results)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# Returns a Postgres query that can be executed to return similar items.
|
31
|
+
# Reflects on the association to get relevant table names, and then
|
32
|
+
# uses Postgres's integer array intersection/union operators to
|
33
|
+
# efficiently compute a Jaccard similarity metric between this item
|
34
|
+
# and all other items in the table.
|
35
|
+
def similar_query(association_name:, n_results:)
|
36
|
+
reflection = self.class.reflect_on_association(association_name)
|
37
|
+
|
38
|
+
if reflection.nil?
|
39
|
+
raise ArgumentError, "Could not find association #{association_name}"
|
40
|
+
end
|
41
|
+
|
42
|
+
metadata = association_metadata(reflection)
|
43
|
+
join_table = metadata[:join_table]
|
44
|
+
fkey = metadata[:foreign_key]
|
45
|
+
assoc_fkey = metadata[:association_foreign_key]
|
46
|
+
this_table = self.class.table_name
|
47
|
+
|
48
|
+
<<-SQL
|
49
|
+
WITH similar_items AS (
|
50
|
+
SELECT
|
51
|
+
t2.#{fkey},
|
52
|
+
(# (array_agg(DISTINCT t1.#{assoc_fkey}) & array_agg(DISTINCT t2.#{assoc_fkey})))::float/
|
53
|
+
(# (array_agg(DISTINCT t1.#{assoc_fkey}) | array_agg(DISTINCT t2.#{assoc_fkey})))::float as similarity
|
54
|
+
FROM #{join_table} AS t1, #{join_table} AS t2
|
55
|
+
WHERE t1.#{fkey} = #{id} and t2.#{fkey} != #{id}
|
56
|
+
GROUP BY t2.#{fkey}
|
57
|
+
ORDER BY similarity DESC
|
58
|
+
LIMIT #{n_results}
|
59
|
+
)
|
60
|
+
SELECT #{this_table}.*, similarity
|
61
|
+
FROM similar_items
|
62
|
+
JOIN #{this_table} ON #{this_table}.id = similar_items.#{fkey}
|
63
|
+
ORDER BY similarity DESC;
|
64
|
+
SQL
|
65
|
+
end
|
66
|
+
|
67
|
+
# Returns database metadata about an association based on its type,
|
68
|
+
# used in constructing a similarity query based on that association
|
69
|
+
def association_metadata(reflection)
|
70
|
+
case reflection
|
71
|
+
when ActiveRecord::Reflection::HasAndBelongsToManyReflection
|
72
|
+
AssociationMetadata.new(
|
73
|
+
reflection.join_table,
|
74
|
+
reflection.foreign_key,
|
75
|
+
reflection.association_foreign_key
|
76
|
+
)
|
77
|
+
when ActiveRecord::Reflection::ThroughReflection
|
78
|
+
AssociationMetadata.new(
|
79
|
+
reflection.through_reflection.table_name,
|
80
|
+
reflection.through_reflection.foreign_key,
|
81
|
+
reflection.association_foreign_key
|
82
|
+
)
|
83
|
+
else
|
84
|
+
raise ArgumentError, "Association '#{reflection.name}' is not a supported type"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
AssociationMetadata = Struct.new(:join_table, :foreign_key, :association_foreign_key)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/test/dummy/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
data/test/dummy/bin/rake
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
# path to your application root.
|
5
|
+
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
|
6
|
+
|
7
|
+
Dir.chdir APP_ROOT do
|
8
|
+
# This script is a starting point to setup your application.
|
9
|
+
# Add necessary setup steps to this file:
|
10
|
+
|
11
|
+
puts "== Installing dependencies =="
|
12
|
+
system "gem install bundler --conservative"
|
13
|
+
system "bundle check || bundle install"
|
14
|
+
|
15
|
+
# puts "\n== Copying sample files =="
|
16
|
+
# unless File.exist?("config/database.yml")
|
17
|
+
# system "cp config/database.yml.sample config/database.yml"
|
18
|
+
# end
|
19
|
+
|
20
|
+
puts "\n== Preparing database =="
|
21
|
+
system "bin/rake db:setup"
|
22
|
+
|
23
|
+
puts "\n== Removing old logs and tempfiles =="
|
24
|
+
system "rm -f log/*"
|
25
|
+
system "rm -rf tmp/cache"
|
26
|
+
|
27
|
+
puts "\n== Restarting application server =="
|
28
|
+
system "touch tmp/restart.txt"
|
29
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
# Pick the frameworks you want:
|
4
|
+
require "active_record/railtie"
|
5
|
+
require "action_controller/railtie"
|
6
|
+
require "action_mailer/railtie"
|
7
|
+
require "action_view/railtie"
|
8
|
+
# require "sprockets/railtie"
|
9
|
+
require "rails/test_unit/railtie"
|
10
|
+
|
11
|
+
Bundler.require(*Rails.groups)
|
12
|
+
require "simple_recommender"
|
13
|
+
|
14
|
+
module Dummy
|
15
|
+
class Application < Rails::Application
|
16
|
+
# Settings in config/environments/* take precedence over those specified here.
|
17
|
+
# Application configuration should go into files in config/initializers
|
18
|
+
# -- all .rb files in that directory are automatically loaded.
|
19
|
+
|
20
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
21
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
22
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
23
|
+
|
24
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
25
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
26
|
+
# config.i18n.default_locale = :de
|
27
|
+
|
28
|
+
# Do not swallow errors in after_commit/after_rollback callbacks.
|
29
|
+
config.active_record.raise_in_transactional_callbacks = true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# PostgreSQL. Versions 8.2 and up are supported.
|
2
|
+
#
|
3
|
+
# Install the pg driver:
|
4
|
+
# gem install pg
|
5
|
+
# On OS X with Homebrew:
|
6
|
+
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
|
7
|
+
# On OS X with MacPorts:
|
8
|
+
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
|
9
|
+
# On Windows:
|
10
|
+
# gem install pg
|
11
|
+
# Choose the win32 build.
|
12
|
+
# Install PostgreSQL and put its /bin directory on your path.
|
13
|
+
#
|
14
|
+
# Configure Using Gemfile
|
15
|
+
# gem 'pg'
|
16
|
+
#
|
17
|
+
default: &default
|
18
|
+
adapter: postgresql
|
19
|
+
encoding: unicode
|
20
|
+
# For details on connection pooling, see rails configuration guide
|
21
|
+
# http://guides.rubyonrails.org/configuring.html#database-pooling
|
22
|
+
pool: 5
|
23
|
+
|
24
|
+
development:
|
25
|
+
<<: *default
|
26
|
+
database: dummy_development
|
27
|
+
|
28
|
+
# The specified database role being used to connect to postgres.
|
29
|
+
# To create additional roles in postgres see `$ createuser --help`.
|
30
|
+
# When left blank, postgres will use the default role. This is
|
31
|
+
# the same name as the operating system user that initialized the database.
|
32
|
+
#username: dummy
|
33
|
+
|
34
|
+
# The password associated with the postgres role (username).
|
35
|
+
#password:
|
36
|
+
|
37
|
+
# Connect on a TCP socket. Omitted by default since the client uses a
|
38
|
+
# domain socket that doesn't need configuration. Windows does not have
|
39
|
+
# domain sockets, so uncomment these lines.
|
40
|
+
#host: localhost
|
41
|
+
|
42
|
+
# The TCP port the server listens on. Defaults to 5432.
|
43
|
+
# If your server runs on a different port number, change accordingly.
|
44
|
+
#port: 5432
|
45
|
+
|
46
|
+
# Schema search path. The server defaults to $user,public
|
47
|
+
#schema_search_path: myapp,sharedapp,public
|
48
|
+
|
49
|
+
# Minimum log levels, in increasing order:
|
50
|
+
# debug5, debug4, debug3, debug2, debug1,
|
51
|
+
# log, notice, warning, error, fatal, and panic
|
52
|
+
# Defaults to warning.
|
53
|
+
#min_messages: notice
|
54
|
+
|
55
|
+
# Warning: The database defined as "test" will be erased and
|
56
|
+
# re-generated from your development database when you run "rake".
|
57
|
+
# Do not set this db to the same as development or production.
|
58
|
+
test:
|
59
|
+
<<: *default
|
60
|
+
database: dummy_test
|
61
|
+
|
62
|
+
# As with config/secrets.yml, you never want to store sensitive information,
|
63
|
+
# like your database password, in your source code. If your source code is
|
64
|
+
# ever seen by anyone, they now have access to your database.
|
65
|
+
#
|
66
|
+
# Instead, provide the password as a unix environment variable when you boot
|
67
|
+
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
|
68
|
+
# for a full rundown on how to provide these environment variables in a
|
69
|
+
# production deployment.
|
70
|
+
#
|
71
|
+
# On Heroku and other platform providers, you may have a full connection URL
|
72
|
+
# available as an environment variable. For example:
|
73
|
+
#
|
74
|
+
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
|
75
|
+
#
|
76
|
+
# You can use this database configuration with:
|
77
|
+
#
|
78
|
+
# production:
|
79
|
+
# url: <%= ENV['DATABASE_URL'] %>
|
80
|
+
#
|
81
|
+
production:
|
82
|
+
<<: *default
|
83
|
+
database: dummy_production
|
84
|
+
username: dummy
|
85
|
+
password: <%= ENV['DUMMY_DATABASE_PASSWORD'] %>
|