#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL input_word AS STRING
    LOCAL soundex_code AS STRING
    LOCAL i AS LONG
    LOCAL prev_code AS STRING
    LOCAL curr_char AS STRING
    LOCAL curr_code AS STRING
    LOCAL c AS LONG
    LOCAL temp_str AS STRING

    ' Soundex: letter -> digit mapping
    ' B,F,P,V=1, C,G,J,K,Q,S,X,Z=2, D,T=3, L=4, M,N=5, R=6

    PRINT "Soundex Phonetic Algorithm"
    PRINT "=========================="
    PRINT ""

    ' Test: Washington -> W252
    input_word = "WASHINGTON"
    soundex_code = ""

    IF LEN(input_word) > 0 THEN
        c = ASC(LEFT$(input_word, 1))
        IF c >= 97 AND c <= 122 THEN c = c - 32
        soundex_code = CHR$(c)

        prev_code = ""
        IF c = 66 OR c = 70 OR c = 80 OR c = 86 THEN prev_code = "1"
        IF c = 67 OR c = 71 OR c = 74 OR c = 75 OR c = 81 OR c = 83 OR c = 88 OR c = 90 THEN prev_code = "2"
        IF c = 68 OR c = 84 THEN prev_code = "3"
        IF c = 76 THEN prev_code = "4"
        IF c = 77 OR c = 78 THEN prev_code = "5"
        IF c = 82 THEN prev_code = "6"

        i = 2
        WHILE i <= LEN(input_word) AND LEN(soundex_code) < 4
            c = ASC(MID$(input_word, i, 1))
            IF c >= 97 AND c <= 122 THEN c = c - 32
            curr_code = ""
            IF c = 66 OR c = 70 OR c = 80 OR c = 86 THEN curr_code = "1"
            IF c = 67 OR c = 71 OR c = 74 OR c = 75 OR c = 81 OR c = 83 OR c = 88 OR c = 90 THEN curr_code = "2"
            IF c = 68 OR c = 84 THEN curr_code = "3"
            IF c = 76 THEN curr_code = "4"
            IF c = 77 OR c = 78 THEN curr_code = "5"
            IF c = 82 THEN curr_code = "6"

            IF curr_code <> "" AND curr_code <> prev_code THEN
                soundex_code = soundex_code + curr_code
                prev_code = curr_code
            END IF

            IF curr_code = "" THEN
                prev_code = ""
            END IF

            i = i + 1
        WEND

        WHILE LEN(soundex_code) < 4
            soundex_code = soundex_code + "0"
        WEND
    END IF

    PRINT "Washington -> " + soundex_code

    ' Test: Lee -> L000
    input_word = "LEE"
    soundex_code = ""
    IF LEN(input_word) > 0 THEN
        c = ASC(LEFT$(input_word, 1))
        IF c >= 97 AND c <= 122 THEN c = c - 32
        soundex_code = CHR$(c)
        prev_code = ""
        IF c = 66 OR c = 70 OR c = 80 OR c = 86 THEN prev_code = "1"
        IF c = 67 OR c = 71 OR c = 74 OR c = 75 OR c = 81 OR c = 83 OR c = 88 OR c = 90 THEN prev_code = "2"
        IF c = 68 OR c = 84 THEN prev_code = "3"
        IF c = 76 THEN prev_code = "4"
        IF c = 77 OR c = 78 THEN prev_code = "5"
        IF c = 82 THEN prev_code = "6"
        i = 2
        WHILE i <= LEN(input_word) AND LEN(soundex_code) < 4
            c = ASC(MID$(input_word, i, 1))
            IF c >= 97 AND c <= 122 THEN c = c - 32
            curr_code = ""
            IF c = 66 OR c = 70 OR c = 80 OR c = 86 THEN curr_code = "1"
            IF c = 67 OR c = 71 OR c = 74 OR c = 75 OR c = 81 OR c = 83 OR c = 88 OR c = 90 THEN curr_code = "2"
            IF c = 68 OR c = 84 THEN curr_code = "3"
            IF c = 76 THEN curr_code = "4"
            IF c = 77 OR c = 78 THEN curr_code = "5"
            IF c = 82 THEN curr_code = "6"
            IF curr_code <> "" AND curr_code <> prev_code THEN
                soundex_code = soundex_code + curr_code
                prev_code = curr_code
            END IF
            IF curr_code = "" THEN prev_code = ""
            i = i + 1
        WEND
        WHILE LEN(soundex_code) < 4
            soundex_code = soundex_code + "0"
        WEND
    END IF
    PRINT "Lee -> " + soundex_code

    ' Test: Smith/Smythe -> S530
    input_word = "SMITH"
    soundex_code = ""
    IF LEN(input_word) > 0 THEN
        c = ASC(LEFT$(input_word, 1))
        IF c >= 97 AND c <= 122 THEN c = c - 32
        soundex_code = CHR$(c)
        prev_code = ""
        IF c = 66 OR c = 70 OR c = 80 OR c = 86 THEN prev_code = "1"
        IF c = 67 OR c = 71 OR c = 74 OR c = 75 OR c = 81 OR c = 83 OR c = 88 OR c = 90 THEN prev_code = "2"
        IF c = 68 OR c = 84 THEN prev_code = "3"
        IF c = 76 THEN prev_code = "4"
        IF c = 77 OR c = 78 THEN prev_code = "5"
        IF c = 82 THEN prev_code = "6"
        i = 2
        WHILE i <= LEN(input_word) AND LEN(soundex_code) < 4
            c = ASC(MID$(input_word, i, 1))
            IF c >= 97 AND c <= 122 THEN c = c - 32
            curr_code = ""
            IF c = 66 OR c = 70 OR c = 80 OR c = 86 THEN curr_code = "1"
            IF c = 67 OR c = 71 OR c = 74 OR c = 75 OR c = 81 OR c = 83 OR c = 88 OR c = 90 THEN curr_code = "2"
            IF c = 68 OR c = 84 THEN curr_code = "3"
            IF c = 76 THEN curr_code = "4"
            IF c = 77 OR c = 78 THEN curr_code = "5"
            IF c = 82 THEN curr_code = "6"
            IF curr_code <> "" AND curr_code <> prev_code THEN
                soundex_code = soundex_code + curr_code
                prev_code = curr_code
            END IF
            IF curr_code = "" THEN prev_code = ""
            i = i + 1
        WEND
        WHILE LEN(soundex_code) < 4
            soundex_code = soundex_code + "0"
        WEND
    END IF
    PRINT "Smith -> " + soundex_code

    input_word = "SMYTHE"
    soundex_code = ""
    IF LEN(input_word) > 0 THEN
        c = ASC(LEFT$(input_word, 1))
        IF c >= 97 AND c <= 122 THEN c = c - 32
        soundex_code = CHR$(c)
        prev_code = ""
        IF c = 66 OR c = 70 OR c = 80 OR c = 86 THEN prev_code = "1"
        IF c = 67 OR c = 71 OR c = 74 OR c = 75 OR c = 81 OR c = 83 OR c = 88 OR c = 90 THEN prev_code = "2"
        IF c = 68 OR c = 84 THEN prev_code = "3"
        IF c = 76 THEN prev_code = "4"
        IF c = 77 OR c = 78 THEN prev_code = "5"
        IF c = 82 THEN prev_code = "6"
        i = 2
        WHILE i <= LEN(input_word) AND LEN(soundex_code) < 4
            c = ASC(MID$(input_word, i, 1))
            IF c >= 97 AND c <= 122 THEN c = c - 32
            curr_code = ""
            IF c = 66 OR c = 70 OR c = 80 OR c = 86 THEN curr_code = "1"
            IF c = 67 OR c = 71 OR c = 74 OR c = 75 OR c = 81 OR c = 83 OR c = 88 OR c = 90 THEN curr_code = "2"
            IF c = 68 OR c = 84 THEN curr_code = "3"
            IF c = 76 THEN curr_code = "4"
            IF c = 77 OR c = 78 THEN curr_code = "5"
            IF c = 82 THEN curr_code = "6"
            IF curr_code <> "" AND curr_code <> prev_code THEN
                soundex_code = soundex_code + curr_code
                prev_code = curr_code
            END IF
            IF curr_code = "" THEN prev_code = ""
            i = i + 1
        WEND
        WHILE LEN(soundex_code) < 4
            soundex_code = soundex_code + "0"
        WEND
    END IF
    PRINT "Smythe -> " + soundex_code
    PRINT "Smith/Smythe match: S530"

    PRINT ""
    PRINT "Soundex demo complete."

    FUNCTION = 0
END FUNCTION
