'===========================================================================
' Subject: PARSE IT                           Date: Unknown Date (00:00)   
'  Author: Scott Bailey                       Code: QB, QBasic, PDS        
'  Origin: Parse,it                         Packet: TEXT.ABC
'===========================================================================
'PARSEIT by Scott Bailey
'Public Domain
'Extracts verb & noun from full
'sentence like in adventure games
'Probably could use some optimizing
DEFINT A-Z
DECLARE SUB parseit ()
'---------
'Main vars
'---------
'UserInput$=player's input
'V$=VERB returned
'N$=NOUN returned
'V=VERB num returned
'N=NOUN num returned
'Verb$()=VERB list
'Noun$()=NOUN list
'-------------------
'count the verbs & nouns so we know how
'big to make the arrays
DO
    ve = ve + 1
    READ in$
LOOP UNTIL in$ = "end"
DO
    no = no + 1
    READ in$
LOOP UNTIL in$ = "end"
RESTORE
'best to have them shared
DIM SHARED Verb$(ve), noun$(no), UserInput$, v$, n$, v, n

'all data must be in lowercase
'---------
'Verb data
'---------
DATA kill,throw,shoot,climb,look,inventory,hit,punch,kick
DATA slap,make,save,restore,sing,fly,jump,roll,play,score
DATA time,drink,swallow,poke,stab,roll,push,pull,press,say
DATA run,yell,scream,hop,pour,feed,sit,lay,lie,sleep,nap
DATA take,get,eat,drop,examine,search,dig,read,quit,kiss
DATA list
'data _must_ finish with end
DATA end
'---------
'Noun data
'---------
DATA bottle,bear,cup,self,myself,rock,gun,spear,chair,pouch
DATA light,lamp,water,wine,beer,bird,pop,soda,food,hamburger
DATA mouse,pizza,steak,soup,stew,fork,watch,sandwich
DATA spoon,knife,sword,dagger,tree,bush,lake,pond
DATA wolf,kobold,ogre,lion,computer,time,fly,mosquito
DATA man,woman,girl,boy,baby,radio,car,truck,train,book,tape
DATA pen,key,note,paper,pig,cow,house,mirror,television,floor
DATA ground,stairs,room,piano,banana,phone,quarter,dime
DATA nickel,penny,dollar,money,disk,oven,stove,clock,glass
DATA verbs,nouns
'data _must_ finish with end
DATA end

'get data
FOR count = 1 TO ve
    READ Verb$(count)
NEXT
FOR count = 1 TO no
    READ noun$(count)
NEXT
'----
'Main
'----
DO
LINE INPUT "What now> "; UserInput$
CLS
parseit
PRINT
PRINT "Verb:"; v$, "Noun:"; n$
PRINT "Verb#"; v, "Noun#"; n
PRINT
'some examples
IF v = 0 THEN PRINT "Please rephrase that."
IF v = 49 THEN END  'quit
IF v = 12 THEN PRINT "SAVE the "; n$; "?"
IF v = 13 THEN PRINT "There is nothing to restore!"
IF v = 6 THEN PRINT "You are not carrying anything yet!"

IF v = 51 AND n = 83 THEN   'command: 'LIST VERBS'
    FOR k = 1 TO ve
        PRINT Verb$(k),
    NEXT
    PRINT
END IF

IF v = 51 AND n = 84 THEN   'command: 'LIST NOUNS'
    FOR k = 1 TO no
        PRINT noun$(k),
    NEXT
    PRINT
END IF

SELECT CASE v
    CASE 1, 2, 3, 7, 8, 9, 10, 24 'response to violence
    PRINT "You can not "; v$;
    IF n THEN
        PRINT " the "; n$; "!"
    ELSE
        PRINT " anything!"
    END IF
END SELECT
v$ = "": n$ = "": v = 0: n = 0
LOOP

SUB parseit
UserInput$ = LCASE$(UserInput$) + " "
'--------------
'Parse sentence
'-----------------------------
'The first 3 letters of a verb
'and the first 4 letters of a
'noun is all that is needed.
'-----------------------------
DO WHILE LEN(UserInput$)
  FOR ve = 1 TO LEN(UserInput$)
    Char$ = MID$(UserInput$, ve, 1)
    IF Char$ = " " OR Char$ = "!" OR Char$ = "." OR Char$ = ","+_
"" THEN
        VrbHold$ = LEFT$(UserInput$, ve - 1)
        UserInput$ = MID$(UserInput$, ve + 1)
        Count1 = 1
        Count2 = 1
        DO  'get verb
        IF LEFT$(VrbHold$, 3) = LEFT$(Verb$(Count1), 3) THEN
            v$ = Verb$(Count1)
            v = Count1
            ve = 1
        END IF
        Count1 = Count1 + 1
        IF Verb$(Count1) = "end" THEN EXIT DO
        LOOP

        DO  'get noun
            IF LEFT$(VrbHold$, 4) = LEFT$(noun$(Count2), 4) THEN
                n$ = noun$(Count2)
                n = Count2
                EXIT SUB
            END IF
            Count2 = Count2 + 1
            IF noun$(Count2) = "end" THEN EXIT DO
        LOOP
    END IF
  NEXT
LOOP
END SUB
