module Wilderpeople

Constants

VERSION

Public Instance Methods

date() click to toggle source

Match dates

# File lib/wilderpeople/matcher.rb, line 95
def date
  return true if exact
  @dates = [a, b].collect{|x| date_parse(x)}
  exact(*dates)
rescue ArgumentError # Error raised when entry won't parse
  false
end
exact_except_last_word() click to toggle source

Designed for matching street names, so don't have to worry about Road/Rb and Street/St. Note that if matching one word, just that word is matched

# File lib/wilderpeople/matcher.rb, line 69
def exact_except_last_word
  return true if exact
  words = [a,b].collect(&:split)
  exact *words.collect{|w| w.size == 1 ? w : w[0..-2]}
end
exact_no_space() click to toggle source

Match 'Foo bar' with 'Foobar'

# File lib/wilderpeople/matcher.rb, line 82
def exact_no_space
  return true if exact
  exact *[a,b].collect{|x| remove_white_space(x)}
end
first_letter() click to toggle source

Match the first letter only

# File lib/wilderpeople/matcher.rb, line 76
def first_letter
  return true if exact
  exact *[a,b].collect{|x| x[0]}
end
fuzzy(threshold = self.class.levenshtein_threshold) click to toggle source

User Levenshtein distance to compare similar strings. The Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-character edits required to change one word into the other. See github.com/tliff/levenshtein

# File lib/wilderpeople/matcher.rb, line 118
def fuzzy(threshold = self.class.levenshtein_threshold)
  return true if exact
  return false if a.empty? || b.empty?
  !!Levenshtein.normalized_distance(a,b, threshold)
end
fuzzy_date() click to toggle source

Matches dates, but also handles day and month being swapped. So 3/5/2001 matches 5/3/2001

# File lib/wilderpeople/matcher.rb, line 105
def fuzzy_date
  return true if date
  return false unless dates
  return false if dates[1].day > 12
  exact(dates[0], swap_day_month(@dates[1]))
end
hypocorism() click to toggle source

Match English first names with alternative forms So 'Robert' matches 'Rob'

# File lib/wilderpeople/matcher.rb, line 89
def hypocorism
  return true if exact
  Hypocorism.match(a,b)
end
spaceless_transposed() click to toggle source

All the right letters but not necessarily in the right order and ignoring spaces

# File lib/wilderpeople/matcher.rb, line 61
def spaceless_transposed
  return true if exact
  exact *[a,b].collect{|x| remove_white_space(x).chars.sort}
end
transposed() click to toggle source

All the right letters but not necessarily in the right order

# File lib/wilderpeople/matcher.rb, line 54
def transposed
  return true if exact
  exact *[a,b].collect{|x| x.chars.sort}
end

Private Instance Methods

date_parse(string) click to toggle source
# File lib/wilderpeople/matcher.rb, line 144
def date_parse(string)
  Date.parse(string)
rescue ArgumentError
  try_american_format(string)
end
prep() click to toggle source
# File lib/wilderpeople/matcher.rb, line 126
def prep
  [a,b].each do |attr|
    case attr
    when String
      attr.downcase!
      attr.strip! if attr
    when Array
      attr.collect{|x| prep(x)}
    else
      attr
    end
  end
end
remove_white_space(string) click to toggle source
# File lib/wilderpeople/matcher.rb, line 161
def remove_white_space(string)
  string.gsub(/\s/, '')
end
swap_day_month(date) click to toggle source
# File lib/wilderpeople/matcher.rb, line 140
def swap_day_month(date)
  Date.new(date.year, date.day, date.month)
end
try_american_format(string) click to toggle source
# File lib/wilderpeople/matcher.rb, line 150
def try_american_format(string)
  Date.strptime string, "%m/%d/%Y"
rescue ArgumentError
  # Need to catch instance where system is using American format
  try_proper_format(string)
end
try_proper_format(string) click to toggle source
# File lib/wilderpeople/matcher.rb, line 157
def try_proper_format(string)
  Date.strptime string, "%d/%m/%Y"
end