GW BASIC chatbot with DATABASE.TXT file

85 views Asked by At

I'm trying to code a simple GW Basic chatbot that reads keywords and replies and default replies from a text file. Here is the example DATABASE.TXT file

============= GW BASIC CHATBOT DATABASE =============

========= TESTS ROUTINE ===========

k:hello
k:hi

r:hi hello how are you?
r:good day nice to talk to you
r:how are you doing this lovely day?

k:sad
k:lonely

r:well i'm here with you let's talk?
r:what's sitting on your mind?

k:bad day

r:it's just a feeling that will pass cheer up
r:it's just a bad day not a bad life...

d:go on i'l listening
d:i see
d:i think i can understand
d:can you explain more?

And here is the code that I try to implement:

40 REM PROGRAM INITIALIZATION
50 DIM K$(100),R$(100),D$(20): KCOUNT=0: RCOUNT=0: DCOUNT=0:FLINE$=""
60 DIM KCS(100),RCS(100),DCS(20),KCE(100),RCE(100)
70 DATAFILE$="DATABASE.TXT"
80 REM LOAD DATABASE
82 ISKEYWORD = 0: KEYWORDNUM = 1: REPLYNUM = 1
90 OPEN "I",#1,DATAFILE$
100 WHILE NOT EOF(1)
110 LINE INPUT #1,FLINE$
120 IF LEN(FLINE$)>0 THEN TAGP = INSTR(FLINE$,":")
130 IF TAGP>0 THEN TAG$=LEFT$(FLINE$,1) : STEXT$=MID$(FLINE$,TAGP+1)
140 REM
150 IF TAG$="k" AND ISKEYWORD = 0 THEN ISKEYWORD=1 : RCE(RCOUNT)= REPLYNUM : RCOUNT=RCOUNT+1 : RCS(RCOUNT) = REPLYNUM
160 IF TAG$="k" AND ISKEYWORD = 1 THEN K$(KEYWORDNUM)=" "+STEXT$+" " : KEYWORDNUM=KEYWORDNUM+1
165 IF TAG$="r" AND ISKEYWORD = 1 THEN ISKEYWORD=0 : KCE(KCOUNT)= KEYWORDNUM : KCOUNT=KCOUNT+1 : KCS(KCOUNT) = KEYWORDNUM
170 IF TAG$="r" AND ISKEYWORD = 0 THEN R$(REPLYNUM)=STEXT$ : REPLYNUM=REPLYNUM+1
175 IF TAG$="k" AND ISKEYWORD = 0 THEN ISKEYWORD=1 : RCE(RCOUNT)= REPLYNUM : RCOUNT=RCOUNT+1 : RCS(RCOUNT) = REPLYNUM
180 IF TAG$="d" THEN D$(DCOUNT)=STEXT$ : DCOUNT=DCOUNT+1
190 WEND
200 CLOSE #1

250 PRINT "Hello! How can I assist you today?"
260 INPUT "YOU: ", USERINPUT$
270 REM
280 found = 0
290 FOR I = 1 TO KCOUNT
300   FOR J = KCS(I) TO KCE(I)
310     IF INSTR(USERINPUT$, K$(J)) > 0 THEN PRINT "BOT: ";R$(RCS(I) + INT(RND * (RCE(I) - RCS(I) + 1))): found = 1
320       REM
330       REM
340     REM
350   NEXT J
360 NEXT I
370 IF found = 0 THEN PRINT D$(INT(RND * DCOUNT) + 1)
380 GOTO 260

The code works but not as I expected: for the first keywords in the first keywords - replies pair, I get replies from the second or third keywords/replies pair or default replies or an empty string. I suspect there are several bugs in my code. Some in the "read database.txt file and load the arrays" and some in the chatbot input processing loop

1

There are 1 answers

0
ron77 On BEST ANSWER

Well, after many attempts, I decided to simplify the database and the code - instead of multi keywords/replies pairs a database with just ONE keyword to THREE replies pairs and use a 2D array for the R$ array of replies - this is what I've come up with, and it works:

DATABASE.TXT

======== DATABASE GW CHATBOT 2 =============

=========ONE KEYWORD THREE REPLIES==========

======== TEST DRIVE ==============

k:hi

r:hello
r:hi there
r:how are you?

k:hello

r:hi hello how are you?
r:hi nice to meet you
r:hello how is your day coming along?

k:sad

r:don't be sad it will be alright
r:why are you sad?
r:whats wrong? you can tell me...

k:lonely day

r:yes we are all alone in the world
r:is that how you feel? just remember this too shell pass
r:i'm sorry you feel like that i wish i could help

k:bad day

r:i hope your day gets better
r:everyone has bad bays tomorrow will be better
r:hang on i'm sure tomorrow will be better

d:i see
d:go on please
d:i'm listening
d:i think i understand

AND THE CHATBOT CODE:

40 REM PROGRAM INITIALIZATION
50 DIM K$(100),R$(100, 3),D$(20): DCOUNT=1:FLINE$="":TAG$="":STEXT$="":TAGP=0:KNUM=0
60 REM 
70 DATAFILE$="DATABASE.TXT": RANDOMIZE TIMER
80 REM LOAD DATABASE
82 KEYWORDNUM = 1: REPLYNUM = 1
90 OPEN "I",#1,DATAFILE$
100 WHILE NOT EOF(1)
110 LINE INPUT #1,FLINE$
120 IF LEN(FLINE$)>0 THEN TAGP = INSTR(FLINE$,":")
130 IF TAGP>0 THEN TAG$=LEFT$(FLINE$,1) : STEXT$=MID$(FLINE$,TAGP+1) ELSE TAG$=""
140 IF TAG$="k" THEN K$(KEYWORDNUM)=" "+ STEXT$+ " ": REPLYNUM = 1: KEYWORDNUM=KEYWORDNUM+1
150 IF TAG$="r" THEN R$(KEYWORDNUM - 1, REPLYNUM)=STEXT$: REPLYNUM=REPLYNUM+1
160 IF TAG$="d" THEN DCOUNT=DCOUNT+1: D$(DCOUNT)=STEXT$
170 WEND
180 CLOSE #1

190 REM MAIN LOOP
200 PRINT "============================="
210 INPUT "YOU: ", USERINPUT$
211 USERINPUT$ = " " + USERINPUT$ + " "
220 IF USERINPUT$ = " BYE " THEN GOTO 500
230 KFOUND = 0
240 FOR I = 1 TO KEYWORDNUM - 1
250   IF INSTR(USERINPUT$,K$(I)) > 0 THEN KFOUND = 1: KNUM = I: GOTO 300
260 NEXT I
270 IF KFOUND = 0 THEN GOTO 400

300 REM KEYWORD FOUND, SELECT A RANDOM REPLY
310 RNUM = INT(RND * 3) + 1
320 PRINT "BOT: " + R$(KNUM, RNUM)
390 GOTO 190


400 REM KEYWORD NOT FOUND, USE DEFAULT REPLY
410 DNUM = INT(RND * DCOUNT) + 1
420 PRINT "BOT: " + D$(DNUM)
430 GOTO 190

500 REM EXIT
510 PRINT "BOT: GOODBYE!"
520 END