2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -0,0 +1,8 @@
declare -A value=(
[B]=1 [F]=1 [P]=1 [V]=1
[C]=2 [G]=2 [J]=2 [K]=2 [Q]=2 [S]=2 [X]=2 [Z]=2
[D]=3 [T]=3
[L]=4
[M]=5 [N]=5
[R]=6
)

View file

@ -0,0 +1,36 @@
soundex() {
local -u word=${1//[^[:alpha:]]/.}
local letter=${word:0:1}
local soundex=$letter
local previous=$letter
word=${word:1}
word=${word//[AEIOUY]/.}
word=${word//[WH]/=}
while [[ ${#soundex} -lt 4 && -n $word ]]; do
letter=${word:0:1}
if [[ $letter == "." ]]; then
previous=""
elif [[ $letter == "=" ]]; then
if [[ $previous == [A-Z] && ${word:1:1} == [A-Z] ]] &&
[[ ${value[$previous]} -eq ${value[${word:1:1}]} ]]
then
word=${word:1}
fi
elif [[ -z $previous ]] ||
[[ $letter != $previous && ${value[$letter]} -ne ${value[$previous]} ]]
then
previous=$letter
soundex+=${value[$letter]}
fi
word=${word:1}
done
# right pad with zeros
soundex+="000"
echo "${soundex:0:4}"
}

View file

@ -0,0 +1,44 @@
soundex2() {
local -u word=${1//[^[:alpha:]]/}
# 1. Save the first letter. Remove all occurrences of 'h' and 'w' except first letter.
local first=${word:0:1}
word=${word:1}
word=$first${word//[HW]/}
# 2. Replace all consonants (include the first letter) with digits as in [2.] above.
local consonants=$(IFS=; echo "${!value[*]}")
local tmp letter
local -i i
for ((i=0; i < ${#word}; i++)); do
letter=${word:i:1}
if [[ $consonants == *$letter* ]]; then
tmp+=${value[$letter]}
else
tmp+=$letter
fi
done
word=$tmp
# 3. Replace all adjacent same digits with one digit.
local char
tmp=${word:0:1}
local previous=${word:0:1}
for ((i=1; i < ${#word}; i++)); do
char=${word:i:1}
[[ $char != [[:digit:]] || $char != $previous ]] && tmp+=$char
previous=$char
done
word=$tmp
# 4. Remove all occurrences of a, e, i, o, u, y except first letter.
tmp=${word:1}
word=${word:0:1}${tmp//[AEIOUY]/}
# 5. If first symbol is a digit replace it with letter saved on step 1.
[[ $word == [[:digit:]]* ]] && word=$first${word:1}
# 6. right pad with zeros
word+="000"
echo "${word:0:4}"
}

View file

@ -0,0 +1,21 @@
soundex3() {
local -u word=${1//[^[:alpha:]]/}
# 1. Save the first letter. Remove all occurrences of 'h' and 'w' except first letter.
local first=${word:0:1}
word=$first$( tr -d "HW" <<< "${word:1}" )
# 2. Replace all consonants (include the first letter) with digits as in [2.] above.
# 3. Replace all adjacent same digits with one digit.
local consonants=$( IFS=; echo "${!value[*]}" )
local values=$( IFS=; echo "${value[*]}" )
word=$( tr -s "$consonants" "$values" <<< "$word" )
# 4. Remove all occurrences of a, e, i, o, u, y except first letter.
# 5. If first symbol is a digit replace it with letter saved on step 1.
word=$first$( tr -d "AEIOUY" <<< "${word:1}" )
# 6. right pad with zeros
word+="000"
echo "${word:0:4}"
}

View file

@ -0,0 +1,28 @@
declare -A tests=(
[Soundex]=S532 [Example]=E251 [Sownteks]=S532 [Ekzampul]=E251
[Euler]=E460 [Gauss]=G200 [Hilbert]=H416 [Knuth]=K530
[Lloyd]=L300 [Lukasiewicz]=L222 [Ellery]=E460 [Ghosh]=G200
[Heilbronn]=H416 [Kant]=K530 [Ladd]=L300 [Lissajous]=L222
[Wheaton]=W350 [Burroughs]=B620 [Burrows]=B620 ["O'Hara"]=O600
[Washington]=W252 [Lee]=L000 [Gutierrez]=G362 [Pfister]=P236
[Jackson]=J250 [Tymczak]=T522 [VanDeusen]=V532 [Ashcraft]=A261
)
run_tests() {
local func=$1
echo "Testing with function $func"
local -i all=0 fail=0
for name in "${!tests[@]}"; do
s=$($func "$name")
if [[ $s != "${tests[$name]}" ]]; then
echo "FAIL - $s - $name -- EXPECTING ${tests[$name]}"
((fail++))
fi
((all++))
done
echo "$fail out of $all failures"
}
run_tests soundex
run_tests soundex2
run_tests soundex3