Module: Philiprehberger::FuzzyMatch::Metaphone

Defined in:
lib/philiprehberger/fuzzy_match/metaphone.rb

Constant Summary collapse

SIMPLE =
{ 'F' => 'F', 'J' => 'J', 'L' => 'L', 'M' => 'M', 'N' => 'N',
'Q' => 'K', 'R' => 'R', 'V' => 'F', 'X' => 'KS', 'Z' => 'S' }.freeze

Class Method Summary collapse

Class Method Details

.code(string) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/philiprehberger/fuzzy_match/metaphone.rb', line 6

def self.code(string)
  return '' if string.nil? || string.empty?

  word = string.upcase.gsub(/[^A-Z]/, '')
  return '' if word.empty?

  word = drop_initial_silent(word)
  result = +''
  i = 0

  while i < word.length
    char = word[i]
    next_char = word[i + 1]
    coded, skip = encode_char(word, i, char, next_char)
    result << coded if coded
    i += 1 + skip
  end

  result
end