func soundex(word, length=3) {
word.uc!.tr!('A-Z', '', 'cd')
return(nil) if (word.is_empty)
var firstLetter = word.char(0)
word.tr!('BFPV', '1', 's')
word.tr!('CGJKQSXZ', '2', 's')
word.tr!('DT', '3', 's')
word.tr!('L', '4', 's')
word.tr!('MN', '5', 's')
word.tr!('R', '6', 's')
word.ft!(1)
word.tr!('AEHIOUWY', '', 'd')
firstLetter + (word.chars + length.of('0') -> first(length).join)
}
func testSoundex {
var sndx = Hash(
"Euler" => "E4600",
"Gauss" => "G2000",
"Hilbert" => "H4163",
"Knuth" => "K5300",
"Lloyd" => "L3000",
"Lukasieicz" => "L2220",
'fulkerson' => 'F4262',
'faulkersuhn' => 'F4262',
'fpfffffauhlkkersssin' => 'F4262',
'Aaeh' => 'A0000',
)
sndx.keys.sort.each { |name|
var findSdx = soundex(name, 4)
say "The soundex for #{name} should be #{sndx{name}} and is #{findSdx}"
if (findSdx != sndx{name}) {
say "\tHowever, that is incorrect!\n"
}
}
}
testSoundex()