#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL word AS STRING
    LOCAL code AS STRING
    LOCAL i AS LONG
    LOCAL c AS LONG
    LOCAL prev_c AS LONG
    LOCAL temp_str AS STRING

    ' Simple Metaphone: encode words to phonetic key
    ' Rules for consonant clusters and vowel handling

    PRINT "Metaphone Phonetic Encoding"
    PRINT "==========================="
    PRINT ""

    word = "KNIGHT"
    code = ""
    i = 1

    WHILE i <= LEN(word) AND LEN(code) < 6
        c = ASC(UPPER$(MID$(word, i, 1)))

        IF i = 1 THEN
            ' Initial consonant clusters
            IF c = 75 AND i + 1 <= LEN(word) AND ASC(UPPER$(MID$(word, i + 1, 1))) = 78 THEN
                code = code + "N"
                i = i + 2
            ELSEIF c = 71 AND i + 1 <= LEN(word) AND ASC(UPPER$(MID$(word, i + 1, 1))) = 78 THEN
                code = code + "N"
                i = i + 2
            ELSEIF c = 80 AND i + 1 <= LEN(word) AND ASC(UPPER$(MID$(word, i + 1, 1))) = 78 THEN
                code = code + "N"
                i = i + 2
            ELSEIF c = 87 AND i + 1 <= LEN(word) AND ASC(UPPER$(MID$(word, i + 1, 1))) = 82 THEN
                code = code + "R"
                i = i + 2
            ELSEIF c = 88 THEN
                code = code + "S"
                i = i + 1
            ELSE
                i = i
            END IF
        END IF

        IF i <= LEN(word) THEN
            c = ASC(UPPER$(MID$(word, i, 1)))
        END IF

        IF i <= LEN(word) THEN
            IF c = 66 OR c = 70 OR c = 80 OR c = 86 THEN
                temp_str = RIGHT$(code, 1)
                IF temp_str <> "B" AND temp_str <> "F" AND temp_str <> "P" AND temp_str <> "V" THEN
                    code = code + CHR$(c)
                END IF
            ELSEIF c = 67 THEN
                prev_c = 0
                IF i > 1 THEN prev_c = ASC(UPPER$(MID$(word, i - 1, 1)))
                IF i + 1 <= LEN(word) AND ASC(UPPER$(MID$(word, i + 1, 1))) = 72 THEN
                    code = code + "X"
                    i = i + 1
                ELSEIF prev_c = 83 THEN
                    code = code + "X"
                ELSEIF i + 1 <= LEN(word) AND ASC(UPPER$(MID$(word, i + 1, 1))) = 73 AND i + 2 <= LEN(word) AND ASC(UPPER$(MID$(word, i + 2, 1))) = 65 THEN
                    code = code + "X"
                ELSE
                    code = code + "K"
                END IF
            ELSEIF c = 68 THEN
                IF i + 1 <= LEN(word) AND ASC(UPPER$(MID$(word, i + 1, 1))) = 71 THEN
                    code = code + "J"
                    i = i + 1
                ELSE
                    code = code + "T"
                END IF
            ELSEIF c = 71 THEN
                IF i + 1 <= LEN(word) AND ASC(UPPER$(MID$(word, i + 1, 1))) = 72 THEN
                    code = code + "F"
                    i = i + 1
                ELSE
                    code = code + "K"
                END IF
            ELSEIF c = 72 THEN
                IF RIGHT$(code, 1) <> "H" THEN
                    code = code + "H"
                END IF
            ELSEIF c = 74 THEN
                code = code + "J"
            ELSEIF c = 75 THEN
                code = code + "K"
            ELSEIF c = 76 THEN
                code = code + "L"
            ELSEIF c = 77 THEN
                code = code + "M"
            ELSEIF c = 78 THEN
                code = code + "N"
            ELSEIF c = 80 THEN
                code = code + "P"
            ELSEIF c = 81 THEN
                code = code + "Q"
            ELSEIF c = 82 THEN
                code = code + "R"
            ELSEIF c = 83 THEN
                code = code + "S"
            ELSEIF c = 84 THEN
                code = code + "T"
            ELSEIF c = 86 THEN
                code = code + "V"
            ELSEIF c = 87 THEN
                code = code + "W"
            ELSEIF c = 88 THEN
                code = code + "X"
            ELSEIF c = 89 THEN
                code = code + "Y"
            ELSEIF c = 90 THEN
                code = code + "Z"
            END IF
        END IF

        i = i + 1
    WEND

    PRINT "KNIGHT -> " + code
    PRINT "KNIGHT -> N (expected: NFT)?"

    word = "PHYSICS"
    code = ""
    i = 1
    WHILE i <= LEN(word) AND LEN(code) < 6
        c = ASC(UPPER$(MID$(word, i, 1)))
        IF c = 80 AND i + 1 <= LEN(word) AND ASC(UPPER$(MID$(word, i + 1, 1))) = 72 THEN
            code = code + "F"
            i = i + 2
        ELSEIF c = 89 THEN
            i = i + 1
        ELSE
            IF c = 66 OR c = 70 THEN code = code + "F"
            IF c = 67 THEN code = code + "K"
            IF c = 68 THEN code = code + "T"
            IF c = 71 THEN code = code + "K"
            IF c = 74 THEN code = code + "J"
            IF c = 75 THEN code = code + "K"
            IF c = 76 THEN code = code + "L"
            IF c = 77 THEN code = code + "M"
            IF c = 78 THEN code = code + "N"
            IF c = 80 THEN code = code + "P"
            IF c = 81 THEN code = code + "K"
            IF c = 82 THEN code = code + "R"
            IF c = 83 THEN code = code + "S"
            IF c = 84 THEN code = code + "T"
            IF c = 86 THEN code = code + "F"
            IF c = 87 THEN code = code + "W"
            IF c = 88 THEN code = code + "S"
            IF c = 90 THEN code = code + "S"
            i = i + 1
        END IF
    WEND
    PRINT "PHYSICS -> " + code

    PRINT ""
    PRINT "Metaphone demo complete."

    FUNCTION = 0
END FUNCTION
