local d, digits, alpha = '01230120022455012623010202', {}, ('A'):byte()
d:gsub(".",function(c) 
  digits[string.char(alpha)] = c
  alpha = alpha + 1
end)

function soundex(w)
  local res = {}
  for c in w:upper():gmatch'.'do
    local d = digits[c]
    if d then
      if #res==0 then
        res[1] =  c
      elseif #res==1 or d~= res[#res] then
        res[1+#res] = d
      end      
    end    
  end
  if #res == 0 then
    return '0000'
  else
    res = table.concat(res):gsub("0",'')
    return (res .. '0000'):sub(1,4)
  end
end