Class: Phonetic::RefinedSoundex

Inherits:
Algorithm show all
Defined in:
lib/phonetic/refined_soundex.rb

Overview

Class for encoding string to Refined Soundex code. A refined soundex code is optimized for spell checking words.

Examples:

Phonetic::RefinedSoundex.encode('Caren')   # => 'C30908'
Phonetic::RefinedSoundex.encode('Hayers')  # => 'H093'
Phonetic::RefinedSoundex.encode('Lambard') # => 'L7081096'

Constant Summary collapse

CODE =
{
  B: 1, P: 1,
  F: 2, V: 2,
  C: 3, K: 3, S: 3,
  G: 4, J: 4,
  Q: 5, X: 5, Z: 5,
  D: 6, T: 6,
  L: 7,
  M: 8, N: 8,
  R: 9
}

Class Method Summary collapse

Methods inherited from Algorithm

encode

Class Method Details

.encode_word(word, options = {}) ⇒ Object

Encode word to its Refined Soundex value



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/phonetic/refined_soundex.rb', line 25

def self.encode_word(word, options = {})
  w = word.upcase
  res = w[0]
  pg = nil
  w.chars.each do |c|
    g = CODE[c.to_sym] || 0
    if pg != g
      res += g.to_s
      pg = g
    end
  end
  res
end