Class: FuzzyMatch::Similarity

Inherits:
Object
  • Object
show all
Defined in:
lib/fuzzy_match/similarity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wrapper1, wrapper2) ⇒ Similarity

Returns a new instance of Similarity.



6
7
8
9
# File 'lib/fuzzy_match/similarity.rb', line 6

def initialize(wrapper1, wrapper2)
  @wrapper1 = wrapper1
  @wrapper2 = wrapper2
end

Instance Attribute Details

#wrapper1Object (readonly)

Returns the value of attribute wrapper1.



3
4
5
# File 'lib/fuzzy_match/similarity.rb', line 3

def wrapper1
  @wrapper1
end

#wrapper2Object (readonly)

Returns the value of attribute wrapper2.



4
5
6
# File 'lib/fuzzy_match/similarity.rb', line 4

def wrapper2
  @wrapper2
end

Instance Method Details

#<=>(other) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/fuzzy_match/similarity.rb', line 11

def <=>(other)
  by_score = best_score <=> other.best_score
  if by_score == 0
    original_weight <=> other.original_weight
  else
    by_score
  end
end

#best_scoreObject



20
21
22
# File 'lib/fuzzy_match/similarity.rb', line 20

def best_score
  @best_score ||= FuzzyMatch.score_class.new(best_wrapper1_variant, best_wrapper2_variant)
end

#inspectObject



30
31
32
# File 'lib/fuzzy_match/similarity.rb', line 30

def inspect
  %{#<FuzzyMatch::Similarity #{wrapper2.render.inspect}=>#{best_wrapper2_variant.inspect} versus #{wrapper1.render.inspect}=>#{best_wrapper1_variant.inspect} original_weight=#{"%0.5f" % original_weight} best_score=#{best_score.inspect}>}
end

#original_weightObject

Weight things towards short original strings



35
36
37
# File 'lib/fuzzy_match/similarity.rb', line 35

def original_weight
  @original_weight ||= (1.0 / (wrapper1.render.length * wrapper2.render.length))
end

#satisfy?(needle, threshold) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/fuzzy_match/similarity.rb', line 24

def satisfy?(needle, threshold)
  best_score.dices_coefficient_similar > threshold or
    ((wrapper2.render.length < 3 or needle.render.length < 3) and best_score.levenshtein_similar > 0) or
    (needle.words & wrapper2.words).any?
end