| 1 | def soundex_al(word): |
| 2 | cap_word = word.upper() # convert the word to uppercase |
| 3 | |
| 4 | return_val = "" |
| 5 | return_val = "" + cap_word[0] # get the first letter of the word |
| 6 | |
| 7 | # dictonary to give values to the letters |
| 8 | code_dict = {"BFPV": "1", "CGJKQSXZ": "2", "DT": "3", "L": "4", "MN": "5", "R": "6"} |
| 9 | |
| 10 | # array of charactors to remove from the word |
| 11 | rem_charactors = ["A", "E", "I", "O", "U", "H", "W", "Y"] |
| 12 | |
| 13 | # for loop to remove all the 0 valued charactors |
| 14 | temp = "" |
| 15 | for char in cap_word[1:]: |
| 16 | if char not in rem_charactors: |
| 17 | temp = temp + char |
| 18 | |
| 19 | # get the values from the 'code_dict' and create the soundex code |
| 20 | for char in temp: |
| 21 | for key in code_dict.keys(): |
| 22 | if char in key: |
| 23 | code = code_dict[key] |
| 24 | if code != return_val[-1]: # Remove all pairs of consecutive digits. |
| 25 | return_val += code |
| 26 | |
| 27 | return_val = return_val[:4] # crop the word to 4 charactors |
| 28 | |
| 29 | # if soundex code doen't contain 4 digits. fill it with zeros |
| 30 | if len(return_val) < 4: |
| 31 | for x in range(len(return_val), 4): |
| 32 | return_val = return_val + "0" |
| 33 | |
| 34 | # return the value |
| 35 | return return_val |
| 36 | |
| 37 | |
| 38 | # testing the fucntion |