Soundex algorithm to achieve python

Soundex is a phonetic algorithm, calculates the approximation using the English word pronunciation, the value consists of four characters, the first character of the English alphabet, numbers of the three. In alphabetic writing, but sometimes there can not be read correctly spell the word circumstances, can do Soundex similar fuzzy matching results. E.g. Knuth and Kant two strings, their values are Soundex "K530". Its computer guru Donald Knuth famous book "Art of Computer Programming" are described in detail in. (Cited in Wikipedia Chinese version )
I'm here to implement the algorithm with a relatively simple manner, without a good level to understand.

def Soundex(string):
    string = string.lower()
    law = {'aehiouwy' : 0,
           'bfpv': 1,
           'cgjkqsxz': 2,
           'dt': 3,
           'l': 4,
           'mn': 5,
           'r': 6}
    temp_string = string
    for key, value in law.items():
        temp_string = re.sub('[{}]'.format(key), str(value), temp_string)
    temp_string = re.sub('0', '', temp_string)
    result = []
    for char in temp_string:
        if char not in result:
            result.append(char)

    result = string[0] + ''.join(result[:3])
    while len(result) != 4:
        result += '0'
    print(result)
Published 12 original articles · won praise 3 · Views 2055

Guess you like

Origin blog.csdn.net/weixin_40902563/article/details/105382376