engine of a dynamic chatbot with database

New to FreeBASIC? Post your questions here.
Post Reply
ron77
Posts: 212
Joined: Feb 21, 2019 19:24

engine of a dynamic chatbot with database

Post by ron77 »

hi, all here is a chatbot that uses a text file as a database but instead of using multiple dynamic arrays, it uses an array of UDT with two arrays inside (one for keywords second for replies). that way the code is more dynamic and fewer lines of code :)

the BAS file:

Code: Select all

Screen 19

Type ArraySet
   ReDim Keywords(any) As String
   ReDim Replys(Any)   As String
End Type

ReDim Shared g_Key_Reply(Any) As ArraySet
Dim Shared As string inpt, rply
'Dim user As String = "ronen"

'DIM SHARED TTSvoice as STRING
'TTSvoice = "Microsoft Zira Desktop" 'tts female or male voice (or Zira or David)
RANDOMIZE TIMER


SUB sAppend(arr() AS string, item AS STRING)
   REDIM PRESERVE arr(LBOUND(arr) TO UBOUND(arr) +1)
   arr(UBOUND(arr)) = item
END SUB


SUB loadArrays(filename AS STRING)
   DIM h AS INTEGER = FREEFILE()
   DIM fline AS STRING

   OPEN filename FOR INPUT AS #h
   Dim As Integer IsKeyWord=0,iKeyReplyNum=-1
   WHILE NOT EOF(h)
      LINE INPUT #h, fline
      Var iPosi = InStr(fline,":")
      'ignore the line if theres no : or if its too short or too long
      If iPosi < 2 Or iPosi > 8 Then Continue While
      Var sText = TRIM(MID(fline, iPosi+1))
      If iPosi = 2 Then 'check for 1 chracter entries
         Select Case fline[0]
            Case Asc("k") 'Keywords
               If IsKeyWord=0 Then
                  'if the previous entry was not a keyword add a new set entry
                  IsKeyWord=1:iKeyReplyNum += 1
                  ReDim Preserve g_Key_Reply(iKeyReplyNum)
               EndIf
               sAppend( g_Key_Reply(iKeyReplyNum).Keywords(), sText )
            Case Asc("r") 'Reply
               If iKeyReplyNum < 0 Then
                  Print "ERROR: Reply without Keyword"
               EndIf
               IsKeyWord = 0 'not a Keyword
               sAppend( g_Key_Reply(iKeyReplyNum).Replys(), sText )
         End select
      Endif
   WEND
   CLOSE #h

END SUB

FUNCTION checkArray(Array() AS STRING, inpt AS STRING) AS BOOLEAN
   var result = 0
   dim as boolean Found = false
   for i as integer =  0 to ubound(Array)
      result = Instr(inpt, Array(i))
      if result <> 0 then
         Found = True
         exit for
      end if
   next i
   RETURN found
END Function

function userQuestion(txt AS STRING) As String
   Dim As String rply
   For N As Integer = 0 To UBound(g_Key_Reply)
      With g_Key_Reply(N)
         If checkArray(.Keywords(), txt) THEN
            Return .Replys(Int(RND*(UBOUND(.Replys))+1))
         End If
      End With
   Next N

   Return "i don't know what to say. sorry."
End Function

loadArrays("database.txt")

Do
   Input "> ", Inpt
   rply = userQuestion(inpt)
   Print rply

Loop Until LCase(inpt) = "quit"
Print "goodbye"

sleep
the file "database.txt"

Code: Select all

k:hello
k:hi

r:hello there how are you doing today?
r:hi. nice to meet you :)

k:i'm sad
k:i am sad
k:i don't feel good

r:i'm sorry to hear that. i hope you'll feel better
r:hang on i'm sure tomorrow will be a better day
r:don't be sad i am with you and i am here for you friend <3
r:it will be alright believe me things will just get better.

k:do you care
k:do you love
k:are you my friend

r:yes i do i am here for you
r:yes i am i believe in you always
r:i am your friend dear one :) your my budd
the rule is k: -> for keywords r: -> replies and each set of keywords are linked to the following replies (the database must start with keywords first)

to get more interesting stuff you are invited to visit my small forum community called "Pure Programming Communnity" dedicated to freeBASIC, C, and assembly programming in general and as well chatbots development and also Qbasic/Powerbasic/Other Basic languages...

ron77

p.s. - I hope this link is not considered to be spam :O

okay I removed the link as this was inappropriate
Last edited by ron77 on Jul 01, 2021 12:37, edited 1 time in total.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: engine of a dynamic chatbot with database

Post by fxm »

ron77 wrote:.....
to get more interesting stuff you are invited to visit my small forum community called "Pure Programming Communnity" dedicated to freeBASIC, C, and assembly programming in general and as well chatbots development and also Qbasic/Powerbasic/Other Basic languages...

[url]https://.....[/url]

ron77

p.s. - I hope this link is not considered to be spam :O
Conversely, I do not see on your site any link to freebasic.net[/forum] !
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: engine of a dynamic chatbot with database

Post by BasicCoder2 »

It would probably help if you printed all the possible inputs to choose from for who is going to remember or even know without a list what are legitimate inputs?

AI has always fascinated me and one of the first programs I wrote amounted to using human language to store and retrieve information. I began by looking at simple sentences.

Input and output was in the form of an English sentence. Essentially a data base with a human language interface.
INPUT: Mary's hair is brown.
QUERY: What is the colour of Mary's hair?
OUPUT: Mary's hair is brown.
This required breaking the sentence into parts and knowing that brown is a colour attribute and hair is property of a human.

Humans do not store fixed input/output sentences the way you have done with your program. Not even the Dr Eliza program is that simple minded. Advanced sentence analysis and synthesis was first done by a program called SHRDLU but was limited to a simple world of different shaped blocks and their spatial relationships.
https://en.wikipedia.org/wiki/SHRDLU

So considering one of your input examples:
"I am sad"
First the program would need to know to whom it was conversing and respond accordingly. Does it simply store the fact you are sad? Sad is a variable state. Human interactions here would involve human emotions such as empathy. It is all very complex involving vast amounts of data about everything that most humans know (stored data) or can learn (new data).

Current chatbots are not companions. They cannot replace a human. The google chatbot is good for obtaining information but is not capable of human emotions or empathy.
Last edited by BasicCoder2 on Jul 01, 2021 11:55, edited 1 time in total.
ron77
Posts: 212
Joined: Feb 21, 2019 19:24

Re: engine of a dynamic chatbot with database

Post by ron77 »

hello @fxm and @BasicCoder2

thanks for replying - I will try to give a link to freebasic community forum in my forum (to this forum)...

as for the abilities of simple chatbots (or chatbox) yes there is still much to go before they become more human-like empathy...

I hope to see you and others come more often to my forum and that my forum members come more often to FB forum as well :)

ron77
Post Reply