Soundex is an algorithm for creating indices for words based on their pronunciation.



Task

The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling   (from the   soundex   Wikipedia article).

Caution

There is a major issue in many of the implementations concerning the separation of two consonants that have the same soundex code! According to the official Rules [[1]]. So check for instance if Ashcraft is coded to A-261.

 If a vowel (A, E, I, O, U) separates two consonants that have the same soundex code, the consonant to the right of the vowel is coded. Tymczak is coded as T-522 (T, 5 for the M, 2 for the C, Z ignored (see "Side-by-Side" rule above), 2 for the K). Since the vowel "A" separates the Z and K, the K is coded.
 If "H" or "W" separate two consonants that have the same soundex code, the consonant to the right of the vowel is not coded. Example: Ashcraft is coded A-261 (A, 2 for the S, C ignored, 6 for the R, 1 for the F). It is not coded A-226.


*       # Soundex coding
*       # ABCDEFGHIJKLMNOPQRSTUVWXYZ
*       # 01230127022455012623017202
 
        define('soundex(str)init,ch') :(soundex_end)
soundex sdxmap = '01230127022455012623017202'
        str = replace(str,&lcase,&ucase)
sdx1    str notany(&ucase) = :s(sdx1)
        init = substr(str,1,1)
        str = replace(str,&ucase,sdxmap)
sdx2    str len(1) $ ch span(*ch) = ch :s(sdx2)
*       # Omit next line for Knuth's simple Soundex
sdx3    str len(1) $ ch ('7' *ch) = ch :s(sdx3)
        str len(1) = init
sdx4    str any('07') = :s(sdx4)
        str = substr(str,1,4)
        str = lt(size(str),4) str dupl('0',4 - size(str))
        soundex = str :(return)
soundex_end
 
*       # Test and display
        test = " Washington Lee Gutierrez Pfister Jackson Tymczak"
+              " Ashcroft Swhgler O'Connor Rhys-Davies"
loop    test span(' ') break(' ') . name = :f(end)
        output = soundex(name) ' ' name :(loop)
end