Created
February 20, 2019 10:32
-
-
Save reflash/95b91095e8f302e74c9507f7d44833a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def soundex(name, len=4):\n", | |
" \"\"\" soundex module conforming to Knuth's algorithm\n", | |
" implementation 2000-12-24 by Gregory Jorgensen\n", | |
" public domain\n", | |
" \"\"\"\n", | |
"\n", | |
" # digits holds the soundex values for the alphabet\n", | |
" digits = '01230120022455012623010202'\n", | |
" sndx = ''\n", | |
" fc = ''\n", | |
"\n", | |
" # translate alpha chars in name to soundex digits\n", | |
" for c in name.upper():\n", | |
" if c.isalpha():\n", | |
" if not fc: fc = c # remember first letter\n", | |
" d = digits[ord(c)-ord('A')]\n", | |
" # duplicate consecutive soundex digits are skipped\n", | |
" if not sndx or (d != sndx[-1]):\n", | |
" sndx += d\n", | |
"\n", | |
" # replace first digit with first alpha character\n", | |
" sndx = fc + sndx[1:]\n", | |
"\n", | |
" # remove all 0s from the soundex code\n", | |
" sndx = sndx.replace('0','')\n", | |
"\n", | |
" # return soundex code padded to len characters\n", | |
" return (sndx + (len * '0'))[:len]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'F460'" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"soundex('flower')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'F460'" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"soundex('flour')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment