Skip to content

Instantly share code, notes, and snippets.

@sharkdeng
Last active October 18, 2020 12:03
Show Gist options
  • Save sharkdeng/5f77486b00311c1c8ad655942d3d9310 to your computer and use it in GitHub Desktop.
Save sharkdeng/5f77486b00311c1c8ad655942d3d9310 to your computer and use it in GitHub Desktop.
Soundex algorithm
def soundex(string):
# keep first letter of a string
string = string.lower()
result = string[0]
string = string[1:]
# remove [a, e, i, o, u, y, h, w]
tmp = ['a', 'e', 'i', 'o', 'u', 'y', 'h', 'w']
for t in tmp:
if t in string:
string.replace(t, '')
# Replace all consonants from position 2 onwards with digits using these rules:
dicts = {'bfpv': 1,
'cgjkqsxz': 2,
'dt': 3,
'l':4,
'mn':5,
'r':6}
for c in string:
for k in dicts:
if c in k:
result += str(dicts[k])
# only keep unique adjacent digits
# return 4
return result[:4].ljust(4, '0')
def test():
assert soundex('gail') == 'g400'
assert soundex('christine') == 'c623'
assert soundex('kristina') == 'k623'
assert soundex('peter') == 'p360'
assert soundex('gayle') == 'g400'
assert soundex('christina') == 'c623'
assert soundex('kirstin') == 'k623'
assert soundex('christen') == 'c623'
print(soundex('Brian'))
print(soundex('Elizabeth'))
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment