!Takoma Park Middle School

!INTERMEDIATE-3

!ANDREW DAS SARMA

!CONTEST 3 06-07: SWEDISH CHEF

 

PRINT "This highly complex application translates words from English to the language of the mystical Swedish Chef."

FOR i = 1 to 5

    LINE INPUT prompt "English String? " : english$

    LET english$ = trim$(english$)

    IF english$[len(english$):len(english$)] <> "." AND english$[len(english$):len(english$)] <> "!" AND english$[len(english$):len(english$)] <> "?" then

       LET swedish$ = ""

       CALL do_word(ucase$(english$), swedish$)

       LET english$ = ""

    ELSE

       LET swedish$ = ""

       CALL do_sent(english$, swedish$)

       LET english$ = ""

    END IF

    PRINT "Swedish Chef says: ";swedish$

NEXT i

END

 

SUB do_sent(e$, s$)

    LET e$ = ucase$(trim$(e$))

    !PRINT "hi."

    LET punc$ = e$[len(e$):len(e$)]

    LET e$ = e$[1:len(e$)-1]

    !print e$

    DIM wa$(50)

    DO

       LET x = pos(e$, " ")

       LET k = k + 1

       IF x = 0 then

          EXIT DO

       END IF

       LET wa$(k) = e$[1:x-1]

       LET e$ = e$[x+1:len(e$)]

    LOOP

    LET wa$(k) = e$

    !mat print wa$

    FOR I = 1 to k

        IF wa$(i) <> "I" and wa$(i) <> "A" then

           LET y$ = ""

           CALL do_word(ucase$(trim$(wa$(i))), y$)

           LET wa$(i) = y$

        END IF

    NEXT i

    FOR i = 1 to k

        LET s$ = s$ & " " & wa$(i)

    NEXT i

    FOR i = 1 to len(s$)-2

        IF s$[i:i+2] = "THE" then

           LET s$[i:i+2] = "ZEE"

        END IF

    NEXT i

    LET s$ = s$ & punc$ & "  BORK BORK BORK!"

END SUB

 

 

