Rem Project: chatbot Rem Created: 15/01/2007 12:53:35 Rem ***** Main Source File ***** global dim word$(1,1,1) global maxx global dim maxy(1) global dim maxz(1,1) global dim word$(15,15,5) gosub worddata makewordbank(worddata$) print "Hi there." do input "> ",input$ cls gosub response sync loop response: `parse input$ for keywords with lengths from 3 to 8 letters found=0 length=len(input$) if length>=10 then wordlength=10 else wordlength=length for a=1 to length for b=2 to wordlength if a+b-1<=length test$=right$(left$(input$,a+b-1),b) category=0 for x=1 to maxx if word$(x,0,0)=test$ `print word$(x,0,0) category=x `print category exit endif next x if category>0 for c=1 to length for d=2 to wordlength if c+d-1<=length test$=right$(left$(input$,c+d-1),d) `print test$ for y=1 to maxy(category) if word$(category,y,0)=test$ response$=word$(category,y,1+rnd(maxz(category,y)-1)) if left$(response$,3)="key" keynumber$=right$(response$,1) if keynumber$="1" response$="No, you "+input$ endif if keynumber$="2" Print "who's there?" input answer$ print answer$, "who?" input answer$ response$=answer$+"? That's the worst joke I ever heard!" endif if keynumber$="3" print "sorry, what did you say your name was?" input name$ response$="Ah. Hi there "+name$ endif endif print response$ found=1 endif if found=1 then exit next y endif if found=1 then exit next d if found=1 then exit next c endif endif if found=1 then exit next b if found=1 then exit next a if found=0 `no standard response found if name$="" print "Mmm. What's your name?" input name$ print "Hi ",name$,". How are you today?" input howiam$ response$="I'm "+howiam$+" too." else reply=rnd(4) if reply=0 then response$="That's an odd thing to say, don't you think?" if reply=1 then response$="Why do you say that?" if reply=2 randomobject=rnd(9) if randomobject=0 then object$="dogs" if randomobject=1 then object$="penguins" if randomobject=2 then object$="chips" if randomobject=3 then object$="watching tv" if randomobject=4 then object$="writing computer programs" if randomobject=5 then object$="travelling" if randomobject=6 then object$="your job" if randomobject=7 then object$="playing games" if randomobject=8 then object$="computers" if randomobject=9 then object$="shopping" print "I don't know much about that. Do you like ",object$,"?" input answer$ if answer$="yes" then response$="Tell me more about "+object$ if answer$="no" then response$="Good. I hate "+object$+" too!" if answer$<>"yes" and answer$<>"no" then response$="That's interesting to know" endif if reply=3 then response$="Your talking a different language!" if reply=4 a=rnd(100) b=rnd(100) response$="Did you know that "+str$(a)+"x"+str$(b)+"="+str$(a*b)+"?" endif endif print response$ endif if response$=oldresponse$ then print "I seem to be repeating myself!" oldresponse$=response$ return function makewordbank(worddata$) length=len(worddata$) x=1 maxx=1 for test=1 to length character$=right$(left$(worddata$,test),1) letter=0 if character$<>"(" if character$<>"," if character$<>")" letter=1 endif endif endif donothing=0 if letter=1 `if the character is a letter, then it isn't the end of a word, so add letter to word word$=word$+character$ else if character$="," and oldcharacter$=")" then donothing=1 `if its a comma following a closed bracket if character$=")" and oldcharacter$=")" then donothing=1 `if its a closed bracket following a closed bracket if donothing=1 `do nothing else `else any other non letter signifies the end of the word `dim word$(maxx,overallmaxy+1,overallmaxz+1) `seems to delete array contents already stored - keep outside loop word$(x,y,z)=word$:word$="" `so store the word at x,y,z and clear to start the next one endif if character$="(" inc bracket `bracket is the integer number of current open brackets if bracket>oldbracket and bracket=1 `if first open bracket, then need new item inc y if y>maxy(x) maxy(x)=y `store number of items within that title endif if y>overallmaxy overallmaxy=y dim maxz(maxx,overallmaxy) `store highest number of items for any title, to use for array dimension endif endif if bracket>oldbracket and bracket=2 `if second open bracket, need new subitem inc z if z>overallmaxz then overallmaxz=z if z>maxz(x,y) maxz(x,y)=z `store number of subitems within that item endif endif endif if character$=")" dec bracket if bracket=0 then y=0:z=0 if bracket=1 then z=0 endif if character$="," if bracket=0 inc x if x>maxx maxx=x dim maxy(maxx) `store number of titles dim maxz(maxx,overallmaxy) endif endif if bracket=1 inc y if y>maxy(x) maxy(x)=y `store number of items within that title endif if y>overallmaxy overallmaxy=y dim maxz(maxx,overallmaxy) `store highest number of items for any title, to use for array dimension endif endif if bracket=2 inc z if z>overallmaxz then overallmaxz=z if z>maxz(x,y) maxz(x,y)=z `store number of subitems within that item endif endif endif endif oldbracket=bracket oldcharacter$=character$ next test endfunction worddata: worddata$="weather(weather(It's always cold where I live)),are(you(No I certainly am not!)),my(name(key3)),knock(knock(key2)),hi(hi(Hello again!)),hello(hello(Hi)),yes(yes(I'm glad we're in agreement. Tell me a joke)),off(off(Are you being rude?,key1)),go(away(No.,I was here first. You go away,You bored already? I've only just started!)),I'm(bored(I'm a bit bored too.),I'm(It's all about you - isn't it.,Let's talk about me.))" worddata$=worddata$+",do(like(I love it,No - I hate it,It's okay I suppose)),no(no(That's a bit negative.,All you ever say is no!)),what(job(My job is to serve you!,I compute stuff),living(I just sit in a metal box all day),favourite(I like many different kinds,I don't really have a preference),name(I don't have a name - I'm a computer.),age(Not as old as you.),hobbies(I like playing with numbers - what about you?),hobby(I like playing with numbers.),animal(I like dogs,I really love cats)),where(live(In a metal box.),born(In a factory),from(A shop.)),how(you(I'm very well thanks. How are you?),old(That's personal - dont you think?),going(Not too bad),know(I'm clever))" worddata$=worddata$+",who(made(Ric made me),programmed(Ric programmed me),friend(I have lot's of friends),favourite(I don't really know any I'm afraid))" return