Posted on December 23, 2005 at 2:40 pm
I’ve found two implementations of the enhanced Soundex algorithm for Ruby. The first is written by Mike Stok, and is a port of his Perl implementation:
def soundex(string)
copy = string.upcase.tr '^A-Z', ''
return nil if copy.empty?
first_letter = copy[0, 1]
copy.tr_s! 'AEHIOUWYBFPVCGJKQSXZDTLMNR', '00000000111122222222334556'
copy.sub!(/^(.)\1*/, '').gsub!(/0/, '')
"#{first_letter}#{copy.ljust(3,"0")}"
end
The other is written by Michael Neumann and is available at the Ruby Application Archive.
I would recommend using Mike Stok’s implementation unless you need to be able to pass in an array. It’s twice as fast as the one at the RAA site. Both algorithms tested over 10,000 iterations:
user system total real stok soundex: 0.290000 0.010000 0.300000 ( 0.350133) raa soundex: 0.600000 0.010000 0.610000 ( 0.708554)


