Skip to content

Instantly share code, notes, and snippets.

@livibetter
Created March 8, 2012 01:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save livibetter/1997957 to your computer and use it in GitHub Desktop.
Save livibetter/1997957 to your computer and use it in GitHub Desktop.
Soundex in Bash
Copyright (c) 2012 Yu-Jie Lin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#!/bin/bash
# Copyright (c) 2012 Yu-Jie Lin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
shopt -s extglob
# http://en.wikipedia.org/wiki/Soundex#Rules
soundex() {
local s="${1,,}"
s="${s//[^a-z]/}"
local l="${s::1}"
s="${s//[bfpv]/1}"
s="${s//[cgjkqsxz]/2}"
s="${s//[dt]/3}"
s="${s//l/4}"
s="${s//[mn]/5}"
s="${s//r/6}"
local old_s
s="${s//w/h}"
while [[ "$s" != "$old_s" ]]; do
old_s="$s"
s="${s//1?(h)1/1}"
s="${s//2?(h)2/2}"
s="${s//3?(h)3/3}"
s="${s//4?(h)4/4}"
s="${s//5?(h)5/5}"
s="${s//6?(h)6/6}"
done
s="${s//[eiou]/a}"
s="${s//1a1/11}"
s="${s//2a2/22}"
s="${s//3a3/33}"
s="${s//4a4/44}"
s="${s//5a5/55}"
s="${s//6a6/66}"
s="${s:1}"
s="${s//[ayh]/}000"
s="${l^^}${s::3}"
ret="$s"
}
while (( $# > 0 )); do
name="$1"
soundex "$1"
echo "$ret"
shift
done
#!/bin/bash
source fuzzy.sh
test_soundex() {
soundex "$1"
if [[ "$ret" == "$2" ]]; then
printf "PASS: %-12s -> %4s\n" "$1" "$2"
let test_pass++
else
printf "FAIL: %-12s -> %4s != %4s\n" "$1" "$ret" "$2"
let test_fail++
fi
}
tests_soundex() {
test_pass=0
test_fail=0
# test cases from http://en.wikipedia.org/wiki/Soundex
test_soundex "Robert" "R163"
test_soundex "Rupert" "R163"
test_soundex "Rubin" "R150"
test_soundex "Ashcraft" "A261"
test_soundex "Ashcroft" "A261"
test_soundex "Tymczak" "T522"
test_soundex "Pfister" "P236"
# test cases from http://tldp.org/LDP/abs/html/contributed-scripts.html
test_soundex "Smith" "S530"
test_soundex "Smythe" "S530"
test_soundex "Harrison" "H625"
test_soundex "Hargison" "H622"
test_soundex "Harriman" "H655"
# test cases from http://rosettacode.org/wiki/Soundex
test_soundex "Soundex" "S532"
test_soundex "Example" "E251"
test_soundex "Sownteks" "S532"
test_soundex "Ekzampul" "E251"
test_soundex "Euler" "E460"
test_soundex "Gauss" "G200"
test_soundex "Hilbert" "H416"
test_soundex "Knuth" "K530"
test_soundex "Lloyd" "L300"
test_soundex "Lukasiewicz" "L222"
test_soundex "Ellery" "E460"
test_soundex "Ghosh" "G200"
test_soundex "Heilbronn" "H416"
test_soundex "Kant" "K530"
test_soundex "Ladd" "L300"
test_soundex "Lissajous" "L222"
test_soundex "Wheaton" "W350"
test_soundex "Burroughs" "B620"
test_soundex "Burrows" "B620"
test_soundex "O'Hara" "O600"
test_soundex "Washington" "W252"
test_soundex "Lee" "L000"
test_soundex "Gutierrez" "G362"
test_soundex "Jackson" "J250"
test_soundex "VanDeusen" "V532"
# test cases from me
test_soundex "Tomahawk" "T520"
echo "Tests passed: $test_pass / $(( test_pass + test_fail ))"
}
tests_soundex
TIMEFORMAT=%R
d=$((time for ((i=0; i<1000; i++)); do soundex "fuzzy" &>/dev/null; done) 2>&1)
echo "$(bc <<< "scale = 3; 1000000 * $d / 1000") us per call"
@livibetter
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment