#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL first_word AS STRING
    LOCAL last_word AS STRING
    LOCAL first_code AS STRING
    LOCAL last_code AS STRING
    LOCAL i AS LONG
    LOCAL c AS LONG
    LOCAL prev_char AS LONG
    LOCAL temp_str AS STRING

    ' NYSIIS phonetic encoding: name comparison algorithm
    ' Rules: translate names to phonetic key, handle vowel patterns

    PRINT "NYSIIS Phonetic Encoding"
    PRINT "========================"
    PRINT ""

    first_word = "MacDonald"
    first_code = ""
    i = 1

    ' Step 1: Handle initial characters
    WHILE i <= LEN(first_word)
        c = ASC(UPPER$(MID$(first_word, i, 1)))
        IF i = 1 THEN
            IF c = 77 THEN
                IF i + 1 <= LEN(first_word) AND ASC(UPPER$(MID$(first_word, i + 1, 1))) = 65 THEN
                    first_code = first_code + "M"
                ELSE
                    first_code = first_code + "M"
                END IF
            ELSEIF c = 75 THEN
                IF i + 1 <= LEN(first_word) AND ASC(UPPER$(MID$(first_word, i + 1, 1))) = 78 THEN
                    first_code = first_code + "N"
                    i = i + 1
                ELSE
                    first_code = first_code + "C"
                END IF
            ELSEIF c = 80 THEN
                IF i + 1 <= LEN(first_word) AND ASC(UPPER$(MID$(first_word, i + 1, 1))) = 72 THEN
                    first_code = first_code + "F"
                    i = i + 1
                ELSE
                    first_code = first_code + "P"
                END IF
            ELSEIF c = 87 THEN
                IF i + 1 <= LEN(first_word) AND ASC(UPPER$(MID$(first_word, i + 1, 1))) = 82 THEN
                    first_code = first_code + "R"
                    i = i + 1
                ELSE
                    first_code = first_code + "W"
                END IF
            ELSE
                first_code = first_code + CHR$(c)
            END IF
        ELSE
            IF c = 69 OR c = 73 OR c = 79 OR c = 85 THEN
                ' Vowel: translate to A
                temp_str = RIGHT$(first_code, 1)
                IF temp_str <> "A" THEN
                    first_code = first_code + "A"
                END IF
            ELSEIF c = 65 THEN
                ' A stays if previous not A
                temp_str = RIGHT$(first_code, 1)
                IF temp_str <> "A" THEN
                    first_code = first_code + "A"
                END IF
            ELSEIF c = 72 THEN
                prev_char = 0
                IF i > 1 THEN prev_char = ASC(UPPER$(MID$(first_word, i - 1, 1)))
                IF i + 1 <= LEN(first_word) THEN
                    temp_str = RIGHT$(first_code, 1)
                    ' H between consonants: use previous
                END IF
            ELSEIF c = 87 THEN
                ' W preceded by vowel: use previous
                prev_char = 0
                IF i > 1 THEN prev_char = ASC(UPPER$(MID$(first_word, i - 1, 1)))
                IF prev_char = 65 OR prev_char = 69 OR prev_char = 73 OR prev_char = 79 OR prev_char = 85 THEN
                    ' Keep what we have
                ELSE
                    first_code = first_code + "W"
                END IF
            ELSEIF c = 81 THEN
                first_code = first_code + "G"
            ELSEIF c = 90 THEN
                first_code = first_code + "S"
            ELSEIF c = 77 THEN
                first_code = first_code + "N"
            ELSEIF c = 75 THEN
                IF i + 1 <= LEN(first_word) AND ASC(UPPER$(MID$(first_word, i + 1, 1))) = 78 THEN
                    i = i + 1
                END IF
                first_code = first_code + "C"
            ELSEIF c = 83 THEN
                first_code = first_code + "S"
            ELSE
                first_code = first_code + CHR$(c)
            END IF
        END IF
        i = i + 1
    WEND

    PRINT "MacDonald -> " + first_code

    ' Encode "McDonald"
    first_word = "McDonald"
    first_code = ""
    i = 1
    WHILE i <= LEN(first_word)
        c = ASC(UPPER$(MID$(first_word, i, 1)))
        IF i = 1 THEN
            IF c = 77 THEN
                first_code = first_code + "M"
            ELSE
                first_code = first_code + CHR$(c)
            END IF
        ELSE
            IF c = 69 OR c = 73 OR c = 79 OR c = 85 THEN
                temp_str = RIGHT$(first_code, 1)
                IF temp_str <> "A" THEN
                    first_code = first_code + "A"
                END IF
            ELSEIF c = 65 THEN
                temp_str = RIGHT$(first_code, 1)
                IF temp_str <> "A" THEN
                    first_code = first_code + "A"
                END IF
            ELSEIF c = 81 THEN
                first_code = first_code + "G"
            ELSEIF c = 90 THEN
                first_code = first_code + "S"
            ELSEIF c = 77 THEN
                first_code = first_code + "N"
            ELSEIF c = 75 THEN
                first_code = first_code + "C"
            ELSEIF c = 83 THEN
                first_code = first_code + "S"
            ELSE
                first_code = first_code + CHR$(c)
            END IF
        END IF
        i = i + 1
    WEND

    PRINT "McDonald -> " + first_code
    temp_str = "M"
    i = 1
    WHILE i <= 6 AND i <= LEN(first_code)
        temp_str = temp_str + MID$(first_code, i, 1)
        i = i + 1
    WEND

    ' Encode "Johnson"
    first_word = "Johnson"
    first_code = ""
    i = 1
    WHILE i <= LEN(first_word)
        c = ASC(UPPER$(MID$(first_word, i, 1)))
        IF i = 1 THEN
            first_code = first_code + "J"
        ELSE
            IF c = 69 OR c = 73 OR c = 79 OR c = 85 THEN
                temp_str = RIGHT$(first_code, 1)
                IF temp_str <> "A" THEN
                    first_code = first_code + "A"
                END IF
            ELSEIF c = 65 THEN
                temp_str = RIGHT$(first_code, 1)
                IF temp_str <> "A" THEN
                    first_code = first_code + "A"
                END IF
            ELSEIF c = 81 THEN
                first_code = first_code + "G"
            ELSEIF c = 90 THEN
                first_code = first_code + "S"
            ELSEIF c = 83 THEN
                first_code = first_code + "S"
            ELSEIF c = 78 THEN
                first_code = first_code + "N"
            ELSE
                first_code = first_code + CHR$(c)
            END IF
        END IF
        i = i + 1
    WEND
    PRINT "Johnson -> " + first_code

    PRINT ""
    PRINT "NYSIIS demo complete."

    FUNCTION = 0
END FUNCTION
