Author Topic: Re: THE BABY CHATBOT  (Read 838 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: THE BABY CHATBOT
« on: November 27, 2019, 09:14:02 am »
Code: QB64: [Select]
  1. _TITLE "THE BABY CHATBOT"
  2. 'original code by petr mod by ron77 2019-11-27
  3.  
  4. Words(0) = "mom"
  5. Words(1) = "dad"
  6. Words(2) = "food"
  7. Words(3) = "hi"
  8.  
  9.  
  10. T$ = "Hi.i'm a baby chatbot teach me what and how to speak"
  11. DO UNTIL in$ = "end"
  12.     PRINT T$
  13.     INPUT "", in$
  14.     T$ = Answer$(in$)
  15.     Add in$
  16.     PRINT: PRINT "let's see baby's word list grow:"
  17.     FOR i = LBOUND(words) TO UBOUND(words)
  18.         PRINT i, Words(i)
  19.     NEXT
  20.     PRINT
  21.  
  22. FUNCTION Answer$ (Player_say$)
  23.     DO UNTIL INSTR(1, Words(search), Player_say$)
  24.         search = search + 1
  25.         IF search > UBOUND(words) THEN Answer$ = "i've learned to say something new! thank you!": EXIT FUNCTION
  26.     LOOP
  27.     'Answer$ = Words(search)
  28.     Answer$ = Words(INT(RND * UBOUND(words) + 1))
  29.  
  30. SUB Add (TextInput AS STRING)
  31.     i = UBOUND(words)
  32.     REDIM _PRESERVE Words(i + 1) AS STRING
  33.     Words(i + 1) = TextInput
  34.  
  35.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: THE BABY CHATBOT
« Reply #1 on: November 27, 2019, 09:21:51 am »
Might want to
1. remove duplicate words
2. save words to file so it's not like 50 first dates
3. sort words to find faster
4. associate words to keywords > actually might be a start to learning! that would be cool!
« Last Edit: November 27, 2019, 10:31:16 am by bplus »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: THE BABY CHATBOT
« Reply #2 on: November 27, 2019, 10:35:27 am »
Unfortunately I am not able to help you build sentences in English. But I remember a long time ago when I was trying a similar program in MS - DOS in Quick Basic. I had a lot of fun with that. I analyzed the sentences into words. I tried by algorithm to find out whether it is a verb or an adjective or a noun, or a conjunction or an adverb. Of course - I didn't do much. But what I was able to filter out from the sentences was then stored in files (something like a database). Each word species into its own set. Then I tried to randomly generate a skeleton of sentences such as a title, then an adjective, an interjection, a noun and a verb. (the sentence structure is absolutely different from English). According to this skeleton, I randomly selected words from databases.
Maybe someone will be interested with this idea :)

Here is small upgrade. Program in begin load database with words (if exists) and save words before end. If database not exist, program self create new. Source code also contains easy function
AnswerRND$ (unused), which demonstrate how insert random words from database.

