soundex (revisited)

Daniel Klein DanielK at aracnet.com
Sun Dec 24 13:29:54 EST 2000


After seeing the post from several days ago on soundex, I gave it whirl to
see if I could come up with something different (and possibly better),
following the rules laid down by Knuth:

def get_soundex(name, digits = 3):
    soundexcodes = "01230120022455012623010202"
    #               ABCDEFGHIJKLMNOPQRSTUVWXYZ
    instring = name.upper()
    soundex = instring[0]
    last = soundex
    instring = instring[1:]
    for char in instring:
        if 65 <= ord(char) <= 90:
            sx = soundexcodes[ord(char) - 65]
            if int(sx) and char != last:
                soundex += sx
                last = char
    if len(soundex) < (digits + 1): soundex = (soundex + ("0" * digits))
    return soundex[:digits + 1]


As this is one of the first (complete) functions I have created in Python,
all comments, suggestions, derogatory remarks :^) are welcome.

One question: how is it that this understands the 'upper()' function without
explicitly importing the 'string' module?

Dan:





More information about the Python-list mailing list