Class: FuzzyString::AdjustedScore

Inherits:
Object
  • Object
show all
Defined in:
lib/fuzzy_string/adjusted_score.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first, second) ⇒ AdjustedScore

Returns a new instance of AdjustedScore.



3
4
5
6
# File 'lib/fuzzy_string/adjusted_score.rb', line 3

def initialize(first,second)
  @first  = first
  @second = second
end

Class Method Details

.rank(first, second) ⇒ Object



2
# File 'lib/fuzzy_string/adjusted_score.rb', line 2

def self.rank(first,second) new(first,second).rank end

Instance Method Details

#adjusted_levenschtein_distanceObject



13
14
15
16
17
18
19
20
# File 'lib/fuzzy_string/adjusted_score.rb', line 13

def adjusted_levenschtein_distance
  pieces = @first.split(/#{@second.chars.to_a.join('(.*?)')}/i)
  score  = pieces[0][-1] == ' ' ? -1 : 0
  score += pieces[0..@second.length - 1].uniq == [''] ? -@first.length.to_f / 2 : 0
  score += (pieces[@second.length] || [])[0] == ' ' ? -@first.length.to_f / 2 : 0
  score += cost(pieces.shift,0.5) + cost(pieces.pop,0.25) + cost(pieces.join,1)
  score += FuzzyString::Levenshtein.distance(@first,@second)
end

#rankObject



7
8
9
10
11
12
# File 'lib/fuzzy_string/adjusted_score.rb', line 7

def rank
  return 0              if (@first == @second)
  return @second.length if (@first.length == 0)
  return @first.length  if (@second.length == 0)
  adjusted_levenschtein_distance
end