Code: QB64: [Select]
  1.     _TITLE "THE BABY CHATBOT"
  2. 'original code by petr mod by ron77 2019-11-27
  3. 'add save/load for words
  4.  
  5. Words(0) = "mom"
  6. Words(1) = "dad"
  7. Words(2) = "food"
  8. Words(3) = "hi"
  9.  
  10. 'load words database
  11. IF _FILEEXISTS("Dwords.txt") THEN
  12.     ff = FREEFILE
  13.     OPEN "Dwords.txt" FOR INPUT AS #ff
  14.     i = UBOUND(words)
  15.     WHILE NOT EOF(ff)
  16.         i = i + 1
  17.         REDIM _PRESERVE Words(i) AS STRING
  18.         LINE INPUT #ff, Words(i)
  19.     WEND
  20.     CLOSE ff
  21.  
  22. 'RANDOMIZE TIMER  'not need. RND is not used.
  23.  
  24. T$ = "Hi.i'm a baby chatbot teach me what and how to speak"
  25. DO UNTIL in$ = "end"
  26.     PRINT T$
  27.     INPUT "", in$
  28.     T$ = Answer$(in$)
  29.     Add in$
  30.     '    PRINT: PRINT "let's see baby's word list grow:"
  31.     '    FOR i = LBOUND(words) TO UBOUND(words)
  32.     '    PRINT i, Words(i)
  33.     '   NEXT
  34.     PRINT
  35.  
  36. 'save words database - begin with record 4, because first three records are built - in   in this program:
  37. IF UBOUND(words) = 3 THEN END
  38. OPEN "Dwords2.txt" FOR OUTPUT AS #ff
  39. FOR w = 4 TO UBOUND(words)
  40.     PRINT #ff, Words(w)
  41. IF _FILEEXISTS("dwords.txt") THEN KILL "dwords.txt"
  42. NAME "Dwords2.txt" AS "Dwords.txt"
  43.  
  44.  
  45. FUNCTION AnswerRND$ 'example, how do answers randomly  (with this uncomment also RANDOMIZE TIMER)
  46.     record = RND * UBOUND(words)
  47.     AnswerRND$ = Words(record)
  48.  
  49.  
  50. FUNCTION Answer$ (Player_say$)
  51.     DO UNTIL INSTR(1, Words(search), Player_say$)
  52.         search = search + 1
  53.         IF search > UBOUND(words) THEN Answer$ = "i've learned to say something new! thank you!": EXIT FUNCTION
  54.     LOOP
  55.     'Answer$ = Words(search)
  56.     Answer$ = Words(INT(RND * UBOUND(words) + 1))
  57.  
  58. SUB Add (TextInput AS STRING)
  59.     i = UBOUND(words)
  60.  
  61.     'test if this word is in database or not:
  62.     FOR test = LBOUND(word) TO UBOUND(word)
  63.         IF TextInput = Words(test) THEN EXIT SUB
  64.     NEXT
  65.  
  66.     'if is not, add it.
  67.     REDIM _PRESERVE Words(i + 1) AS STRING
  68.     Words(i + 1) = TextInput
  69.  
« Last Edit: November 27, 2019, 11:09:05 am by Petr »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: THE BABY CHATBOT
« Reply #3 on: November 27, 2019, 11:15:03 am »
Hi. Check out my last post. I edited it and added the code while you entered a new one with a query, which my code already solve.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: THE BABY CHATBOT
« Reply #4 on: November 27, 2019, 11:33:23 am »
Oh.... Rewrite on row 71 (word) to (words) in both cases...

Repaired here:

