module Text::Soundex

Public Class Methods

get_code(char) click to toggle source
# File lib/text/soundex.rb, line 53
def get_code(char)
  char.tr! "AEIOUYWHBPFVCSKGJQXZDTLMNR", "00000000111122222222334556"
end
soundex(str_or_arr) click to toggle source
# File lib/text/soundex.rb, line 11
def soundex(str_or_arr)
  case str_or_arr
  when String
    soundex_str(str_or_arr)
  when Array
    str_or_arr.collect{|ele| soundex_str(ele)}
  else
    nil
  end
end
soundex_str(str) click to toggle source

returns nil if the value couldn’t be calculated (empty-string, wrong-character) do not change the parameter “str”

# File lib/text/soundex.rb, line 29
def soundex_str(str)
  str = str.upcase.gsub(/[^A-Z]/, "")
  return nil if str.empty?

  last_code = get_code(str[0,1])
  soundex_code = str[0,1]

  for index in 1...(str.size) do
    return soundex_code if soundex_code.size == 4

    code = get_code(str[index,1])

    if code == "0" then
      last_code = nil
    elsif code != last_code then
      soundex_code += code
      last_code = code
    end
  end # for

  return soundex_code.ljust(4, "0")
end

Private Instance Methods

get_code(char) click to toggle source
# File lib/text/soundex.rb, line 53
def get_code(char)
  char.tr! "AEIOUYWHBPFVCSKGJQXZDTLMNR", "00000000111122222222334556"
end
soundex(str_or_arr) click to toggle source
# File lib/text/soundex.rb, line 11
def soundex(str_or_arr)
  case str_or_arr
  when String
    soundex_str(str_or_arr)
  when Array
    str_or_arr.collect{|ele| soundex_str(ele)}
  else
    nil
  end
end
soundex_str(str) click to toggle source

returns nil if the value couldn’t be calculated (empty-string, wrong-character) do not change the parameter “str”

# File lib/text/soundex.rb, line 29
def soundex_str(str)
  str = str.upcase.gsub(/[^A-Z]/, "")
  return nil if str.empty?

  last_code = get_code(str[0,1])
  soundex_code = str[0,1]

  for index in 1...(str.size) do
    return soundex_code if soundex_code.size == 4

    code = get_code(str[index,1])

    if code == "0" then
      last_code = nil
    elsif code != last_code then
      soundex_code += code
      last_code = code
    end
  end # for

  return soundex_code.ljust(4, "0")
end