Author Topic: Simple Chat Bot  (Read 5852 times)

0 Members and 1 Guest are viewing this topic.

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Simple Chat Bot
« on: December 02, 2018, 08:58:33 pm »
Hey,
I got bored (as you do) and decided to make a very simple chat bot. Yes I know its not very effiecent, but it works ok.
By the way you can teach it to respond to anything you like :)

Code: QB64: [Select]
  1. DIM line$(1000000)
  2. OPEN "AIDatabase.txt" FOR INPUT AS #1
  3.     lines = lines + 1
  4.     INPUT #1, line$(lines)
  5.  
  6. PRINT "LOADED " + STR$(lines) + " ASSETS"
  7.  
  8.     tmpNull = 0
  9.     INPUT ">> ", in$
  10.     in$ = LCASE$(in$)
  11.  
  12.     FOR n = 1 TO lines STEP 2
  13.         IF in$ = line$(n) THEN
  14.             PRINT line$(n + 1)
  15.         ELSE
  16.             tmpNull = tmpNull + 2
  17.         END IF
  18.     NEXT n
  19.  
  20.     IF tmpNull = lines THEN
  21.         PRINT "How should I respond to that?"
  22.         INPUT ">> ", res$
  23.         OPEN "AIDatabase.txt" FOR APPEND AS #1
  24.         WRITE #1, in$
  25.         WRITE #1, res$
  26.         CLOSE #1
  27.  
  28.         FOR n = 1 TO lines
  29.             line$(n) = ""
  30.         NEXT n
  31.  
  32.         OPEN "AIDatabase.txt" FOR INPUT AS #1
  33.         WHILE NOT EOF(1)
  34.             lines = lines + 1
  35.             INPUT #1, line$(lines)
  36.         WEND
  37.         CLOSE #1
  38.     END IF