SUB do_word(e$, s$)               !INT. EDITION

    DIM c(100, 2)                 !c(x, 1) = startchar; c(x, 2) = rule

    LET cb = 1

    DIM r$(14, 2)

    DIM alloc(9)

    MAT READ r$

    MAT READ alloc

    DATA "AN", "UN"

    DATA "AU", "OO"

    DATA "A", "E"

    DATA "OW", "OO"

    DATA "TION", "SHUN"

    DATA "IR", "UR"

    DATA "O", "U"

    DATA "I", "EE"

    DATA "EN", "EE"

    DATA "F", "FF"

    DATA "E", "E-A"

    DATA "U", "OO"

    DATA "V", "F"

    DATA "W", "V"

    !    MAT PRINT r$

    DATA 1, 2, 4, 5, 6, 7, 10, 13, 14

    FOR i = 1 to len(e$)

        LET u$ = u$ & "0"

    NEXT i

    LET kx = 1

    FOR i = 1 to 14

        IF alloc(kx) = i then

           LET kx = kx + 1

           FOR j = 1 to len(e$) - len(r$(i, 1)) + 1

               CALL chk_inst(r$(i, 1), e$, j, yesno)

               LET nx = 0

               FOR k = j to j + len(r$(i, 1))-1

                   LET nx = nx + val(u$[k:k])

               NEXT k

               IF yesno = 1 and nx = 0 then

                  LET c(cb, 1) = j

                  LET c(cb, 2) = i

                  LET cb = cb + 1

                  FOR k = j to j+len(r$(i, 1))-1

                      LET u$[k:k] = "1"

                  NEXT k

               END IF

           NEXT j

        ELSEIF i = 3 then

           FOR j = 1 to len(e$) - len(r$(i, 1))

               CALL chk_inst(r$(i, 1), e$, j, yesno)

               LET nx = 0

               FOR k = j to j + len(r$(i, 1))-1

                   LET nx = nx + val(u$[k:k])

               NEXT k

               IF yesno = 1 and nx = 0 then

                  LET c(cb, 1) = j

                  LET c(cb, 2) = i

                  LET cb = cb + 1

                  FOR k = j to j+len(r$(i, 1))-1

                      LET u$[k:k] = "1"

                  NEXT k

               END IF

           NEXT j

        ELSEIF i = 9 then

           IF val(u$[len(u$)-1:len(u$)-1])+val(u$[len(u$):len(u$)]) = 0 then

              IF e$[len(e$)-1:len(e$)] = "EN" then

                 LET c(cb, 1) = len(e$)-1

                 LET c(cb, 2) = 9

                 LET cb = cb + 1

                 LET u$[len(u$)-1:len(u$)] = "11"

              END IF

           END IF

        ELSEIF i = 11 then

           IF u$[len(u$):len(u$)] <> "1" then

              IF e$[len(e$):len(e$)] = "E" then

                 LET u$[len(u$):len(u$)]="1"

                 LET c(cb, 1) = len(e$)

                 LET c(cb, 2) = i

                 LET cb = cb + 1

              END IF

           END IF

        ELSEIF i = 8 then

           FOR j = 2 to len(e$) - len(r$(i, 1)) + 1

               CALL chk_inst(r$(i, 1), e$, j, yesno)

               LET nx = 0

               FOR k = j to j + len(r$(i, 1))-1

                   LET nx = nx + val(u$[k:k])

               NEXT k

               IF yesno = 1 and nx = 0 and sha = 0 then

                  LET c(cb, 1) = j

                  LET sha = 1

                  LET c(cb, 2) = i

                  LET cb = cb + 1

                  FOR k = j to j+len(r$(i, 1))-1

                      LET u$[k:k] = "1"

                  NEXT k

               END IF

           NEXT j

        ELSEIF i = 12 then

           FOR j = 2 to len(e$) - len(r$(i, 1)) + 1

               CALL chk_inst(r$(i, 1), e$, j, yesno)

               LET nx = 0

               FOR k = j to j + len(r$(i, 1))-1

                   LET nx = nx + val(u$[k:k])

               NEXT k

               IF yesno = 1 and nx = 0 and sha = 0 then

                  LET c(cb, 1) = j

                  LET sha = 1

                  LET c(cb, 2) = i

                  LET cb = cb + 1

                  FOR k = j to j+len(r$(i, 1))-1

                      LET u$[k:k] = "1"

                  NEXT k

               END IF

           NEXT j

        ELSE

           PRINT "WARNING:  IN TRANSLATE, i EQUALS";i;"(SHOULD BE INTEGER FROM 1-14)."

        END IF

    NEXT i

    LET s$ = e$

    FOR I = 1 TO size(c, 1)

        !print c(i, 1),c(i, 2)

        IF c(i+1, 1) = 0 then

           LET cutoff = i

           EXIT FOR

        END IF

    NEXT i

    FOR i = 1 to cutoff

        FOR j = 1 to cutoff - 1

            IF c(j, 1) > c(j+1, 1) then

               LET t = c(j, 1)

               LET q = c(j, 2)

               LET c(j, 1) = c(j+1, 1)

               LET c(j, 2) = c(j+1, 2)

               LET c(j+1, 1) = t

               LET c(j+1, 2) = q

            ELSEIF c(j, 1) = c(j+1, 1) then

               !print "ERROR.  SEE DUMP."

            END IF

        NEXT j

    NEXT i

    !print

    !print

    FOR i = 1 to cutoff

        !PRINT c(i, 1),c(i, 2)

    NEXT i

    !print

    !print

    IF c(1, 1) = 0 then

       LET cutoff = 0

    END IF

    FOR i = 1 to cutoff

        LET s$[c(i, 1):c(i, 1)+len(r$(c(i, 2), 1))-1] = r$(c(i, 2), 2)

        IF len(r$(c(i, 2), 2)) > len(r$(c(i, 2), 1)) then

           FOR j = i+1 to cutoff

               LET c(j, 1) = c(j, 1) + 1

           NEXT j

        END IF

    NEXT i

    !   PRINT e$

    !   PRINT u$

    !print s$

END SUB

 

SUB chk_inst(fstr$, x$, loc, yesno)

    IF x$[loc:loc+len(fstr$)-1] = fstr$ then

       LET yesno = 1

    ELSE

       LET yesno = 0

    END IF

END SUB