DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Ruby Soundex

09.12.2007
| 7291 views |
  • submit to reddit
        
class String

    SoundexChars = 'BPFVCSKGJQXZDTLMNR'
    SoundexNums  = '111122222222334556'
    SoundexCharsEx = '^' + SoundexChars
    SoundexCharsDel = '^A-Z'

    # desc: http://en.wikipedia.org/wiki/Soundex
    def soundex(census = true)
        str = upcase.delete(SoundexCharsDel).squeeze

        str[0 .. 0] + str[1 .. -1].
            delete(SoundexCharsEx).
            tr(SoundexChars, SoundexNums)[0 .. (census ? 2 : -1)].
            ljust(3, '0') rescue ''
    end

    def sounds_like(other)
        soundex == other.soundex
    end
end

# puts "Quadratically".soundex
# 'Q363'

# puts "Quadratically".soundex(false)
# 'Q36324'
    

Comments

Snippets Manager replied on Fri, 2009/11/13 - 1:35am

You've got a bug there: have to squeeze after transliterating. For example, "example" and "eksampel" should produce the same code. str = upcase.delete(SoundexCharsDel) str[0 .. 0] + str[1 .. -1]. delete(SoundexCharsEx). tr(SoundexChars, SoundexNums). squeeze[0 .. (census ? 2 : -1)]. ljust(3, '0') rescue ''