Zep.
+[--->++<]>+.+++[->++++<]>.[--->+<]>+.-[---->+<]>++.+[->+++<]>+.+++++++++++.----------.[--->+<]>----.+[---->+<]>+++.---[->++++<]>.------------.+.++++++++++.+[---->+<]>+++.+[->+++<]>++.[--->+<]>+.[->+++<]>+.++++++++++.+.>++++++++++.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simple Chat Bot
« Reply #1 on: December 03, 2018, 03:40:02 pm »
Hey nice start, I got bored and added some more powers to the bot.
Code: QB64: [Select]
  1. _TITLE "AI Chat Bot Z" ' started by Zeppelin 2018-12-03
  2. '2018-12-03 B+ mods fix no file started error,  more efficient coding of new lines
  3. ' changed topNull to found to continue to use lines to track last loaded item in line$()
  4. ' created function to remove punctuation and lower case all letters, like Eliza
  5. ' setup global variables: line$(), lines, the file as CONST
  6. ' created sub to show the data file to check wording call either as in$ or res$, for debugging
  7. ' create function to read commands from user input in$ or user input res$, for Interpreter stuff like Bill's bot
  8. ' so started a commands list and parameters list.
  9.  
  10. 'global variables start with cap, constants are all cap
  11. CONST DATFILE = "AI Chat Data.txt"
  12. DIM SHARED Line$(1 TO 1000000) 'need base 1
  13.  
  14. 'load line$
  15. OPEN DATFILE FOR APPEND AS #1 '<<<<<<<<<<<<<<<<<< in case a file is not started yet
  16. OPEN DATFILE FOR INPUT AS #1
  17.     Lines = Lines + 1 ' <<<<<<<<<<<<<<<<<<<<< let lines track last entry in array
  18.     INPUT #1, Line$(Lines) ' <<<<<<<<<<<<<<<< here you have lines tracking top line number
  19. PRINT "LOADED " + STR$(Lines) + " ASSETS"
  20.  
  21. 'load commands
  22. DIM SHARED Commands(1 TO 6) AS STRING 'Show is a command, just adjust this as add commands
  23. Commands(1) = "show "
  24.  
  25. 'no paramter
  26. Commands(2) = "bye"
  27. Commands(3) = "good bye"
  28. Commands(4) = "goodbye"
  29. Commands(5) = "shutup"
  30. Commands(6) = "shut up"
  31.  
  32. 'load parameters
  33. DIM SHARED Parameters(1 TO 5) AS STRING 'Data or Time are Parameters eg Show Time, Show Data...
  34. Parameters(1) = " data"
  35. Parameters(2) = " time"
  36. Parameters(3) = " date"
  37. Parameters(4) = " today"
  38. Parameters(5) = " commands"
  39.  
  40.     found = 0 'topNull ????
  41.     LINE INPUT "Input >> ", in$ '<<<<<<<<<<<<<<<<<<<<<<<<<<< line input allows commas
  42.     modIN$ = RemovePunctuation$(in$)
  43.     testInterpret = Interpret(modIN$)
  44.     IF testInterpret = 0 THEN 'no commands found
  45.         'in$ = LCASE$(in$)    '<<<< removePunctuation$ will do this and more
  46.  
  47.         FOR n = 1 TO Lines STEP 2 'need base 1 array
  48.             IF modIN$ = RemovePunctuation$(Line$(n)) THEN
  49.                 found = n
  50.                 EXIT FOR
  51.             END IF
  52.         NEXT
  53.  
  54.         IF found THEN
  55.             PRINT Line$(found + 1)
  56.         ELSE
  57.             'try searching the in$ string for keywords like show or tell
  58.  
  59.  
  60.             'no key words or phrases found  THEN do this
  61.             PRINT "How should I respond to that? (Just enter will skip recording a response.)"
  62.             LINE INPUT "respond >> ", res$ '<<<<<<<<<<<<<<<<<<<<<<<<<<< line input allows commas
  63.             IF res$ <> "" THEN 'sometimes you just don't know a good response
  64.                 modRes$ = RemovePunctuation$(res$)
  65.                 test = Interpret(modRes$)
  66.                 IF test = 0 THEN
  67.                     OPEN DATFILE FOR APPEND AS #1
  68.                     WRITE #1, in$
  69.                     WRITE #1, res$
  70.                     CLOSE #1
  71.                     ' instead of this VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
  72.                     'FOR n = 1 TO lines
  73.                     '    line$(n) = ""
  74.                     'NEXT n
  75.  
  76.                     'OPEN "AIDatabase.txt" FOR INPUT AS #1
  77.                     'WHILE NOT EOF(1)
  78.                     '    lines = lines + 1
  79.                     '    INPUT #1, line$(lines)
  80.                     'WEND
  81.                     'CLOSE #1
  82.                     '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  83.                     'this VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
  84.                     Line$(Lines + 1) = in$
  85.                     Line$(Lines + 2) = res$
  86.                     Lines = Lines + 2
  87.                     '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  88.                 ELSE 'res$ = ""
  89.                     'could offer a menu of options when res$ is blank,   like r for random slection
  90.  
  91.                 END IF 'res$ <> ""
  92.             END IF 'test = 0
  93.         END IF 'found a response in line$()
  94.     END IF 'testInterpret = 0
  95.  
  96. FUNCTION RemovePunctuation$ (s$) 'and return lower case for copmaring Data string with Input string
  97.     FOR i = 1 TO LEN(s$)
  98.         IF INSTR(".,?!;:", MID$(s$, i, 1)) <= 0 THEN
  99.             b$ = b$ + LCASE$(MID$(s$, i, 1))
  100.         END IF
  101.     NEXT
  102.     RemovePunctuation$ = b$
  103.  
  104. SUB ShowData 'this is first thing I want to see when debugging the Chat Bot
  105.     FOR i = 1 TO Lines STEP 2
  106.         PRINT i, Line$(i)
  107.         PRINT i + 1, Line$(i + 1)
  108.         PRINT
  109.         IF i MOD 10 = 0 THEN
  110.             PRINT
  111.             INPUT "Press enter to continue data display... any other to quit display."; cont$
  112.             IF LEN(cont$) THEN EXIT FOR
  113.         END IF
  114.     NEXT
  115.     INPUT "End of Data, Press enter to continue... any other to quit."; cont$
  116.     IF LEN(cont$) THEN END
  117.  
  118. 'assume removePunctuation has been used on string
  119. FUNCTION Interpret% (s$) 'return 1 if found something to do (and did it) else 0 so main code can process it as normal chat
  120.     ' commands
  121.     Interpret = 1
  122.     IF s$ = "bye" OR s$ = "good bye" OR s$ = "goodbye" OR s$ = "shutup" OR s$ = "shut up" THEN
  123.         INPUT "Do you wish to end this session? y for yes ", cont$
  124.         IF LEFT$(LCASE$(cont$), 1) = "y" THEN END ELSE EXIT FUNCTION
  125.     END IF
  126.     FOR i = LBOUND(Commands) TO UBOUND(Commands)
  127.         IF INSTR(s$, Commands(i)) > 0 THEN
  128.             FOR j = LBOUND(Parameters) TO UBOUND(Parameters)
  129.                 IF INSTR(s$, Parameters(j)) > 0 THEN
  130.                     SELECT CASE Commands(i) + Parameters(j)
  131.                         CASE "show  data": ShowData: EXIT FUNCTION
  132.                         CASE "show  time", "show  date", "show  today": PRINT DATE$ + " " + TIME$: EXIT FUNCTION
  133.                         CASE "show  commands"
  134.                             CLS
  135.                             FOR k = LBOUND(commands) TO UBOUND(commands)
  136.                                 PRINT "Command"; k, Commands(k)
  137.                                 lineCount = lineCount + 1
  138.                                 IF lineCount MOD 25 = 24 THEN
  139.                                     INPUT "Press enter to continue, any other to quit ", cont$
  140.                                     IF LEN(cont$) THEN EXIT FUNCTION
  141.                                     CLS: lineCount = 0
  142.                                 END IF
  143.                             NEXT
  144.                             FOR k = LBOUND(parameters) TO UBOUND(parameters)
  145.                                 PRINT "Parameter"; k, Parameters(k)
  146.                                 IF lineCount MOD 25 = 24 THEN
  147.                                     INPUT "Press enter to continue, any other to quit ", cont$
  148.                                     IF LEN(cont$) THEN EXIT FUNCTION
  149.                                     CLS: lineCount = 0
  150.                                 END IF
  151.                             NEXT
  152.                             EXIT FUNCTION
  153.                     END SELECT
  154.                 END IF
  155.             NEXT
  156.         END IF
  157.     NEXT
  158.     Interpret = 0
  159.  
  160.  

