Method: String#fuzzy_match

Defined in:
lib/fat_core/string.rb

#fuzzy_match(other) ⇒ Object

See if self contains colon- or space-separated words that include the colon- or space-separated words of other. Return the matched portion of self. Other cannot be a regex embedded in a string.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fat_core/string.rb', line 16

def fuzzy_match(other)
  # Remove periods, commas, and apostrophes
  other = other.gsub(/[\*.,']/, '')
  target = self.gsub(/[\*.,']/, '')
  matched_text = nil
  matchers = other.split(/[: ]+/)
  regexp_string = matchers.map {|m| ".*?#{Regexp.escape(m)}.*?"}.join('[: ]')
  regexp_string.sub!(/^\.\*\?/, '')
  regexp_string.sub!(/\.\*\?$/, '')
  regexp = /#{regexp_string}/i
  if match = regexp.match(target)
    matched_text = match[0]
  else
    matched_text = nil
  end
  matched_text
end