Skip to content

Commit 0b0b6ec

Browse files
committed
CHANGE: ported Kamp's Soundex script to Rebol3 module
1 parent 31ca958 commit 0b0b6ec

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

src/modules/soundex.reb

+32-31
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
REBOL [
22
Title: "Soundex"
3-
Date: 17-Jul-1999
3+
Date: 16-Jul-2024
44
File: %soundex.r
5-
Author: "Allen Kamp"
5+
Author: "Allen Kamp, Oldes"
66
Purpose: {Soundex Encoding returns similar codes for similar sounding words or names. eg Stephens, Stevens are both S315, Smith and Smythe are both S53. Useful for adding Sounds-like searching to databases}
77
Comment: {
8-
This simple Soundex returns a code that is up to 4 characters
9-
long, the /integer refinement will return an integer code
10-
value instead. An example for searching a simple phone number
11-
database, with Soundex is included. For improved search
12-
speed, you could store the soundex codes in the database.
13-
14-
This is the basic algorithm (There are a number of different
8+
This is the basic Soundex algorithm (There are a number of different
159
one floating around)
1610
1711
1. Remove vowels, H, W and Y
@@ -21,8 +15,7 @@ REBOL [
2115
4. Return First letter, followed by the next 3 letter's code
2216
numbers, if they exist.
2317
24-
Others I will implement soon include, Extended Soundex,
25-
Metaphone and the LC Cutter table
18+
TODO: Other algorithms: Extended Soundex, Metaphone and the LC Cutter table
2619
}
2720
Language: "English"
2821
Email: %allenk--powerup--com--au
@@ -36,42 +29,50 @@ REBOL [
3629
license: none
3730
see-also: none
3831
]
39-
Version: 1.0.0
32+
Version: 1.1.0
33+
Type: module
34+
Exports: [soundex]
35+
Needs: 3.0.0
36+
History: [
37+
17-Jul-1999 @Allen "Initial version"
38+
16-Jul-2024 @Oldes "Ported to Rebol3"
39+
40+
]
4041
]
4142

42-
soundex: func[
43+
soundex: function/with [
4344
{Returns the Census Soundex Code for the given string}
4445
string [any-string!] "String to Encode"
45-
/local code val letter
4646
][
4747

4848
code: make string! ""
4949

50-
; Create Rules
51-
set1: [["B" | "F" | "P" | "V"](val: "1")]
52-
set2: [["C" | "G" | "J" | "K" | "Q" | "S" | "X" | "Z"](val: "2")]
53-
set3: [["D" | "T"](val: "3")]
54-
set4: [["L"](val: "4")]
55-
set5: [["M" | "N"] (val: "5")]
56-
set6: [["R"](val: "6")]
57-
; Append val to code if not a duplicate of previous code val
58-
soundex-match: [[set1 | set2 | set3 | set4 | set5 | set6 ]
59-
(if val <> back tail code [append code val]) ]
60-
61-
; If letter not a matched letter its val is 0, but we only care
62-
; about it if it is the first letter.
63-
soundex-no-match: [(if (length? code) = 0 [append code "0"])]
64-
6550
either all [string? string string <> ""] [
6651
string: uppercase trim copy string
6752

6853
foreach letter string [
69-
parse to-string letter [soundex-match | soundex-no-match]
70-
if (length? code) = 4 [break] ;maximum length for code is 4
54+
parse to string! letter [soundex-match | soundex-no-match]
55+
if 4 = length? code [break] ;maximum length for code is 4
7156
]
7257
] [
7358
return string ; return unchanged
7459
]
7560
change code first string ; replace first number with first letter
7661
return code
62+
][
63+
code: val: none
64+
; Create Rules
65+
set1: [[#"B" | #"F" | #"P" | #"V"](val: #"1")]
66+
set2: [[#"C" | #"G" | #"J" | #"K" | #"Q" | #"S" | #"X" | #"Z"](val: #"2")]
67+
set3: [[#"D" | #"T"](val: #"3")]
68+
set4: [[#"L"](val: "4")]
69+
set5: [[#"M" | #"N"] (val: #"5")]
70+
set6: [[#"R"](val: #"6")]
71+
; Append val to code if not a duplicate of previous code val
72+
soundex-match: [[set1 | set2 | set3 | set4 | set5 | set6 ]
73+
(if val <> back tail code [append code val]) ]
74+
75+
; If letter not a matched letter its val is 0, but we only care
76+
; about it if it is the first letter.
77+
soundex-no-match: [(if (length? code) = 0 [append code "0"])]
7778
]

0 commit comments

Comments
 (0)