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
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
AND THE CHATBOT CODE: