coletivo-mongoid 0.0.3

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,70 @@
1
+ = coletivo-mongoid
2
+
3
+ A simple Rails 3 recommendations engine for Mongoid models.
4
+
5
+ Coletivo uses {Euclidean Distance}[http://en.wikipedia.org/wiki/Euclidean_distance] or {Pearson's Correlation Coefficient}[http://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient] to calculate the similarity between persons and their preferences.
6
+
7
+ This is a fork of [coletivo](https://github.com/diogenes/coletivo), if you need ActiveRecord support, check coletivo.
8
+
9
+ == Installation:
10
+
11
+ sudo gem install coletivo-mongoid
12
+
13
+ or in your rails app, add following line in Gemfile:
14
+
15
+ gem "coletivo-mongoid"
16
+
17
+ == Usage:
18
+
19
+ At your Rails model that represents a person (can be an _User_, _Member_, or something like that):
20
+
21
+ class User
22
+ include Mongoid::Document
23
+ include Coletivo::Models::Person
24
+ include Coletivo::Models::Recommendable
25
+ has_own_preferences
26
+
27
+ # ...
28
+ end
29
+
30
+ At your models that represent something that can be recommend (such as _Movie_):
31
+
32
+ class Movie
33
+ include Mongoid::Document
34
+ include Coletivo::Models::Person
35
+ include Coletivo::Models::Recommendable
36
+
37
+ # ...
38
+ end
39
+
40
+ So, a person can rate things:
41
+
42
+ current_user = User.create(:name => 'Diogenes')
43
+ movie = Movie.create(:name => 'The Tourist', :year => 2010)
44
+
45
+ current_user.rate!(movie, 4.5)
46
+
47
+ And after a lot of ratings... *recommendations*:
48
+
49
+ Movie.find_recommendations_for(current_user) # => movies and more movies...
50
+
51
+ By default, the similarity strategy used is Euclidean Distance, but you can change that passing the _strategy_ option:
52
+ Movie.find_recommendations_for(current_user, :strategy => :pearson)
53
+
54
+
55
+ == Contributing to coletivo-mongoid
56
+
57
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
58
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
59
+ * Fork the project
60
+ * Start a feature/bugfix branch
61
+ * Commit and push until you are happy with your contribution
62
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
63
+ * 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.
64
+
65
+ == Copyright
66
+
67
+ Copyright (c) 2011 Diógenes Falcão.
68
+ Copyright (c) 2012 Francis Chong.
69
+
70
+ See LICENSE.txt for further details.
data/lib/coletivo.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'active_model'
2
+ require 'active_record'
3
+ require 'active_support'
4
+
5
+ module Coletivo
6
+ module Models
7
+ autoload :Recommendable, 'coletivo/models/recommendable'
8
+ autoload :Person, 'coletivo/models/person'
9
+ autoload :PersonRating, 'coletivo/models/person_rating'
10
+ end
11
+
12
+ module Similarity
13
+ NO_SIMILARITY = -1.0..0.49
14
+ SIMILAR = 0.5..0.99
15
+ IDENTICAL = 1.0
16
+
17
+ autoload :BaseStrategy, 'coletivo/similarity/base_strategy'
18
+ autoload :EuclideanDistanceStrategy, 'coletivo/similarity/euclidean_distance_strategy'
19
+ autoload :PearsonCorrelationStrategy, 'coletivo/similarity/pearson_correlation_strategy'
20
+ autoload :Engine, 'coletivo/similarity/engine'
21
+ end
22
+
23
+ module Config
24
+ mattr_accessor :ratings_container
25
+
26
+ # Defaults
27
+ self.ratings_container = Coletivo::Models::PersonRating
28
+ end
29
+
30
+ if defined?(Rails)
31
+ require 'coletivo/rails/engine'
32
+ end
33
+ 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,27 @@
1
+ require 'mongoid'
2
+
3
+ module Coletivo
4
+ module Models
5
+ class PersonRating
6
+ include Mongoid::Document
7
+ include Mongoid::Timestamps
8
+
9
+ field :person_id, type: String
10
+ field :person_type, type: String
11
+
12
+ field :rateable_id, type: String
13
+ field :rateable_type, type: String
14
+
15
+ field :weight, type: BigDecimal, :precision => 5, :scale => 2
16
+
17
+ belongs_to :person, :polymorphic => true
18
+ belongs_to :rateable, :polymorphic => true
19
+
20
+ validates :person, :rateable, :weight, :presence => true
21
+
22
+ def self.find_for_recommendation(person, rateable_type)
23
+ where(:rateable_type => rateable_type.to_s)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,78 @@
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(person, options = {})
11
+ preferences = options[:preferences] ||=
12
+ load_preferences_for_recommendation(person)
13
+ top = predict_highest_ratings(person, preferences, options)
14
+ ids = top.collect(&:last)
15
+ where(:_id.in => ids).limit(options[:limit]).all
16
+ end
17
+
18
+ def map_ratings_to_preferences(ratings)
19
+ #TODO: (???) Item based mapping.
20
+ key, subkey = :person_id, :rateable_id
21
+ preferences = {}
22
+
23
+ ratings.each do |rating|
24
+ p = preferences[rating.send(key)] ||= {}
25
+ p[rating.send(subkey)] = rating.weight
26
+ end
27
+
28
+ preferences
29
+ end
30
+
31
+ def load_preferences_for_recommendation(person)
32
+ r = Coletivo::Config.ratings_container\
33
+ .find_for_recommendation(person, self)
34
+
35
+ map_ratings_to_preferences(r)
36
+ end
37
+
38
+ private
39
+
40
+ def predict_highest_ratings(person, people_preferences, options)
41
+ data = {}
42
+ people_preferences.each do |other, other_prefs|
43
+ next if other == person
44
+
45
+ sim = person.similarity_with(other, options)
46
+ next if sim <= 0
47
+
48
+ other_prefs.each do |item, weight|
49
+ unless people_preferences[person.id].keys.include?(item)
50
+ data[item] ||= {:total_similarity => 0.0, :weighted_mean => 0.0}
51
+ data[item][:total_similarity] += sim
52
+ data[item][:weighted_mean] += weight * sim
53
+ end
54
+ end
55
+ end
56
+
57
+ # e.g: [[5.35, "movie_2"], [2.0, "movie_4"]]
58
+ guessed_rating_and_id = Proc.new do |item, item_data|
59
+ [item_data[:weighted_mean] / item_data[:total_similarity], item]
60
+ end
61
+
62
+ # DESC sorting by weighted mean of ratings
63
+ data.collect(&guessed_rating_and_id).sort_by(&:first).reverse
64
+ end
65
+ end
66
+
67
+ module InstanceMethods
68
+ def similarity_with(other_id, options = {})
69
+ p = options[:preferences] ||
70
+ self.class.load_preferences_for_recommendation(self)
71
+
72
+ Coletivo::Similarity::Engine\
73
+ .similarity_between(self.id, other_id, p, options)
74
+ end
75
+ end
76
+ end # Recommendable
77
+ end # Models
78
+ end
@@ -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
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
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coletivo-mongoid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Francis Chong
9
+ - Diógenes Falcão
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-01-21 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ requirement: &70162405915120 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.7
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70162405915120
26
+ - !ruby/object:Gem::Dependency
27
+ name: shoulda
28
+ requirement: &70162405901680 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0.beta2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70162405901680
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ requirement: &70162405888800 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.0.14
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70162405888800
48
+ - !ruby/object:Gem::Dependency
49
+ name: jeweler
50
+ requirement: &70162405862020 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.6.2
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *70162405862020
59
+ - !ruby/object:Gem::Dependency
60
+ name: turn
61
+ requirement: &70162405857100 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *70162405857100
70
+ - !ruby/object:Gem::Dependency
71
+ name: mongoid
72
+ requirement: &70162405842760 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 2.0.0
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *70162405842760
81
+ - !ruby/object:Gem::Dependency
82
+ name: bson_ext
83
+ requirement: &70162405839780 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: *70162405839780
92
+ description: A simple Rails 3 recommendations engine using Mongoid models
93
+ email: francis@ignition.hk
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files:
97
+ - LICENSE.txt
98
+ - README.rdoc
99
+ files:
100
+ - lib/coletivo.rb
101
+ - lib/coletivo/models/person.rb
102
+ - lib/coletivo/models/person_rating.rb
103
+ - lib/coletivo/models/recommendable.rb
104
+ - lib/coletivo/rails/engine.rb
105
+ - lib/coletivo/similarity/base_strategy.rb
106
+ - lib/coletivo/similarity/engine.rb
107
+ - lib/coletivo/similarity/euclidean_distance_strategy.rb
108
+ - lib/coletivo/similarity/pearson_correlation_strategy.rb
109
+ - LICENSE.txt
110
+ - README.rdoc
111
+ homepage: http://github.com/siuying/coletivo-mongoid
112
+ licenses:
113
+ - MIT
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ segments:
125
+ - 0
126
+ hash: -2909253334946169336
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 1.8.10
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: A simple Rails 3 recommendations engine using Mongoid models
139
+ test_files: []