coletivo 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Diógenes Falcão
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/README.rdoc ADDED
@@ -0,0 +1,44 @@
1
+ = coletivo
2
+
3
+ An awesome, flexible, powerful, useful, tricky and liar Rails 3 recommendations engine.
4
+
5
+ == Installation:
6
+
7
+ sudo gem install coletivo
8
+ rails g coletivo
9
+ rake db:migrate
10
+
11
+ == Usage:
12
+
13
+ At your Rails model that represents a person (can be an _User_, _Member_, or something like that):
14
+
15
+ class User < ActiveRecord::Base
16
+ has_own_preferences
17
+
18
+ # ...
19
+ end
20
+
21
+ # ratings
22
+ current_user = User.create(:name => 'Diogenes')
23
+ movie = Movie.create(:name => 'The Tourist', :year => 2010)
24
+
25
+ current_user.rate!(movie, 4.5)
26
+
27
+ # after a lot of ratings... recommendations.
28
+ Movie.find_recommendations_for(current_user) # => movies and more movies...
29
+
30
+ == Contributing to coletivo
31
+
32
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
33
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
34
+ * Fork the project
35
+ * Start a feature/bugfix branch
36
+ * Commit and push until you are happy with your contribution
37
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
38
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
39
+
40
+ == Copyright
41
+
42
+ Copyright (c) 2011 Diógenes Falcão. See LICENSE.txt for
43
+ further details.
44
+
data/lib/coletivo.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'rails'
2
+ require 'active_model'
3
+ require 'active_record'
4
+ require 'active_support'
5
+
6
+ module Coletivo
7
+ module Models
8
+ autoload :Recommendable, 'coletivo/models/recommendable'
9
+ autoload :Person, 'coletivo/models/person'
10
+ autoload :PersonRating, 'coletivo/models/person_rating'
11
+ end
12
+
13
+ module Similarity
14
+ NO_SIMILARITY = -1.0..0.49
15
+ SIMILAR = 0.5..0.99
16
+ IDENTICAL = 1.0
17
+
18
+ autoload :BaseStrategy, 'coletivo/similarity/base_strategy'
19
+ autoload :EuclideanDistanceStrategy, 'coletivo/similarity/euclidean_distance_strategy'
20
+ autoload :PearsonCorrelationStrategy, 'coletivo/similarity/pearson_correlation_strategy'
21
+ autoload :Engine, 'coletivo/similarity/engine'
22
+ end
23
+
24
+ module Config
25
+ mattr_accessor :ratings_container
26
+
27
+ # Defaults
28
+ self.ratings_container = Coletivo::Models::PersonRating
29
+ end
30
+
31
+ if defined?(Rails)
32
+ require 'coletivo/rails/engine'
33
+ require 'coletivo/rails/active_record'
34
+ end
35
+ end
@@ -0,0 +1,27 @@
1
+ module Coletivo
2
+ module Models
3
+ module Person
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ # TODO: has_own_preferences doc.
10
+ def has_own_preferences(options = {})
11
+ self.send :include, InstanceMethods
12
+ end
13
+ end # ClassMethods
14
+
15
+ module InstanceMethods
16
+ def rate!(rateable, weight)
17
+ Coletivo::Config.ratings_container.create!({
18
+ :person => self,
19
+ :rateable => rateable,
20
+ :weight => weight
21
+ })
22
+ end
23
+ end # InstanceMethods
24
+
25
+ end # Person
26
+ end # Models
27
+ end
@@ -0,0 +1,14 @@
1
+ module Coletivo
2
+ module Models
3
+ class PersonRating < ActiveRecord::Base
4
+ belongs_to :person, :polymorphic => true
5
+ belongs_to :rateable, :polymorphic => true
6
+
7
+ validates :person, :rateable, :weight, :presence => true
8
+
9
+ def self.find_for_recommendation(recommendable)
10
+ find(:all)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,96 @@
1
+ module Coletivo
2
+ module Models
3
+ module Recommendable
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.send :include, InstanceMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def find_recommendations_for(model, options = {})
11
+ sim_totals, weighted_means = {}, {}
12
+ preferences = options[:preferences] ||
13
+ load_preferences_for_recommendation(model)
14
+
15
+ preferences.each do |other, other_prefs|
16
+ next if other == model
17
+
18
+ sim = model.similarity_with(other, options)
19
+ next if sim <= 0
20
+
21
+ other_prefs.each do |item, weight|
22
+ unless preferences[model.id].keys.include?(item)
23
+ sim_totals[item] ||= 0
24
+ weighted_means[item] ||= 0
25
+
26
+ sim_totals[item] += sim
27
+ weighted_means[item] += weight * sim
28
+ end
29
+ end
30
+ end
31
+
32
+ # DESC sort by weighted mean of ratings
33
+ # e.g: [[5.35, "movie_2"], [2.0, "movie_4"]]
34
+ top = weighted_means.collect { |i, mean| [mean/sim_totals[i], i] }\
35
+ .sort { |t_one, t_other| t_other <=> t_one }
36
+
37
+ ids = top.collect(&:last) # e.g: ['movie_2', 'movie_4']
38
+ models = where(:id => ids)
39
+
40
+ top.collect { |weight, item| models.detect {|m| m.id == item } }\
41
+ .compact
42
+ end
43
+
44
+ def find_ratings_for_recommendation(recommendable)
45
+ Coletivo::Config.ratings_container\
46
+ .find_for_recommendation(recommendable)
47
+ end
48
+
49
+ # Map a preferences Hash by a collection of ratings.
50
+ #
51
+ # Rating objects have a +person+, a +rateable+ object and a +weight+.
52
+ #
53
+ # The preferences can be mapped for _persons_, like that:
54
+ # {
55
+ # :person_1 => {:movie_1 => 2.0, :movie_2 => 5.0},
56
+ # :person_2 => {:movie_1 => 3.0, :movie_2 => 4.0}
57
+ # }
58
+ #
59
+ # Or for _rateable_ items, like that:
60
+ # {
61
+ # :movie_1 => {:person_1 => 2.0, :person_2 => 3.0},
62
+ # :movie_2 => {:person_1 => 5.0, :person_2 => 4.0}
63
+ # }
64
+ #
65
+ # Expected keys are :person or :rateable
66
+ def map_ratings_to_preferences(ratings)
67
+ #TODO: ?? #preferences_by_ratings(:rateable, :person): Item based rec.
68
+ key, subkey = :person, :rateable
69
+ preferences = {}
70
+
71
+ ratings.each do |rating|
72
+ p = preferences[rating.send(key).id] ||= {}
73
+ p[rating.send(subkey).id] = rating.weight
74
+ end
75
+
76
+ preferences
77
+ end
78
+
79
+ def load_preferences_for_recommendation(model)
80
+ r = model.class.find_ratings_for_recommendation(model)
81
+ model.class.map_ratings_to_preferences(r)
82
+ end
83
+ end
84
+
85
+ module InstanceMethods
86
+ def similarity_with(other_id, options = {})
87
+ p = options[:preferences] ||
88
+ self.class.load_preferences_for_recommendation(self)
89
+
90
+ Coletivo::Similarity::Engine\
91
+ .similarity_between(self.id, other_id, p, options)
92
+ end
93
+ end
94
+ end # Recommendable
95
+ end # Models
96
+ end
@@ -0,0 +1,2 @@
1
+ ActiveRecord::Base.send :include, Coletivo::Models::Person
2
+ ActiveRecord::Base.send :include, Coletivo::Models::Recommendable
@@ -0,0 +1,6 @@
1
+ require 'coletivo'
2
+
3
+ module Coletivo
4
+ class Engine < Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,25 @@
1
+ module Coletivo
2
+ module Similarity
3
+ class BaseStrategy
4
+ attr_accessor :preferences
5
+
6
+ def similarity_between(one, other)
7
+ raise "The #similarity_between was not implemented in #{self.class}"
8
+ end
9
+
10
+ def train_with(people_preferences)
11
+ @preferences = people_preferences
12
+ end
13
+
14
+ protected
15
+
16
+ def shared_items_between(one, other)
17
+ return [] unless preferences[one] && preferences[other]
18
+
19
+ preferences[one].keys.select { |item|
20
+ preferences[other].keys.include? item
21
+ }
22
+ end
23
+ end
24
+ end # Similarity
25
+ end # Coletivo
@@ -0,0 +1,22 @@
1
+ module Coletivo
2
+ module Similarity
3
+ class Engine
4
+ def self.similarity_between(one, other, preferences, options = {})
5
+ strategy = load_strategy options[:strategy]
6
+ strategy.train_with(preferences)
7
+
8
+ strategy.similarity_between(one, other)
9
+ end
10
+
11
+ protected
12
+
13
+ def self.load_strategy(key)
14
+ if :pearson == key
15
+ Coletivo::Similarity::PearsonCorrelationStrategy.new
16
+ else
17
+ Coletivo::Similarity::EuclideanDistanceStrategy.new
18
+ end
19
+ end
20
+ end # Engine
21
+ end # Similarity
22
+ end
@@ -0,0 +1,18 @@
1
+ module Coletivo
2
+ module Similarity
3
+ class EuclideanDistanceStrategy < BaseStrategy
4
+ def similarity_between(one, other)
5
+ shared = shared_items_between(one, other)
6
+
7
+ return 0 if shared.empty?
8
+
9
+ sum_of_squares = shared.inject(0.0) { |sum, item|
10
+ sum + (preferences[one][item] - preferences[other][item]) ** 2
11
+ }
12
+
13
+ 1 / (1 + sum_of_squares)
14
+ end
15
+ end
16
+ end
17
+ end # Similarity
18
+
@@ -0,0 +1,35 @@
1
+ module Coletivo
2
+ module Similarity
3
+ class PearsonCorrelationStrategy < BaseStrategy
4
+ def similarity_between(one, other)
5
+ shared = shared_items_between(one, other)
6
+ prefs_one = preferences[one]
7
+ prefs_other = preferences[other]
8
+
9
+ return 0 if shared.empty?
10
+
11
+ sum_prefs_one = sum_prefs_other = sum_squares_one = \
12
+ sum_squares_other = p_sum = 0.0
13
+
14
+ shared.each { |item|
15
+ sum_prefs_one += prefs_one[item]
16
+ sum_prefs_other += prefs_other[item]
17
+ sum_squares_one += prefs_one[item] ** 2
18
+ sum_squares_other += prefs_other[item] ** 2
19
+ p_sum += prefs_one[item] * prefs_other[item]
20
+ }
21
+
22
+ total_shared = shared.size
23
+
24
+ numerator = p_sum - (sum_prefs_one * sum_prefs_other / total_shared)
25
+
26
+ den_one = sum_squares_one - (sum_prefs_one ** 2) / total_shared
27
+ den_other = sum_squares_other - (sum_prefs_other ** 2) / total_shared
28
+
29
+ denominator = Math.sqrt(den_one * den_other)
30
+
31
+ denominator == 0 ? 0 : numerator / denominator
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class ColetivoGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ def self.source_root
8
+ File.join(File.dirname(__FILE__), 'templates')
9
+ end
10
+
11
+ # Implement the required interface for Rails::Generators::Migration.
12
+ def self.next_migration_number(dirname) #:nodoc:
13
+ if ActiveRecord::Base.timestamped_migrations
14
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
15
+ else
16
+ "%.3d" % (current_migration_number(dirname) + 1)
17
+ end
18
+ end
19
+
20
+ def create_migration_file
21
+ migration_template 'person_ratings_migration.rb',
22
+ 'db/migrate/create_person_ratings.rb'
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ class CreatePersonRatings < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :person_ratings do |t|
4
+ t.integer :person_id
5
+ t.string :person_type
6
+
7
+ t.integer :rateable_id
8
+ t.string :rateable_type
9
+
10
+ t.decimal :weight, :precision => 5, :scale => 2
11
+
12
+ t.timestamps
13
+ end
14
+ end
15
+
16
+ def self.down
17
+ drop_table :person_ratings
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coletivo
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - "Di\xC3\xB3genes Falc\xC3\xA3o"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-08 00:00:00 -03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ name: rails
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 9
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 7
34
+ version: 3.0.7
35
+ requirement: *id001
36
+ type: :runtime
37
+ - !ruby/object:Gem::Dependency
38
+ prerelease: false
39
+ name: shoulda
40
+ version_requirements: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: -1848230022
46
+ segments:
47
+ - 3
48
+ - 0
49
+ - 0
50
+ - beta2
51
+ version: 3.0.0.beta2
52
+ requirement: *id002
53
+ type: :development
54
+ - !ruby/object:Gem::Dependency
55
+ prerelease: false
56
+ name: bundler
57
+ version_requirements: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ hash: 11
63
+ segments:
64
+ - 1
65
+ - 0
66
+ - 14
67
+ version: 1.0.14
68
+ requirement: *id003
69
+ type: :development
70
+ - !ruby/object:Gem::Dependency
71
+ prerelease: false
72
+ name: jeweler
73
+ version_requirements: &id004 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ hash: 11
79
+ segments:
80
+ - 1
81
+ - 6
82
+ - 2
83
+ version: 1.6.2
84
+ requirement: *id004
85
+ type: :development
86
+ - !ruby/object:Gem::Dependency
87
+ prerelease: false
88
+ name: turn
89
+ version_requirements: &id005 !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirement: *id005
99
+ type: :development
100
+ description: An awesome, flexible, powerful, useful, tricky and liar Rails 3 recommendations engine.
101
+ email: diogenes.araujo@gmail.com
102
+ executables: []
103
+
104
+ extensions: []
105
+
106
+ extra_rdoc_files:
107
+ - LICENSE.txt
108
+ - README.rdoc
109
+ files:
110
+ - lib/coletivo.rb
111
+ - lib/coletivo/models/person.rb
112
+ - lib/coletivo/models/person_rating.rb
113
+ - lib/coletivo/models/recommendable.rb
114
+ - lib/coletivo/rails/active_record.rb
115
+ - lib/coletivo/rails/engine.rb
116
+ - lib/coletivo/similarity/base_strategy.rb
117
+ - lib/coletivo/similarity/engine.rb
118
+ - lib/coletivo/similarity/euclidean_distance_strategy.rb
119
+ - lib/coletivo/similarity/pearson_correlation_strategy.rb
120
+ - lib/generators/coletivo/coletivo_generator.rb
121
+ - lib/generators/coletivo/templates/person_ratings_migration.rb
122
+ - LICENSE.txt
123
+ - README.rdoc
124
+ has_rdoc: true
125
+ homepage: http://github.com/diogenes/coletivo
126
+ licenses:
127
+ - MIT
128
+ post_install_message:
129
+ rdoc_options: []
130
+
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ hash: 3
139
+ segments:
140
+ - 0
141
+ version: "0"
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ hash: 3
148
+ segments:
149
+ - 0
150
+ version: "0"
151
+ requirements: []
152
+
153
+ rubyforge_project:
154
+ rubygems_version: 1.3.7
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: An awesome, flexible, powerful, useful, tricky and liar Rails 3 recommendations engine.
158
+ test_files: []
159
+