class Yn::Lenient

Gracefully handles typos in the words yes or no.

Constants

E_MATCH
NO_MATCH_SCORE_THRESHOLD
N_MATCH
O_MATCH
S_MATCH
YES_MATCH_SCORE_THRESHOLD
Y_MATCH

Public Instance Methods

call(input, default) click to toggle source

Returns true or false if input resembles the words yes or no respectively, or the given default otherwise

@param [String] input The string to compare with the words yes or no @param [Object] default Value returned when the input doesn't resemble the words yes or no

@return [Object]

@api private

# File lib/yn/lenient.rb, line 73
def call(input, default)
  return true if get_yes_match_score(input) >= YES_MATCH_SCORE_THRESHOLD
  return false if get_no_match_score(input) >= NO_MATCH_SCORE_THRESHOLD

  default
end

Private Instance Methods

get_no_match_score(input) click to toggle source

Returns a match score with word no between 0 and 2

@param [String] input The string to compare with the word no

@return [Fixnum|Float]

@api private

# File lib/yn/lenient.rb, line 108
def get_no_match_score(input)
  n, o = input.split('')

  score = 0
  score += N_MATCH.fetch(n) if N_MATCH.key?(n)
  score += O_MATCH.fetch(o) if O_MATCH.key?(o)
  score
end
get_yes_match_score(input) click to toggle source

Returns a match score with word yes between 0 and 3

@param [String] input The string to compare with the word yes

@return [Fixnum|Float]

@api private

# File lib/yn/lenient.rb, line 90
def get_yes_match_score(input)
  y, e, s = input.split('')

  score = 0
  score += Y_MATCH.fetch(y) if Y_MATCH.key?(y)
  score += E_MATCH.fetch(e) if E_MATCH.key?(e)
  score += S_MATCH.fetch(s) if S_MATCH.key?(s)
  score
end