import sys def soundex(name, len=6): """ soundex module conforming to Knuth's algorithm implementation 2000-12-24 by Gregory Jorgensen public domain """ # digits holds the soundex values for the alphabet digits = '01230120022455012623010202' sndx = '' firstchar = '' # translate alpha chars in name to soundex digits for char in name.upper(): if char.isalpha(): if not firstchar: firstchar = char # remember first letter digit = digits[ord(char)-ord('A')] # duplicate consecutive soundex digits are skipped if not sndx or (digit != sndx[-1]): sndx += digit # replace first digit with first alpha character sndx = firstchar + sndx[1:] # remove all 0s from the soundex code sndx = sndx.replace('0','') # return soundex code padded to len characters return (sndx + (len * '0'))[:len] if __name__ == "__main__": for arg in sys.argv: print "INSERT INTO names(soundex) VALUES ('%s') WHERE names.name='%s'" % (soundex(arg), arg)