RosettaCodeData/Task/Soundex/Phix/soundex.phix

80 lines
3.0 KiB
Plaintext

constant soundex_alphabet = "0123012#02245501262301#202"
-- ABCDEFGHIJKLMNOPQRSTUVWXYZ
function soundex(string name)
string res = "0000"
integer rdx = 1, ch, this, prev
for i=1 to length(name) do
ch = upper(name[i])
if ch>='A' and ch<='Z' then
this = soundex_alphabet[ch-'A'+1]
if rdx=1 then
res[1] = ch
rdx = 2
prev = this
elsif this!='#' then
if this!='0' and this!=prev then
res[rdx] = this
if rdx=4 then exit end if
rdx += 1
end if
prev = this
end if
end if
end for
return res
end function
constant tests = {
{"Ashcraft", "A261"}, -- not "A226"
{"Ashcroft", "A261"}, -- not "A226"
{"Ashkrofd", "A261"}, -- not "A226"
{"Burroughs", "B620"},
{"Burrows", "B620"},
{"ciondecks", "C532"},
{"Example", "E251"},
{"Ekzampul", "E251"},
{"Ellery", "E460"},
{"Euler", "E460"},
{"Gauss", "G200"},
{"Ghosh", "G200"},
{"Gutierrez", "G362"},
{"He", "H000"},
{"Heilbronn", "H416"},
{"Hilbert", "H416"},
{"Honeyman", "H555"}, -- not "H500"
{"Jackson", "J250"},
{"Kant", "K530"},
{"Knuth", "K530"},
{"Lee", "L000"},
{"Ladd", "L300"},
{"Lloyd", "L300"},
{"Lissajous", "L222"},
{"Lukasiewicz", "L222"},
{"Moses", "M220"},
{"O'Hara", "O600"},
{"Pfister", "P236"}, -- not "P123"
{"Robert", "R163"},
{"Rupert", "R163"},
{"Rubin", "R150"},
{"r~@sum~@", "R250"},
{"Soundex", "S532"},
{"Sownteks", "S532"},
{"Tymczak", "T522"}, -- not "T520"
{"VanDeusen", "V532"},
{"Washington", "W252"},
{"Wheaton", "W350"},
{"Weeton", "W350"},
{"", "0000"},
{" ", "0000"},
{"12346", "0000"},
{"aaa a", "A000"}
}
for i=1 to length(tests) do
string {name,expected} = tests[i],
res = soundex(name),
ok = iff(res=expected?"":"*** ERROR ***")
printf(1,"%-12s -> %s %s\n",{name,res,ok})
end for