Soundex – Python
Soundex algorithm:
1. Keep the first letter.
2. Convert the remaining letters to digits, according to mapping below:
abcdefghijklmnopqrstuvwxyz => 91239129922455912623919292
The phonetic representation is defined in The Art of Computer Programming, Volume 3: Sorting and Searching, by Donald E. Knuth, as follows:

[From Oracle 10g documentation]
Retain the first letter of the string and remove all other occurrences of the following letters: a, e, h, i, o, u, w, y.
Assign numbers to the remaining letters (after the first) as follows:
b, f, p, v = 1
c, g, j, k, q, s, x, z = 2
d, t = 3
l = 4
m, n = 5
r = 6
If two or more letters with the same number were adjacent in the original name (before step 1), or adjacent except for any intervening h and w, then omit all but the first.
Return the first four bytes padded with 0.
3. Remove consecutive duplicates.
4. Remove all 9s.
5. If the result is shorter than four characters (the first letter plus three digits), pad the result with trailing zeros.
6. if the result is longer than four characters, discard everything after the fourth character.
import string
charToSoundex = string.maketrans(string.letters, "91239129922455912623919292" * 2)
def soundex(source):
if not isalpha(source):
return "0000"
s = source[0].upper() + source[1:].translate(charToSoundex)
s2 = s[0]
for d in s[1:]:
if s2[−1] != d:
s2 += d
s3 = re.sub('9', '', s2)
while len(s3) < 4:
s3 += "0"
return s3[:4]