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(record1, record2) ⇒ Similarity

Returns a new instance of Similarity.



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

def initialize(record1, record2)
  @record1 = record1
  @record2 = record2
end

Instance Attribute Details

#record1Object (readonly)

Returns the value of attribute record1.



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

def record1
  @record1
end

#record2Object (readonly)

Returns the value of attribute record2.



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

def record2
  @record2
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(record1.clean, record2.clean)
end

#inspectObject



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

def inspect
  %{#{record2.clean.inspect} ~ #{record1.clean.inspect} => #{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 / (record1.clean.length * record2.clean.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 || 0) or
    ((record2.clean.length < 3 or needle.clean.length < 3) and best_score.levenshtein_similar > 0) or
    (threshold.nil? && (needle.words & record2.words).any?)
end