Soundex (Sed)

From LiteratePrograms

Jump to: navigation, search
Other implementations: C | Rexx | Sed



The Soundex algorithm is lossy but useful for hashing
T00000 S53200 A42635 I20000 L20000 B30000 U21400 F60000 H25200

As Soundex was originally developed ca. 1920, it probably (when automated) ran on card processing hardware significantly less powerful than sed.


We perform a direct transcription of the algorithm given in Patterson's Soundex (Rexx) implementation.

Logical processing

First, a straightforward lookup table, with y

<<convert characters to numerics>>=
#/* (1) Convert characters to numerics using table:                    */
#/*     0 = A,E,H,I,O,U,W,Y                                            */
#/*     1 = B,F,P,V                                                    */
#/*     2 = C,G,J,K,Q,S,X,Z                                            */
#/*     3 = D,T                                                        */
#/*     4 = L                                                          */
#/*     5 = M,N                                                        */
#/*     6 = R                                                          */

Now for a couple of iterated transformations -- we use : and t to apply the substitutions up to a fixpoint.

<<make multiple digits single>>=
#/* (2) Make multiple digits single.                                   */
<<remove trailing zeroes>>=
#/* (3) Remove zeroes after first position.                            */

Question 1. if f is an operator applying an injective map, and f2 is the map applied twice, what is ?

Exercise 2. note that denull commutes with setify; furthermore, the individual steps of each commute with each other. Rewrite this section to combine the loops.

Question 3. what is the minimum number of loops needed in an iterative program?

Physical processing

Then we pad or truncate to produce a 6 digit code.

<<pad with zeroes to 6 chars>>=
#/* (4) Fill on right with zeroes to make 6 characters.                */

Finally we restore the leading character. Assuming we have a copy of the alphabetical input in the hold space, we can append it to the pattern space with g. The replacement then becomes a simple matter of taking the tail of the numeric string and appending it to the head of the alphabetic:

<<restore first character>>=
#/* (5) Replace first digit with first character of name.              */

Wrapping up

In order to use a copy of the input from the hold space, we must first have stashed it there with the h command. Also, we accept lowercased input while stripping punctuation and other non-alphabetics.

convert characters to numerics
make multiple digits single
remove trailing zeroes
pad with zeroes to 6 chars
restore first character
Download code