Class: Commendo::MySqlBacked::WeightedGroup
- Inherits:
-
Object
- Object
- Commendo::MySqlBacked::WeightedGroup
- Defined in:
- lib/commendo/mysql-backed/weighted_group.rb
Instance Attribute Summary collapse
-
#content_sets ⇒ Object
Returns the value of attribute content_sets.
-
#key_base ⇒ Object
Returns the value of attribute key_base.
-
#mysql ⇒ Object
Returns the value of attribute mysql.
-
#tag_set ⇒ Object
Returns the value of attribute tag_set.
Instance Method Summary collapse
- #filtered_similar_to(resource, options = {}) ⇒ Object
-
#initialize(key_base, *content_sets) ⇒ WeightedGroup
constructor
A new instance of WeightedGroup.
- #similar_to(resource, limit = 0) ⇒ Object
Constructor Details
#initialize(key_base, *content_sets) ⇒ WeightedGroup
Returns a new instance of WeightedGroup.
8 9 10 11 12 13 |
# File 'lib/commendo/mysql-backed/weighted_group.rb', line 8 def initialize(key_base, *content_sets) config_hash = Commendo.config.to_hash @mysql = Mysql2::Client.new(config_hash) @key_base = key_base @content_sets = content_sets end |
Instance Attribute Details
#content_sets ⇒ Object
Returns the value of attribute content_sets.
6 7 8 |
# File 'lib/commendo/mysql-backed/weighted_group.rb', line 6 def content_sets @content_sets end |
#key_base ⇒ Object
Returns the value of attribute key_base.
6 7 8 |
# File 'lib/commendo/mysql-backed/weighted_group.rb', line 6 def key_base @key_base end |
#mysql ⇒ Object
Returns the value of attribute mysql.
6 7 8 |
# File 'lib/commendo/mysql-backed/weighted_group.rb', line 6 def mysql @mysql end |
#tag_set ⇒ Object
Returns the value of attribute tag_set.
6 7 8 |
# File 'lib/commendo/mysql-backed/weighted_group.rb', line 6 def tag_set @tag_set end |
Instance Method Details
#filtered_similar_to(resource, options = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/commendo/mysql-backed/weighted_group.rb', line 22 def filtered_similar_to(resource, = {}) if @tag_set.nil? || ([:include].nil? && [:exclude].nil?) return similar_to(resource, [:limit] || 0) else similar = similar_to(resource) limit = [:limit] || similar.length filtered = [] similar.each do |s| return filtered if filtered.length >= limit filtered << s if @tag_set.matches(s[:resource], [:include], [:exclude]) end return filtered end end |
#similar_to(resource, limit = 0) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/commendo/mysql-backed/weighted_group.rb', line 15 def similar_to(resource, limit = 0) similar = @content_sets.flat_map { |cs| cs[:cs].similar_to(resource).map { |s| {resource: s[:resource], similarity: (s[:similarity] * cs[:weight]).round(3)} } } grouped = similar.group_by { |r| r[:resource] } totaled_similar = grouped.map { |resource, similar| {resource: resource, similarity: similar.inject(0.0) { |sum, s| sum += s[:similarity] }} }.sort_by { |h| [h[:similarity], h[:resource]] }.reverse limit > 0 ? totaled_similar[0..limit-1] : totaled_similar end |