class ObjectSimilarity::ObjectScorer

Public Class Methods

new(type, object, field_scorers, field_weight, field_options = {}) click to toggle source
# File lib/object_similarity.rb, line 62
def initialize(type, object, field_scorers, field_weight, field_options = {})
  @type = type
  raise ArgumentError, "Invalid scoring type: #{@type.inspect}" if not respond_to?("#{@type}_score")
  @object = object

  @field_scorers = field_scorers.map do |field, scorer_definition|
    field_scorer_class(scorer_definition).new(field, object, field_weight[field], field_options[field] || {})
  end
end

Public Instance Methods

euclidean_distance_score(other_object) click to toggle source
# File lib/object_similarity.rb, line 76
def euclidean_distance_score(other_object)
  @field_scorers.inject(0) do |sum, scorer|
    sum += begin
      scorer.weighted_distance(other_object) ** 2
    rescue SkipException
      0 # Is this right?
    end
  end
end
normalized_euclidean_distance_score(other_object) click to toggle source
# File lib/object_similarity.rb, line 86
def normalized_euclidean_distance_score(other_object)
  euclidean_distance_score(other_object) / Math.sqrt(@field_scorers.size)
end
print_distances_report(other_object) click to toggle source
score(other_object) click to toggle source
# File lib/object_similarity.rb, line 72
def score(other_object)
  send("#{@type}_score", other_object)
end

Private Instance Methods

field_scorer_class(scorer_definition) click to toggle source
# File lib/object_similarity.rb, line 105
def field_scorer_class(scorer_definition)
  if scorer_definition.is_a?(FieldScorer)
    scorer_definition
  else
    partial_class_name = scorer_definition.to_s.capitalize.gsub(/_[a-z]/i) do |match|
      match[1].upcase
    end

    Kernel.const_get("::ObjectSimilarity::#{partial_class_name}FieldScorer")
  end
end