So now the bot can do things while you are chatting with it.

"How should I respond to that?", might be handy in conversations. ;-))
« Last Edit: December 03, 2018, 04:03:46 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Simple Chat Bot
« Reply #2 on: December 03, 2018, 04:13:25 pm »
Well Mark, it's no BotaBing, but it ain't botabad. You know I tried making a chat bot of the wife once, but I kept getting trapped in an endless loop. Sometimes I wonder why I bot-her. Anyway, Nice job! You remind me of the days I put to much energy into the BBQ and had to squirt down the grill with a spay bottle. I imagine you doing that to your keyboard!

Hey I can't get the "show" command to do anything. I thought it would show the asset list. I know it is listed as "show " with a space, but I can't get it to work that way, either.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simple Chat Bot
« Reply #3 on: December 03, 2018, 05:00:16 pm »
You do have to record your own responses to get the data file to have anything to show, and then the command is:
show data

show commands - gives the current list of commands with parameters

show time or date or today will print date and time

bye or goodbye or good bye or shutup or shut up don't use any parameters so all of them are for show.

This is just proof of concept stage, can always add more commands later. I wanted to start with ones to help debug the bot code.

 
« Last Edit: December 03, 2018, 05:03:44 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Simple Chat Bot
« Reply #4 on: December 03, 2018, 05:04:58 pm »
Got it now. I was just using "show" bu itself. Ouch! I just typed "Show me yours..." and the damn thing slapped me!

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Re: Simple Chat Bot
« Reply #5 on: December 03, 2018, 07:06:22 pm »
Very nice addiction B+. Removing punctuation really smart, (don't know why I didn't think to do that). But there is always a recurring problem with every chat bot, it never starts the conversation or talks before you do. Bit of a difficult problem, but if anyone is able to type even a simple prototype I'd love to read it.
:)

Zep
+[--->++<]>+.+++[->++++<]>.[--->+<]>+.-[---->+<]>++.+[->+++<]>+.+++++++++++.----------.[--->+<]>----.+[---->+<]>+++.---[->++++<]>.------------.+.++++++++++.+[---->+<]>+++.+[->+++<]>++.[--->+<]>+.[->+++<]>+.++++++++++.+.>++++++++++.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simple Chat Bot
« Reply #6 on: December 03, 2018, 07:10:13 pm »
ELIZA would always start:
"Hi, I'm ELIZA. What's your problem?"

(This is where "shut up" to quit a session comes from, not my standard MO.)

;D