class Phonetic::Soundex

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

@example

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

Constants

CODE

Public Class Methods

encode_word(word, options = {}) click to toggle source

Convert word to its Soundex code

# 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