Ai
1 Star 0 Fork 1

jack2583/PythonExamples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
soundex_algorithm.py 1.30 KB
一键复制 编辑 原始数据 按行查看 历史
Danushka2 提交于 2020-10-03 18:54 +08:00 . soundex algorithm
def soundex_al(word):
cap_word = word.upper() #convert the word to uppercase
return_val = ""
return_val = "" + cap_word[0] #get the first letter of the word
#dictonary to give values to the letters
code_dict = {"BFPV": "1", "CGJKQSXZ":"2", "DT":"3", "L":"4", "MN":"5", "R":"6"}
#array of charactors to remove from the word
rem_charactors = ["A","E","I","O","U","H","W","Y"]
#for loop to remove all the 0 valued charactors
temp = ""
for char in cap_word[1:]:
if char not in rem_charactors:
temp = temp + char
#get the values from the 'code_dict' and create the soundex code
for char in temp:
for key in code_dict.keys():
if char in key:
code = code_dict[key]
if code != return_val[-1]: #Remove all pairs of consecutive digits.
return_val += code
return_val = return_val[:4] #crop the word to 4 charactors
#if soundex code doen't contain 4 digits. fill it with zeros
if len(return_val) < 4:
for x in range(len(return_val), 4):
return_val = return_val + "0"
#return the value
return return_val
#testing the fucntion
print(soundex_al("Danus"))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jack2583/pythonExamples.git
git@gitee.com:jack2583/pythonExamples.git
jack2583
pythonExamples
PythonExamples
master

搜索帮助