Author Topic: Re: a simple chat bot DEMO [still in progress]  (Read 1483 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: a simple chat bot DEMO [still in progress]
« on: February 16, 2019, 10:10:10 pm »
Hi Ron, welcome to the forum!

You might be interested in another chat bot start: https://www.qb64.org/forum/index.php?topic=836.msg100455#msg100455
It asks user how it should respond to some text and saves it for the next time.

I got it setup to perform some basic functions, so far "show data" command dumps all it's memory of replies to make.

Your code had allot of blank lines, I removed them:
Code: QB64: [Select]
  1. _TITLE "CHATBOT 11 V 1.0"
  2. 'A SIMPLE CHATBOT DEVELOPED BY RON77 2/17/2019
  3. CONST FALSE = 0
  4. CONST TRUE = 1
  5. DIM txt$
  6. 'REM $DYNAMIC
  7. CONST TEXTFILE = "CHATBOT11 LOG.txt" '<<<<<<<<LOG FILE OF CONVERSATION
  8. OPEN TEXTFILE FOR APPEND AS #1 '<<<<<<<<<<<<<<<<<< in case a file is not started yet
  9. DIM SHARED user$
  10. user$ = GetName
  11. DIM SHARED Match
  12. 'ARRAYS OF INPUT/OUTPUT ADD AD MUCH AS YOU LIKE
  13. DIM SHARED noAnswer(3) AS STRING
  14. noAnswer(1) = "sorry don't know what to say": noAnswer(2) = "good luck with that": noAnswer(3) = "sorry my code is still being developed"
  15. DIM SHARED inputTest1(3) AS STRING
  16. inputTest1(1) = "good morning": inputTest1(2) = "good day": inputTest1(3) = "to meet you"
  17. DIM SHARED outputTest1(3) AS STRING
  18. outputTest1(1) = "thank you, i hope your'e having a good day having fun. ": outputTest1(2) = "nice to meet you too!. ": outputTest1(3) = "i wish you a good day and pleasent dreams at night. "
  19. DIM SHARED inputTest2(3) AS STRING
  20. inputTest2(1) = "say hello to my friend": inputTest2(2) = "tell us who are you": inputTest2(3) = "how are you doing"
  21. DIM SHARED outputTest2(3) AS STRING
  22. outputTest2(1) = "hello i'm chatbot number 11 writing in qbasic64 :) ": outputTest2(2) = "hello itay this is a demo on qbasic 64 program :) ": outputTest2(3) = "i'm doning better and better thank you! i just wish you whould sleep more and better at nights... "
  23. DIM SHARED inputBotTest1(3) AS STRING
  24. inputBotTest1(1) = "how was your day?": inputBotTest1(2) = "what did you do": inputBotTest1(3) = "how are things"
  25. DIM SHARED outputBotTest1(3) AS STRING
  26. outputBotTest1(1) = "it was okay and how are you? " + user$ + " ": outputBotTest1(2) = "i'm okay and how was your day? did you get a good night sleep or did you code all night? ": outputBotTest1(3) = "i'm fine "
  27.     INPUT ">", txt$
  28.     OPEN TEXTFILE FOR APPEND AS #1
  29.     WRITE #1, txt$
  30.     CLOSE #1
  31.     y = CSRLIN
  32.     x = POS(0)
  33.     COLOR 31: LOCATE y, x: PRINT "THINKING...";
  34.     SLEEP 2
  35.     COLOR 0: LOCATE y, x: PRINT "THINKING...": COLOR 7
  36.     IF txt$ <> "" THEN
  37.         userQuestions (txt$)
  38.     ELSE
  39.         eSpeak "input must not be empty!"
  40.     END IF
  41. LOOP UNTIL UCASE$(txt$) = "GOODBYE"
  42. PRINT "GOODBYE!"
  43. 'TTS SUB NEED TO HAVE FILE "ESPEAK.EXE" INSIDE EXE FOLDER TO WORK OTHERWISE NO VOICE
  44. SUB eSpeak (lines$)
  45.     PRINT UCASE$(lines$): PRINT
  46.     SHELL _HIDE "espeak " + CHR$(34) + lines$ + CHR$(34)
  47.     OPEN TEXTFILE FOR APPEND AS #1
  48.     WRITE #1, lines$
  49.     CLOSE #1
  50. SUB userQuestions (txt$)
  51.     txt$ = LCASE$(txt$)
  52.     Match = FALSE
  53.     re1$ = checkInput(txt$, inputTest1(), outputTest1())
  54.     re2$ = checkInput(txt$, inputTest2(), outputTest2())
  55.     re3$ = checkInput(txt$, inputBotTest1(), outputBotTest1())
  56.     'ADD AS MUCH AS YOU YOU LIKE
  57.     IF Match = TRUE THEN
  58.         eSpeak re1$ + re2$ + re3$ '+ MORE AS YOU LIKE
  59.     ELSE
  60.         r = INT(RND * UBOUND(noAnswer)) + 1
  61.         eSpeak noAnswer(r)
  62.     END IF
  63. 'FUNCTION TO GET USER NAME ON START
  64. FUNCTION GetName$
  65.     eSpeak "what is your name?: "
  66.     INPUT ">", name$: PRINT
  67.     OPEN TEXTFILE FOR APPEND AS #1
  68.     WRITE #1, name$
  69.     CLOSE #1
  70.     eSpeak "hello " + name$
  71.     GetName$ = name$
  72. 'FUNCTION WITH FOR LOOPS TO CHECK ARRAYS OF INPUT AND GENERATE RANDOM REPLY IF THERE IS A MATCH
  73. FUNCTION checkInput$ (in AS STRING, inputArray() AS STRING, outputArray() AS STRING)
  74.     FOR i = 1 TO UBOUND(inputArray)
  75.         k = INSTR(in, inputArray(i))
  76.         IF k >= 1 THEN
  77.             r = INT(RND * UBOUND(outputArray)) + 1
  78.             out$ = outputArray(r)
  79.             checkInput$ = out$
  80.             Match = TRUE
  81.         END IF
  82.     NEXT
  83. 'AND THAT'S ALL FOLKS!
  84. 'THANK YOU AND HOPE SOMEONE WILL FIND IT USEFULL OR EVEN ADD TO IT...
  85.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: a simple chat bot DEMO [still in progress]
« Reply #1 on: February 17, 2019, 01:50:42 am »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline 970037201

  • Newbie
  • Posts: 2
    • View Profile
Re: a simple chat bot DEMO [still in progress]
« Reply #2 on: March 13, 2019, 05:48:20 pm »
_TITLE "PROGRAMMED BY ERIC PETERSEN!"
SCREEN _NEWIMAGE(1080, 920, 32)
_FULLSCREEN
DEFINT A-Z
PRINT "ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»"
PRINT "º°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°º"
PRINT "º°±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±°º"
PRINT "º°±²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²±°º"
PRINT "º°±²ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ²±°º"
PRINT "º°±²ÛÅÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÁÄÅÛ²±°º"
PRINT "º°±²Û´                                                 ÃÛ²±°º"
PRINT "º°±²Û´                ELIZA PROGRAMMED IN:             ÃÛ²±°º"
PRINT "º°±²Û´                                                 ÃÛ²±°º"
PRINT "º°±²Û´                    ÉÍÍÍÍÍÍÍÍ»                   ÃÛ²±°º"
PRINT "º°±²Û´                    ºÉÍÍÍÍÍÍ»º                   ÃÛ²±°º"
PRINT "º°±²Û´                    ººÉÍÍÍÍ»ºº                   ÃÛ²±°º"
PRINT "º°±²Û´                    ºººQB64ººº                   ÃÛ²±°º"
PRINT "º°±²Û´                    ººÈÍÍÍͼºº                   ÃÛ²±°º"
PRINT "º°±²Û´                    ºÈÍÍÍÍÍͼº                   ÃÛ²±°º"
PRINT "º°±²Û´                    ÈÍÍÍÍÍÍÍͼ                   ÃÛ²±°º"
PRINT "º°±²Û´                                                 ÃÛ²±°º"
PRINT "º°±²Û´          TYPE any phrase with + TO EXIT         ÃÛ²±°º"
PRINT "º°±²Û´                                                 ÃÛ²±°º"
PRINT "º°±²Û´           RECOMPILED BY: ERIC PETERSEN          ÃÛ²±°º"
PRINT "º°±²Û´                                                 ÃÛ²±°º"
PRINT "º°±²ÛÅÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÅÛ²±°º"
PRINT "º°±²ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ²±°º"
PRINT "º°±²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²±°º"
PRINT "º°±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±°º"
PRINT "º°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°º"
PRINT "ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ"

PRINT: PRINT: PRINT

'' INITIALIZATION
DIM S(36), R(36), N(36)
DIM KEYWORD$(36), WORDIN$(7)
DIM WORDOUT$(7), REPLIES$(112)
N1 = 36: N2 = 14: N3 = 112

FOR X = 1 TO N1
    READ KEYWORD$(X)
NEXT X
FOR X = 1 TO N2 / 2
    READ WORDIN$(X)
    READ WORDOUT$(X)
NEXT X
FOR X = 1 TO N3
    READ REPLIES$(X)
NEXT X
FOR X = 1 TO N1
    READ S(X), L
    R(X) = S(X)
    N(X) = S(X) + L - 1
NEXT X

PRINT "HI! I'M ELIZA. WHAT'S WRONG WITH YOU?"
LABEL_HI:

'' USER INPUT SECTION
200 INPUT I$
I$ = UCASE$(I$)
I$ = "  " + I$ + "  "

'' GET RID OF APOSTROPHES
FOR L = 1 TO LEN(I$)
    IF L + 1 > LEN(I$) THEN 250
    IF MID$(I$, L, 1) <> "+" THEN 250
    CLS
    PRINT "SEE YOU NEXT TIME!"
    _DELAY 1
    SYSTEM
    END
    250
NEXT L

IF I$ = P$ THEN
    PRINT "PLEASE DON''T REPEAT YOURSELF!"
    GOTO LABEL_HI
END IF

270
'' FIND KEYWORD$ IN I
FOR K = 1 TO N1
    FOR L = 1 TO LEN(I$) - LEN(KEYWORD$(K)) + 1 'missing $
        IF MID$(I$, L, LEN(KEYWORD$(K))) <> KEYWORD$(K) THEN 350
        IF K <> 13 THEN 349
        IF MID$(I$, L, LEN(KEYWORD$(29))) = KEYWORD$(29) THEN K = 29
        349
        F$ = KEYWORD$(K)
        GOTO 390
        350
    NEXT L
NEXT K
370 K = 36: GOTO 570

390
''
'' TAKE PART OF STRING AND CONJUGATE IT
''
C$ = " " + RIGHT$(I$, LEN(I$) - LEN(F$) - L + 1) + " "
FOR X = 1 TO N2 / 2
    FOR L = 1 TO LEN(C$) 'another $
        IF L + LEN(WORDIN$(X)) > LEN(C$) THEN 510 'another $
        IF MID$(C$, L, LEN(WORDIN$(X))) <> WORDIN$(X) THEN 510
        C$ = LEFT$(C$, L - 1) + WORDOUT$(X) + RIGHT$(C$, LEN(C$) - L - LEN(WORDIN$(X)) + 1)
        L = L + LEN(WORDOUT$(X))
        GOTO 540
        510
        IF L + LEN(WORDOUT$(X)) > LEN(C$) THEN 540

        IF MID$(C$, L, LEN(WORDOUT$(X))) <> WORDOUT$(X) THEN 540
        C$ = LEFT$(C$, L - 1) + WORDIN$(X) + RIGHT$(C$, LEN(C$) - L - LEN(WORDOUT$(X)) + 1)
        L = L + LEN(WORDIN$(X))
        540
    NEXT L
NEXT X
IF MID$(C$, 2, 1) = " " THEN
    C$ = RIGHT$(C$, LEN(C$) - 1) 'edit C$ not C
END IF
FOR L = 1 TO LEN(C$)
    557
    IF MID$(C$, L, 1) = "!" THEN
        C$ = LEFT$(C$, L - 1) + RIGHT$(C$, LEN(C$) - L)
        GOTO 557
    END IF
NEXT L

570
F$ = REPLIES$(R(K))
R(K) = R(K) + 1
IF R(K) > N(K) THEN R(K) = S(K)
IF RIGHT$(F$, 1) <> "*" THEN
    PRINT F$
    P$ = I$
    GOTO LABEL_HI
END IF
IF C$ <> "   " THEN 630
PRINT "YOU WILL HAVE TO ELABORATE MORE FOR ME TO HELP YOU"
GOTO LABEL_HI

630
PRINT LEFT$(F$, LEN(F$) - 1); C$ 'edit C$ not C, oh F to F$ too, left$ not left
P$ = I$
GOTO LABEL_HI

'' === DATA ===

'' KEYWORDS
DATA "CAN YOU ","CAN I ","YOU ARE ","YOU''RE "
DATA "I DON''T ","I FEEL "
DATA "WHY DON''T YOU ","WHY CAN''T I ","ARE YOU "
DATA "I CAN''T ","I AM ","I''M "
DATA "YOU ","I WANT ","WHAT ","HOW ","WHO "
DATA "WHERE ","WHEN ","WHY "
DATA "NAME ","CAUSE ","SORRY ","DREAM "
DATA "HELLO ","HI ","MAYBE "
DATA "NO","YOUR ","ALWAYS ","THINK "
DATA "ALIKE ","YES ","FRIEND "
DATA "COMPUTER","NOKEYFOUND"

'' STRING DATA FOR CONJUGATIONS
DATA " ARE "," AM "," WERE "," WAS "
DATA " YOU "," I "," YOUR"," MY "
DATA " I''VE "," YOU''VE "," I''M "," YOU''RE "
DATA " ME "," YOU "

'' REPLIES$
DATA "DON''T YOU BELIEVE THAT I CAN*"
DATA "PERHAPS YOU WOULD LIKE TO BE LIKE ME*"
DATA "YOU WANT ME TO BE ABLE TO*"
DATA "PERHAPS YOU DON''T WANT TO*"
DATA "DO YOU WANT TO BE ABLE TO*"
DATA "WHAT MAKES YOU THINK I AM*"
DATA "DOES IT PLEASE YOU TO BELIEVE I AM*"
DATA "PERHAPS YOU WOULD LIKE TO BE*"
DATA "DO YOU SOMETIMES WISH YOU WERE*"
DATA "DON''T YOU REALLY*"
DATA "WHY DON''T YOU*"
DATA "DO YOU WISH TO BE ABLE TO*"
DATA "DOES THAT TROUBLE YOU*"
DATA "DO YOU OFTEN FEEL*"
DATA "DO YOU OFTEN FEEL*"
DATA "DO YOU ENJOY FEELING*"
DATA "DO YOU REALLY BELIEVE I DON''T*"
DATA "PERHAPS IN GOOD TIME I WILL*"
DATA "DO YOU WANT ME TO*"
DATA "DO YOU THINK YOU SHOULD BE ABLE TO*"
DATA "WHY CAN''T YOU*"
DATA "WHY ARE YOU INTERESTED IN WHETHER OR NOT I AM*"
DATA "WOULD YOU PREFER IF I WERE NOT*"
DATA "PERHAPS IN YOUR FANTASIES I AM*"
DATA "HOW DO YOU KNOW YOU CAN''T*"
DATA "HAVE YOU TRIED?"
DATA "PERHAPS YOU CAN NOW*"
DATA "DID YOU COME TO ME BECAUSE YOU ARE*"
DATA "HOW LONG HAVE YOU BEEN*"
DATA "DO YOU BELIEVE IT IS NORMAL TO BE*"
DATA "DO YOU ENJOY BEING*"
DATA "WE WERE DISCUSSING YOU--NOT ME."
DATA "OH, I*"
DATA "YOU''RE NOT REALLY TALKING ABOUT ME, ARE YOU?"
DATA "WHAT WOULD IT MEAN TO YOU IF YOU GOT*"
DATA "WHY DO YOU WANT*"
DATA "SUPPOSE YOU SOON GOT*"
DATA "WHAT IF YOU NEVER GOT*"
DATA "I SOMETIMES ALSO WANT*"
DATA "WHY DO YOU ASK?"
DATA "DOES THAT QUESTION INTEREST YOU?"
DATA "WHAT ANSWER WOULD PLEASE YOU THE MOST?"
DATA "WHAT DO YOU THINK?"
DATA "ARE SUCH QUESTIONS ON YOUR MIND OFTEN?"
DATA "WHAT IS IT THAT YOU REALLY WANT TO KNOW?"
DATA "HAVE YOU ASKED ANYONE ELSE?"
DATA "HAVE YOU ASKED SUCH QUESTIONS BEFORE?"
DATA "WHAT ELSE COMES TO MIND WHEN YOU ASK THAT?"
DATA "NAMES DON''T INTEREST ME."
DATA "I DON''T CARE ABOUT NAMES --PLEASE GO ON."
DATA "IS THAT THE REAL REASON?"
DATA "DON''T ANY OTHER REASONS COME TO MIND?"
DATA "DOES THAT REASON EXPLAIN ANYTHING ELSE?"
DATA "WHAT OTHER REASONS MIGHT THERE BE?"
DATA "PLEASE DON''T APOLOGIZE!"
DATA "APOLOGIES ARE NOT NECESSARY."
DATA "WHAT FEELINGS DO YOU HAVE WHEN YOU APOLOGIZE?"
DATA "DON''T BE SO DEFENSIVE!"
DATA "WHAT DOES THAT DREAM SUGGEST TO YOU?"
DATA "DO YOU DREAM OFTEN?"
DATA "WHAT PERSONS APPEAR IN YOUR DREAMS?"
DATA "ARE YOU DISTURBED BY YOUR DREAMS?"
DATA "HOW DO YOU DO ...PLEASE STATE YOUR PROBLEM."
DATA "YOU DON''T SEEM QUITE CERTAIN."
DATA "WHY THE UNCERTAIN TONE?"
DATA "CAN''T YOU BE MORE POSITIVE?"
DATA "YOU AREN''T SURE?"
DATA "DON''T YOU KNOW?"
DATA "ARE YOU SAYING NO JUST TO BE NEGATIVE?"
DATA "YOU ARE BEING A BIT NEGATIVE."
DATA "WHY NOT?"
DATA "ARE YOU SURE?"
DATA "WHY NO?"
DATA "WHY ARE YOU CONCERNED ABOUT MY*"
DATA "WHAT ABOUT YOUR OWN*"
DATA "CAN YOU THINK OF A SPECIFIC EXAMPLE?"
DATA "WHEN?"
DATA "WHAT ARE YOU THINKING OF?"
DATA "REALLY, ALWAYS?"
DATA "DO YOU REALLY THINK SO?"
DATA "BUT YOU ARE NOT SURE YOU*"
DATA "DO YOU DOUBT YOU*"
DATA "IN WHAT WAY?"
DATA "WHAT RESEMBLANCE DO YOU SEE?"
DATA "WHAT DOES THE SIMILARITY SUGGEST TO YOU?"
DATA "WHAT OTHER CONNECTIONS DO YOU SEE?"
DATA "COULD THERE REALLY BE SOME CONNECTION?"
DATA "HOW?"
DATA "YOU SEEM QUITE POSITIVE."
DATA "ARE YOU SURE?"
DATA "I SEE."
DATA "I UNDERSTAND."
DATA "WHY DO YOU BRING UP THE TOPIC OF FRIENDS?"
DATA "DO YOUR FRIENDS WORRY YOU?"
DATA "DO YOUR FRIENDS PICK ON YOU?"
DATA "ARE YOU SURE YOU HAVE ANY FRIENDS?"
DATA "DO YOU IMPOSE ON YOUR FRIENDS?"
DATA "PERHAPS YOUR LOVE FOR FRIENDS WORRIES YOU."
DATA "DO COMPUTERS WORRY YOU?"
DATA "ARE YOU TALKING ABOUT ME IN PARTICULAR?"
DATA "ARE YOU FRIGHTENED BY MACHINES?"
DATA "WHY DO YOU MENTION COMPUTERS?"
DATA "WHAT DO YOU THINK MACHINES HAVE TO DO WITH YOUR PROBLEM?"
DATA "DON''T YOU THINK COMPUTERS CAN HELP PEOPLE?"
DATA "WHAT IS IT ABOUT MACHINES THAT WORRIES YOU?"
DATA "SAY, DO YOU HAVE ANY PSYCHOLOGICAL PROBLEMS?"
DATA "WHAT DOES THAT SUGGEST TO YOU?"
DATA "I SEE."
DATA "I''M NOT SURE I UNDERSTAND YOU FULLY."
DATA "COM'ON! ELUCIDATE YOUR THOUGHTS."
DATA "CAN YOU ELABORATE ON THAT?"
DATA "THAT IS QUITE INTERESTING."

'' DATA FOR FINDING RIGHT REPLIES$
DATA 1,3,4,2,6,4,6,4,10,4,14,3,17
DATA 3,20,2,22,3,25,3
DATA 28,4,28,4,32,3,35,5,40,9,40
DATA 9,40,9,40,9,40,9,40,9
DATA 49,2,51,4,55,4,59,4,63,1,63
DATA 1,64,5,69,5,74,2,76,4
DATA 80,3,83,7,90,3,93,6,99,7,106,6
* ERIC PETERSEN ANNAOYING AI.bas (Filesize: 9.35 KB, Downloads: 57)

Marked as best answer by on August 11, 2021, 12:38:19 am

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: a simple chat bot DEMO [still in progress]
« Reply #4 on: March 14, 2019, 02:32:06 pm »
Back in the DOS days we called this ELISA. People paid money for that.  :D
Eh, I see now there is a big ELISA thread here. You all know this.
« Last Edit: March 14, 2019, 03:56:14 pm by Jack002 »
QB64 is the best!