-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundex.sf
61 lines (48 loc) · 1.7 KB
/
soundex.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/ruby
#
## https://rosettacode.org/wiki/Soundex
#
func soundex(word, length=3) {
# Uppercase the argument passed in to normalize it
# and drop any non-alphabetic characters
word.uc!.tr!('A-Z', '', 'cd')
# Return if word does not contain 'A-Z'
return(nil) if (word.is_empty)
var firstLetter = word.char(0)
# Replace letters with corresponding number values
word.tr!('BFPV', '1', 's')
word.tr!('CGJKQSXZ', '2', 's')
word.tr!('DT', '3', 's')
word.tr!('L', '4', 's')
word.tr!('MN', '5', 's')
word.tr!('R', '6', 's')
# Discard the first letter
word.slice!(1)
# Remove A, E, H, I, O, U, W, and Y
word.tr!('AEHIOUWY', '', 'd')
# Return the soundex code
firstLetter + (word.chars + length.of('0') -> first(length).join)
}
func testSoundex {
# Key-value pairs of names and corresponding Soundex codes
var sndx = Hash(
"Euler" => "E4600",
"Gauss" => "G2000",
"Hilbert" => "H4163",
"Knuth" => "K5300",
"Lloyd" => "L3000",
"Lukasieicz" => "L2220",
'fulkerson' => 'F4262',
'faulkersuhn' => 'F4262',
'fpfffffauhlkkersssin' => 'F4262',
'Aaeh' => 'A0000',
)
sndx.keys.sort.each { |name|
var findSdx = soundex(name, 4)
say "The soundex for #{name} should be #{sndx{name}} and is #{findSdx}"
if (findSdx != sndx{name}) {
die "\tHowever, that is incorrect!\n"
}
}
}
testSoundex()