Class: GuessMethod::GuessMethodGuesser
- Defined in:
- lib/guessmethod/guesser.rb
Overview
GuessMethodGuesser finds the closest matches in an array haystack
to a needle
, based on the levenshtein distance of downcased string representations of the objects. However, it returns an empty array if there aren’t matches closer than GuessMethodOptions[:threshold]
Class Method Summary collapse
Class Method Details
.adjusted_distance(from, to) ⇒ Object
31 32 33 34 35 |
# File 'lib/guessmethod/guesser.rb', line 31 def self.adjusted_distance(from, to) from.to_s.downcase.levenshtein(to.to_s.downcase, GuessMethodOptions[:insert_weight], GuessMethodOptions[:delete_weight], GuessMethodOptions[:substitution_weight]) end |
.find_closest(haystack, needle) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/guessmethod/guesser.rb', line 11 def self.find_closest(haystack, needle) closest_distance = (1.0/0.0) closest_matches = haystack.inject([]) do |close_ones, name| current_distance = adjusted_distance(name, needle) if current_distance == closest_distance close_ones << name elsif current_distance < closest_distance closest_distance = current_distance [name] else close_ones end end if closest_distance <= GuessMethodOptions[:threshold] closest_matches else [] end end |