Class: Phonetic::Soundex

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

Overview

Soundex phonetic algorithm was developed by Robert C. Russell and Margaret K. Odell. This class implements American Soundex version of algorithm.

Examples:

Phonetic::Soundex.encode('Ackerman') # => 'A265'
Phonetic::Soundex.encode('ammonium') # => 'A500'
Phonetic::Soundex.encode('implementation') # => 'I514'

Constant Summary collapse

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

Class Method Summary collapse

Methods inherited from Algorithm

encode

Class Method Details

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

Convert word to its Soundex code



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

def self.encode_word(word, options = {})
  return '' if word.empty?
  w = word.upcase
  res = w[0]
  pg = CODE[w[0].to_sym]
  (1...w.size).each do |i|
    g = CODE[w[i].to_sym]
    if g and pg != g
      res += g.to_s
      pg = g
    end
    break if res.size > 3
  end
  res = res.ljust(4, '0')
  res
end