Class: FuzzyString::AdjustedScore
- Inherits:
-
Object
- Object
- FuzzyString::AdjustedScore
- Defined in:
- lib/fuzzy_string/adjusted_score.rb
Class Method Summary collapse
Instance Method Summary collapse
- #adjusted_levenschtein_distance ⇒ Object
-
#initialize(first, second) ⇒ AdjustedScore
constructor
A new instance of AdjustedScore.
- #rank ⇒ Object
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) @long = first.length > second.length ? first : second @short = first.length > second.length ? second : first 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_distance ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/fuzzy_string/adjusted_score.rb', line 13 def adjusted_levenschtein_distance special_chars = [")", "(", "]", "[", "}", "{", ".", "?", "+", "*"] regex = /#{@short.chars.to_a.map{|el| special_chars.include?(el) ? "\\"+el : el}.join('(.*?)')}/i pieces = @long.split(regex) score = FuzzyString::Levenshtein.distance(@short,@long) score *= pieces[0..@short.length - 1].uniq == [''] ? 0.75 : 1 letter_ratio = score.to_f / @long.length score -= pieces[0][-1] == ' ' ? letter_ratio * 0.75 : 0 score -= (pieces[@short.length] || [])[0] == ' ' ? letter_ratio * @short.length * 0.75 : 0 score -= letter_ratio * cost(pieces.shift,0.5) score -= letter_ratio * cost(pieces.pop, 0.9) score -= letter_ratio * cost(pieces.join, 0.1) end |
#rank ⇒ Object
7 8 9 10 11 12 |
# File 'lib/fuzzy_string/adjusted_score.rb', line 7 def rank return 0 if (@long == @short) return @short.length if (@long.length == 0) return @long.length if (@short.length == 0) adjusted_levenschtein_distance end |