Code: QB64: [Select]
  1. _TITLE "THE BABY CHATBOT"
  2. 'original code by petr mod by ron77 2019-11-27
  3. 'add save/load for words
  4.  
  5. Words(0) = "mom"
  6. Words(1) = "dad"
  7. Words(2) = "food"
  8. Words(3) = "hi"
  9.  
  10. 'load words database
  11. IF _FILEEXISTS("Dwords.txt") THEN
  12.     ff = FREEFILE
  13.     OPEN "Dwords.txt" FOR INPUT AS #ff
  14.     i = UBOUND(words)
  15.     WHILE NOT EOF(ff)
  16.         i = i + 1
  17.         REDIM _PRESERVE Words(i) AS STRING
  18.         LINE INPUT #ff, Words(i)
  19.     WEND
  20.     CLOSE ff
  21.  
  22. 'RANDOMIZE TIMER  'not need. RND is not used.
  23.  
  24. T$ = "Hi.i'm a baby chatbot teach me what and how to speak"
  25. DO UNTIL in$ = "end"
  26.     PRINT T$
  27.     INPUT "", in$
  28.     T$ = Answer$(in$)
  29.     Add in$
  30.     '    PRINT: PRINT "let's see baby's word list grow:"
  31.     '    FOR i = LBOUND(words) TO UBOUND(words)
  32.     '    PRINT i, Words(i)
  33.     '   NEXT
  34.     PRINT
  35.  
  36. 'save words database - begin with record 4, because first three records are built - in   in this program:
  37. IF UBOUND(words) = 3 THEN END
  38. OPEN "Dwords2.txt" FOR OUTPUT AS #ff
  39. FOR w = 4 TO UBOUND(words)
  40.     PRINT #ff, Words(w)
  41. IF _FILEEXISTS("dwords.txt") THEN KILL "dwords.txt"
  42. NAME "Dwords2.txt" AS "Dwords.txt"
  43.  
  44.  
  45. FUNCTION AnswerRND$ 'example, how do answers randomly  (with this uncomment also RANDOMIZE TIMER)
  46.     record = RND * UBOUND(words)
  47.     AnswerRND$ = Words(record)
  48.  
  49.  
  50. FUNCTION Answer$ (Player_say$)
  51.     DO UNTIL INSTR(1, Words(search), Player_say$)
  52.         search = search + 1
  53.         IF search > UBOUND(words) THEN Answer$ = "i've learned to say something new! thank you!": EXIT FUNCTION
  54.     LOOP
  55.     'Answer$ = Words(search)
  56.     Answer$ = Words(INT(RND * UBOUND(words) + 1))
  57.  
  58. SUB Add (TextInput AS STRING)
  59.     i = UBOUND(words)
  60.  
  61.     'test if this word is in database or not:
  62.     FOR test = LBOUND(words) TO UBOUND(words)
  63.         IF TextInput = Words(test) THEN EXIT SUB
  64.     NEXT
  65.  
  66.     'if is not, add it.
  67.     REDIM _PRESERVE Words(i + 1) AS STRING
  68.     Words(i + 1) = TextInput
  69.  

« Last Edit: November 27, 2019, 11:36:02 am by Petr »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: THE BABY CHATBOT
« Reply #5 on: November 27, 2019, 11:42:38 am »
If you replace T$ = Answer$(in$) on row 30 (+/-) to T$ = AnswerRND$, then using RANDOMIZE TIMER has sense, otherwise not, if RND is not used.
« Last Edit: November 27, 2019, 11:43:46 am by Petr »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: THE BABY CHATBOT
« Reply #6 on: November 27, 2019, 11:56:21 am »
Dwords .txt is created in the same folder as is your program EXE file placed. KILL is the same as DEL under DOS, delete all file types, not just text files.

Again. In program begin is all contens from Dwords.txt loaded to array Words. Then, if is NOT new record add to array Words, is nothing saved and Dwords.txt file is the same as in begin. If is some new word added to array Words, are all records from array Words writed to file Dwords2.txt  Then is old file Dwords.txt  deleted and Dwords2.txt renamed as Dwords.txt
« Last Edit: November 27, 2019, 12:11:19 pm by Petr »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: THE BABY CHATBOT
« Reply #7 on: November 27, 2019, 12:07:37 pm »
Hi Ron,

Well 2 teachers is bad idea but you should find files with your Source code if you have  "Output EXE to Source folder" Bullet-ed under the RUN menu of IDE. That is, if Petr isn't KILLing everything which I doubt.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: THE BABY CHATBOT
« Reply #8 on: November 27, 2019, 12:13:31 pm »
You're right, BPlus. The Dwords.txt file remains on the disk.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: THE BABY CHATBOT
« Reply #9 on: November 27, 2019, 06:15:43 pm »
You know that is not an uncommon problem.

When you get word list going you might have a timer test for new words and update data file over regular intervals. That way you don't loose so much if the power goes out or you exit early.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: THE BABY CHATBOT
« Reply #10 on: November 27, 2019, 07:11:59 pm »
You know that is not an uncommon problem.

When you get word list going you might have a timer test for new words and update data file over regular intervals. That way you don't loose so much if the power goes out or you exit early.

And code for the _EXIT command, using it to save before logging out.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!