Is there a soundex function for python?

Is there a soundex function for python?

Yes , you can use Fuzzy which is a python library implementing some phonetic algorithms.

sudo pip install fuzzy

>>> import fuzzy
>>> soundex = fuzzy.Soundex(4)
>>> soundex(Jackson)
J250
>>> soundex(Washington)
W252
>>> soundex(Clement)
C453
>>> soundex(Ashcraft)
A261
>>> soundex(Wu)
W000

You can use jellyfish

sudo pip install jellyfish

print Soundextt=, jellyfish.soundex(Ala ma kaca)
>Soundex                = A452
#...
>Metaphone              = AL M KK
>NYSIIS                 = AL
>Match rating codex     = ALMKC

Is there a soundex function for python?

Use the below soundex() function directly without installing any package!

Snippet taken from package
Jellyfish >
_jellyfish.py

Examples

print(soundex(kent)) # K530
print(soundex(Paul)) # P400
print(soundex(amnesty)) # A523

Code

import unicodedata
def soundex(s):

    if not s:
        return 

    s = unicodedata.normalize(NFKD, s)
    s = s.upper()

    replacements = (
        (BFPV, 1),
        (CGJKQSXZ, 2),
        (DT, 3),
        (L, 4),
        (MN, 5),
        (R, 6),
    )
    result = [s[0]]
    count = 1

    # find would-be replacment for first character
    for lset, sub in replacements:
        if s[0] in lset:
            last = sub
            break
    else:
        last = None

    for letter in s[1:]:
        for lset, sub in replacements:
            if letter in lset:
                if sub != last:
                    result.append(sub)
                    count += 1
                last = sub
                break
        else:
            if letter != H and letter != W:
                # leave last alone if middle letter is H or W
                last = None
        if count == 4:
            break

    result += 0 * (4 - count)
    return .join(result)

Leave a Reply

Your email address will not be published. Required fields are marked *