A.I Artificial Intelligence Chatbot Tutorial

(A step by step guide on how to implement your own chatbot)

  1. Introduction - Chatbot Description (first example)
  2. Introducing Keywords and Stimulus Response
  3. Preprocessing the user's input and repetition control
  4. A More flexible way for matching the inputs
  5. Using class's for a better implementation
  6. Controling repetition made by the user
  7. Using "states" to represent different events
  8. Keyword Boundaries Concept
  9. Using Signon Messages
  10. "Keyword Ranking" Concept
  11. Keyword Equivalence Concept
  12. Transposition and Template Response
  13. Keyword Location Concept
  14. Handling Context
  15. Using Text To Speech
  16. Using a Flat File to store the Database
  17. A better Repetition Handling Algorithm
  18. Updating the Database with new Keywords
  19. Saving the Conversation Logs




Basicaly,a chatterbot is a computer program that when you provide it with some inputs in Natural Language (English, French ...) responds with something meaningful in that same language. Which means that the strength of a chatterbot could be directly measured by the quality of the output selected by the Bot in response to the user. By the previous description,we could deduce that a very basic chatterbot can be written in a few lines of code in a given specific programming language. Lets make our first chatterbot (notice that all the codes that will be used in this tutorial will be written in Visual Basic. So, it is assumed that the reader is familiar with these language

'
' Program Name: chatterbot1
' Description: this is a very basic example of a chatterbot program
'
' Author: Gonzales Cenelia
'

Option Explicit

Private KnowledgeBase() As Variant
Private bEndSession As Boolean
Private nNumOfLines As Integer
Const EM_LINESCROLL = &HB6

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
                                    ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long


Private Sub Form_Load()
    ReDim KnowledgeBase(5)
    'initialising the 'KnowledgeBase' with data
    KnowledgeBase = Array("I HEARD YOU!", "SO,YOU ARE TALKING TO ME.", "CONTINUE,I'M LISTENING.", _
                          "VERY INTERESTING CONVERSATION.", "TELL ME MORE...")
    Timer1.Enabled = False
    Timer2.Enabled = False
    bEndSession = False
    nNumOfLines = 0
End Sub

Private Sub Respond(strInput As String)
    Dim strResponse As String
    Dim iSelection As Integer
    Randomize Timer
    iSelection = (Rnd * UBound(KnowledgeBase))
    If strInput = "BYE" Then
        strResponse = "IT WAS NICE TALKING TO YOU USER, SEE YOU NEXT TIME!"
        PrintLine (strResponse)
        bEndSession = True
    Else
        strResponse = KnowledgeBase(iSelection)
        PrintLine (strResponse)
    End If
End Sub

Private Sub Command1_Click()
    Dim strInput As String
    strInput = Text1.Text
    PrintLine (">" & strInput)
    Timer1.Enabled = True
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
        Dim strInput As String
        strInput = Text1.Text
        PrintLine (">" & strInput)
        Timer1.Enabled = True
    End If
End Sub

Private Sub PrintLine(str As String)
    Text2.Text = Text2.Text & str & vbNewLine
    nNumOfLines = nNumOfLines + 1
    If nNumOfLines > 10 Then
        Call SendMessage(Text2.hwnd, EM_LINESCROLL, 0, (nNumOfLines - 10) * 4)
    End If
End Sub

Private Sub Timer1_Timer()
    Respond (Text1.Text)
    Text1.Text = ""
    Timer1.Enabled = False
    If bEndSession = True Then
        Timer2.Enabled = True
    End If
End Sub

Private Sub Timer2_Timer()
    Unload Chatterbot1
End Sub

Download Complete Project
Run The Application (Chatterbot1.exe)

As you can see,it doesn't take a lot of code to write a very basic program that can interact with a user but it would probably be very difficult to write a program that would really be capable of truely interpreting what the user is actualy saying and after that would also generate the approriate response to it. These has been a long term goal since the beginning and even before the very first computers were created. In 1951,the british mathematician Alan Turing has came up with the question Can machines think and he has also propose a test which is now known as the Turing Test. In this test,a computer program and also a real person is set to speak to a third person (the judge) and he has to decide which of them is the real person. Nowadays,there is a competition that was named the Loebner Prize and in this competition bots that has successfuly fool most of the judge for at list 5 minutes would win a prize of 100.000$. So far no computer program was able to pass this test successfuly. One of the major reason for this is that computer programs written to compute in such contest have naturaly the tendency of comiting a lot of typo (they are often out of the context of the conversation). Which means that generaly,it isn't that difficult for a judge to decide wheter he is speaking to a "computer program" or a real person. Also,the direct ancestor of all those program that tries to mimic a conversation between real human beings is Eliza,the first version of this program was written in 1966 by Joseph Weizenbaum a professor of MIT.

Today, we have chatbot such Alice who has successfully won the bronze medal three consecutive times in the Loebner contest and was also judge as being the most human chatbot in the contest.

Alice uses A.I.M.L for the representation of her database.

Due to her usage of A.I.M.L as a standard way to represent her knowledgebase, Alice remains one of the most popular chatbot over the internet. Alice was created by Dr. Richard Wallace.

Chatbots in general are considered to belong to the weak a.i field (weak artificial intelligence) as opposed to strong a.i who's goal is to create programs that are as intelligent as humans or more intelligent. But it doesn't mean that chatbots do not have any true potential. Being able to create a program that could communicate the same way humans do would be a great advance for the a.i field. Chatbot is this part of artificial intelligence which is more accessible to hobbyist (it only take some average programming skill to be a chatbot programmer). So, programmers out there who wanted to create true a.i or some kind of artificial intelligence, writing intelligent chatbots is a great place to start!


Now,lets get back to our previous program,what are the problems with it?

Well,there is a lot of them. First of all,we can clearly see that the program isn't really trying to understand what the user is saying but instead he is just selecting a random response from his database each time the user type some sentence on the keyboard. And also,we could notice that the program repeat himself very often. One of the reason for this is because of the size of the database which is very small (5 sentences). The second thing that would explain the repetitions is that we haven't implemented any mechanism that would control this unwanted behaviour.

How do we move from a program that just select responses randomly to whatever input that the user might enter on the keyboard to a program that shows some more understanding of the inputs?

The answer to that question is quiet simple, we simply need to use keywords.


A keyword is just a sentence (not necessarly a complete one) or even a word that the program might recognize from the user's input which then makes it possible for the program to react to it (ex: by printing a sentence on the screen).

For the next program, we will write a knowledge base or database, it will be composed of keywords and some responses associated to each keyword.

So, now we know what to do to improve "our first chatterbot" and make it more intelligent. Lets proceed on writing "our second bot", we will call it chatterbot2.

'
' Program Name: chatterbot2
' Description: this is an improved version of the previous chatterbot program "chatterbot1"
' this one will try a littlebit more to understand what the user is trying to say
'
' Author: Gonzales Cenelia
'

Option Explicit

Private KnowledgeBase As Variant
Private nNumOfLines As Integer

Const EM_LINESCROLL = &HB6

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
                    ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long

Private Sub Form_Load()
    KnowledgeBase = Array( _
                Array("WHAT IS YOUR NAME", "MY NAME IS CHATTERBOT2."), _
                Array("HI", "HI THERE!"), _
                Array("HOW ARE YOU", "I'M DOING FINE!"), _
                Array("WHO ARE YOU", "I'M AN A.I PROGRAM."), _
                Array("ARE YOU INTELLIGENT", "YES, OFCORSE."), _
                Array("ARE YOU REAL", "DOES THAT QUESTION REALLY MATERS TO YOU?"))
                
                Timer1.Enabled = False
                Timer2.Enabled = False
End Sub

Private Sub Respond(strInput As String)
    Dim strResponse As String
    strResponse = FindMatch(strInput)
    If strInput = "BYE" Then
        strResponse = "IT WAS NICE TALKING TO YOU USER, SEE YOU NEXT TIME!"
        Timer2.Enabled = True
    ElseIf Len(strResponse) = 0 Then
        strResponse = "I'M NOT SURE IF I UNDERSTAND WHAT YOU ARE TALKING ABOUT."
    End If
    PrintLine (strResponse)
End Sub

Private Function FindMatch(strInput As String) As String
    Dim i As Integer
    Dim strMatch As String
    For i = 0 To UBound(KnowledgeBase) Step 1
        If KnowledgeBase(i)(0) = strInput Then
            strMatch = KnowledgeBase(i)(1)
            Exit For
        End If
    Next
    FindMatch = strMatch
End Function

Private Sub PrintLine(str As String)
    Text2.Text = Text2.Text & str & vbNewLine
    nNumOfLines = nNumOfLines + 1
    If nNumOfLines > 10 Then
        Call SendMessage(Text2.hwnd, EM_LINESCROLL, 0, (nNumOfLines - 10) * 4)
    End If
End Sub

Private Sub Command1_Click()
    Dim strInput As String
    strInput = Text1.Text
    PrintLine (">" & strInput)
    Timer1.Enabled = True
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
        Dim strInput As String
        strInput = Text1.Text
        PrintLine (">" & strInput)
        Timer1.Enabled = True
    End If
End Sub

Private Sub Timer1_Timer()
    Respond (Text1.Text)
    Text1.Text = ""
    Timer1.Enabled = False
End Sub

Private Sub Timer2_Timer()
    Unload Chatterbot2
End Sub

Download Complete Project
Run The Application (Chatterbot2.exe)

Now,the program can understand some sentences like "what is your name", "are you intelligent" etc And also he can choose an appropriate response from his list of responses for this given sentence and just display it on the screen. Unlike the previous version of the program(chatterbot1) chatterbot2 is capable of choosing a suitable response to the given user input without choosing random responses that doesn't take into account what actualy the user trying to say. 

We've also added a couple of new technics to theses new program: when the program is unable to find a matching keyword the current user input, it simply answers by saying that it doesn't understand wich is quiet human like.

What can we improve on these previous Chatbot to make it even better?

There are quiet a few things that we can improve, the first one is that since the chatterbot tends to be very repetitive, we might create a mechanism to control these repetitions. We could simply store the previous response of that Chatbot within a string "sPrevResponse" and make some checkings when selecting the next bot response to see if it's not equal to the previous response. If it is the case, we then select a new response from the available responses.

The other thing that we could improve would be the way that the chatbot handles the users inputs, currently if you enter an input that is in lower case the Chatbot would not understand anything about it even if there would be a match inside the bot's database for that input. Also if the input contains extra spaces or punctuation characters (!;,.) this also would prevent the Chatbot from understanding the input. That's the reason why we will try to introduce some new mechanism to preprocess the users inputs before it can be searh into the Chatbot database. We could have a function to put the users inputs in upper case since the keywords inside the database are in uppercase and another procedure to just remove all of the punctuations and extra spaces that could be found within users input. That said, we now have enough material to write our next chatterbot: "Chattebot3".
Download Complete Project
Run The Application (Chatterbot3.exe)

What are the wickness with the current version of the program?

Clearly there are still many limitations with this version of the program. The most obvious one would be that the program use "exact sentence matching" to find a response to the user's input. This means that if you would go and ask him "what is your name again", the program will simply not understand what you are trying to say to him and this is because it was unable to find a match for this input. And this definitly would sound a littlebit surprising considering the fact that the program can understand the sentence "what is your name".

How do we overcome this problem?

There are at list two ways to solve this problem,the most obvious one is to use a slightly more flexible way for matching keywords in the database against the user's input. All we have to do to make this possible is to simply aloud keywords to be found within the inputs so that we will no longer have the previous limitation.

The other possibility is much more complex,it use's the concept of Fuzzy String Search. To apply this method,it could be useful at first to break the inputs and the current keyword in separate words,after that we could create two different vectors,the first one could be use to store the words for the input and the other one would store the words for the current keyword. Once we have done this we could use the Levenshtein distance for measuring the distance between the two word vectors. (Notice that in order for these method to be effective we would also need an extra keyword that would represent the subject of the current keyword).

So,there you have it,two different methods for improving the chatterbot.
Actualy we could combine both methods and just selectioning which one to use on each situation.

Finaly,there are still another problem that you may have noticed with the previous chatterbot,you could repeat the same sentence over and over and the program wouldn't have any reaction to this. We neeed also to correct this problem.

So,we are now ready to write our fourth chatterbot,we will simply call it chatterbot4.
Download Complete Project
Run The Application (Chatterbot4.exe)

As you probably may have seen,the code for "chatterbot4" is very similar to the one for "chatterbot3" but also there was some key changes in it. In particular,the function for searching for keywords inside the database is now a littlebit more flexible.


So,what next?
Dont worry,there are still a lot of things to be covered.

what can we improve in chatterbot4 to make it better?

Here are some ideas

That said, we will now start to write the implementation for chatterbot5.
Download Complete Project
Run The Application (Chatterbot5.exe)

Before proceding to the next part of this tutorial, you are encouraged to try compiling and running the code for "chatterbot5" so that you can understand how it works and also to verifies the changes that have been made in it.


Has you may have seen, the implementation of the "current chatterbot", is now encapsulated into a class, also, there has been some new functions added to the new version of the program.

We will now try to discuss the implementation of "chatterbot5"

Private Sub SelectResponse()
this function selects a response from a list of responses, there is a new helper function that was added to the program "shuffle", this new function shuffles a list of strings randomly

Private Sub SavePrevInput()
this function simply saves the current user input into a variable (m_sPrevInput) before geting some new inputs from the user.

Private Sub SavePrevResponse()
the function "save_prev_response()" saves the current response of the chatterbot before the bot have started to search responses for the current input, the current responsesis save in the varaible (m_sPrevResponse).

Private Sub SavePrevEvent()
this function simply saves the current event (m_sEvent) into the variable (m_sPrevEvent). An event can be when the program has dectected a "null input" from the user also, when the user repeats himself or even when the chatterbot makes repetitions has well etc.

Private Sub SetEvent(str As String)
sets the current event (m_sEvent)

Private Sub SaveInput()
makes a backup of the current input (m_sIntput) into the variable m_sInputBackup.

Private Sub SetInput(str As String)
sets the current input (m_sInput)

Private Sub RestoreInput()
restores the value of the current input (m_sInput) that has been saved previously into the variable m_sInputBackup.

Private Sub PrintResponse()
prints the response that has been selected by the "chat robot" on the screen.

Private Sub PreProcessInput()
this function does some preprocessing on the input like removing punctuations, redundant spaces charactes and also it converts the input to uppercase.

BotRepeat() As Boolean
verifies if the chatterbot has started to repeat himself.

UserRepeat() As Boolean
verifies if the user has repeat himself.

BotUnderstand() As Boolean
verifies that the bot understand the current user input (m_sInput).

NullInput() As Boolean
verifies if the current user input (m_sInput) is null.

NullInputRepetition() As Boolean
verifies if the user has repeated some null inputs.

UserWantToQuit() As Boolean
check to see if the user wants to quit the current session with the chatterbot.

SameEvent() As Boolean
verifies if the current event (m_sEvent) is the same as the previous one (m_sPrevEvent).

NoResponse() As Boolean
checks to see if the program has no response for the current input.

SameInput() As Boolean
verifies if the current input (m_sInput) is the same as the previous one (m_sPrevInput).

SimilarInput() As Boolean
checks to see if the current and previous input are similar, two inputs are considered similar if one of them is the substring of the other one (ex: "how are you" and "how are you doing" would be considered similar because "how are you" is a substring of "how are you doing".

Private Sub GetInput()
gets inputs from the user.

Private Sub Respond()
handles all responses of the "chat robot" wheter it is for events or simply the current user input. So, basicaly, these function controls the behaviour of the program.

Private Sub FindMatch()
finds responses for the current input.

Private Sub HandleRepetition()
handles repetions made by the program.

Private Sub HandleUserRepetition()
handles repetitions made by the user.

Private Sub HandleEvent(str As String)
this function handles events in general.


you can clearly see that "chatterbot5" have much more functionalities than "chatterbot4" and also each functionalities is encapsulated into methods (functions) of the class "CBot" but still there are a lot more improvements to be made on it too.

Chattebot5 introduce the concept of "state", in these new version of the Chatterbot, we associate a different "state" to some of the events that can occure during a conversation. Ex: when the user enters a null input, the chatterbot would set itsel into the "NULL INPUT**" state, when the user repeat the same sentence, it would go into the "REPETITION T1**" state, etc.

Also these new chatterbot uses a bigger database than the previous chatbot that we have seen so far: chatterbot1, chatterbot2, chatterbot3 ... But still, this is quiet insignificant due to the fact that most chatterbots in use today (the very popular ones) have a database of at least 10000 lines or more. So, this would definitly be one of the major goal that we might try to achieve into the next versions of the chatterbot.

But however for now, we will concentrate a little problem concerning the current chatterbot.

What exactly would be this problem?

Well, it's all about keyword boundaries, suppose that user enters the sentence: "I THINK NOT" during a conversation with the chatbot, naturaly the program would look into his database for a keyword that would match the sentence, and it might found the keyword: "HI", wich is also a substring of the word "THINK", clearly this is an unwanted behaviour.

How do we avoid it?

Simply by puting a space character before and after the keywords that can be found inside the database or we can simply apply the changes during the matching process inside the "find_match() function"

Is there any other things that we can improve in "Chatterbot5"?

Certainly there is. So far the Chatbot start a "chating session" with the users without saying nothing at the beginning of the conversations. It would be good if the chatterbot could say anything at all to startup the conversations. This can easily be achieve by introducing "signon messages" into the program. We can simply do this by creating a new state inside the Chatbot "knowledge base" and by adding some appropriate message that links to it. That new state could be call "SIGNON**".
Download Complete Project
Run The Application (Chatterbot6.exe)

Introducing the concept of "Keyword Ranking"

As you can see, on each new version of the chatterbot, we are progressively adding new features in order to make the Chabot more realistic. Now, in these section, we a re going to introduce the concept of 'keyword ranking' into the Chatterbot. Keyword ranking is a way for the program to select the best keywords in his database when there are more than one keyword that match the users inputs. Ex: if we have the current user input: WHAT IS YOUR NAME AGAIN, by looking into his database, the Chatbot would have a list of two keywords that match this input: 'WHAT' and 'WHAT IS YOUR NAME'. Wich one is the best? Well, the answer is quiet simple, it is obviously: 'WHAT IS YOUR NAME' simply because it is the longest keyword. These new feature has been implemented in the new version of the program: Chatterbot7.
Download Complete Project
Run The Application (Chatterbot7.exe)

Equivalent Keywords

Within all the previous Chatterbots the record for the database aloud us to use only one keyword for each set of responses but sometimes it could be useful to have more than one keyword associated to each set of responses. Specialy when these keywords have the same meaning. Ex: WHAT IS YOUR NAME and CAN YOU PLEASE TELL ME YOUR NAME have both have the same meaning. So there would be no need to use different records for these keywords instead we can just modify the record structure so that it aloud us to have more than one keyword per records.
View the code for Chatterbot8

Keyword Transposition and Template Response

One of the well known mechanism of chatterbots is the capacity to reformulate the user's input by doing some basic verb conjugation. Exemple, if the user enters: YOU ARE A MACHINE, the chatterbot might respond: SO, YOU THINK THAT I'M A MACHINE.

How did we arrive at this transformation? We may have done it by using two steps:

Notice that it's not a good thing to use transposition too much during a conversation, the mechanism would become too obvious and it could create some repetition.

View the code for Chatterbot9

Keyword Location Concept

Some keywords can be located anywhere in a given input, some others can only be found in only some specific places in the user's input otherwise it wouldn't make any sence. A keyword like: "WHO ARE YOU" can be found anywhere on the user's input without creating any problems with the meaning of it.

Some Exemples of sentences using "WHO ARE YOU" would be:

  1. WHO ARE YOU?
  2. BY THE WAY, WHO ARE YOU?
  3. SO TELL ME, WHO ARE YOU EXACTLY?

But a keyword such as "WHO IS" can only be found at the beginning or in the middle of a given sentence but it can not be found at end of the sentence or alone.

Exemples of sentences using the keyword: "WHO IS"

  1. WHO IS YOUR FAVORITE SINGER?
  2. DO YOU KNOW WHO IS THE GREATEST MATHEMATICIAN OF ALL TIME?
  3. TELL ME, DO YOU KNOW WHO IS? (these one clearly doesn't make sny sence)

How do we make sure that the chatterbot will be able to distinguish such keywords and the specific places were they are aloud to be found on a sentence? We will simply introduce some new notations for keywords:

  1. Keywords that can only be found at the beginning or in the middle of a sentence will be represented by: _KEYWORD (Ex: _WHO IS)
  2. Keywords that can only be found at end or in the middle of a sentence will be denoted by: KEYWORD_ (WHAT ARE YOU_)
  3. Keywords that should only be found alone in a sentence will be represented by: _KEYWORD_ (Ex: _WHAT)
  4. And finaly, keywords that can be found anywhere in a sentence or even alone would be simply represented by: KEYWORD (Ex: I UNDERSTAND)

A keyword can have different meanings depending on it's position in a given sentence.
View the code for Chatterbot10

Handling Context

Context a is way for the Chatterbot to keep in tract what it has said previously and being able to take this into account when selecting his next response.
So far, every response selected by the Chatbot during a conversation is chosen only based on the current user's input. But sometimes, we might need more data
in order to be able to respond properly to a given input, that's when we need to use context.

To illustrate these concept, we are going to look at the following conversation log:

USER: What is your favorite movie?
CHATTERBOT: IT IS TERMINATOR II.

USER: Why do you like these movie? (Now how are we suppose to answer that question if we knew nothing about the previous response of the Chatbot?)

So clearly, some inputs requires the usage of "context" in order to be able to formulate a correct answer. In the previous example, it would simply be: IT IS TERMINATOR II.
Now the Bot knows what it was talking about previously, it can more easealy formulate a good answer to the user's input.

we can now continue the previous conversation log:
(Context: IT IS TERMINATOR II)
CHATTERBOT: BECAUSE IT IS A SCIENCE-FICTION MOVIE AND I LOVE SCIENCE-FICTION.

Context also aloud us to control improper reaction from the Chatbot. Example, if the user enters the sentence: "Why do you like these movie?" during a conversation without the Chatterbot even talking about these subject.
It could simply respond by saying: WHAT ARE YOU TALKING ABOUT?

The context feature has been implemented in Chatterbot11.
View the code for Chatterbot11

Another great feature that would be very interesting to implement into a Chatterbot is the capacity to anticipate the next response of the user, these would make the Chatbot
looks even more smarter during a conversation.

Using a Flat File to store the Database

So far the, database was always built into the program wich means when ever you modified the database, you would alseo have to recompile the program. This is not really convenient because it might happen sometimes that we only want to edit the database and keep the rest of the program as it is. For these reason and many others, it could be a good thing to have a separate file to store the database which then gives us the capability of justediting the database without having to recompile all the files in the program. To store the database we could basicaly use a simple text file with some specific notations to distinguish the different elements of the database (keywords, response, transpositions, context ...). In the currentprogram, we will use the following notations that has been use before some impletation of the eliza chatbot in pascal.
  1. Lines that starts by "K" in the database will represent keywords.
  2. Lines that starts by "R" will represent responses
  3. Lines that starts by "S" will represent sign on messages
  4. Lines that starts by "T" will represent transpositions
  5. Lines that starts by "E" will represent possible corrections can be made after transposing the user's input
  6. Lines that starts by "N" will represent responses for empty input from the user
  7. Lines that starts by "X" will represent responses for when that chatbot did not find any matching keyword that match the current user input.
  8. Lines that starts by "W" will represent responses for when the user repeat itself.
  9. Lines that starts by "C" will represent the context of the chatbot's current response.
  10. Lines that starts by "#" will represent comments

We now have a complete architecture for the database, we just need to implement theses features into the next version of the chatbot (Chatterbot13)
View the code for Chatterbot12

A better Repetition Handling Algorithm

In an effort to prevent the chatbot from repeatiing itself too much, previously we have use a very basic and simple algorithm that consist of comparing the current chatbot's response to the previous one. If the current response selection is equal to the previous one, we simply discard that response and look over for the next response candidate on the list of available responses. This algorithm is very effcient when it comes to control immediate repetitions from the chatbot. However, it's not that good to avoid more long term repetition. During a chating session, the same response can occures many times. With the new algorithm, we control how long it takes for the chatbot to reselect the same response. Actualy we make sure that it has use all available response for the corresponding keyword before it can repeat the same response. This is in turn can improve the quality of the conversation exchanges. Here is a decription on how the algorithm works: During the conversation between the chatbot and the user, we make a list of all the responses previously bselected by the chat robot. When selecting a new response, we make a search of then current selected response inside the list starting from the end. If the current response candidate was found during that search wihin the list, we then make a comparaison of that position the total number of available responses. if the position plus one is inferior to the total of available responses, we consider that it is a repetition, so we have to discard the current response and select another one.
View the code for Chatterbot13

Updating the Database with new Keywords

Sometimes, when it comes to add new keywords to the database, it could be difficult to choose those that are really relevant. However, there is a very simple solution to that problem. When chating with the chat robot, we just make sure that we store the user's input in a file (ex: unknown.txt) each time the chatbot was not able to find any matching keyword for the current input. Later on, when we need to make some keywords updates in the database, we just have to take a look at the file that we've use to save the unkown sentences found earlier during the previous conversations. By continuously adding new keywords using these procedure, we could create a very good database.
View the code for Chatterbot14

Saving the Conversation Logs

Why saving the conversations between the users and the chatbot? Because it could help us find the weakness of the chatbot during a given conversation. Whe might then decide on which modifications to make to the database in order to make the future conversations exhanges more natural. We could basically save the time and also the date to help us determin the progress of the chatbot after new updates were applied to it. Saving the logs helps us determin how human like is the conversation skill of the chatbot.
View the code for Chatterbot15
Return to beginning of the document

3409