There are quite a few things that we can improve, the first one is that since the chatter-bot 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 and make some checking 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 user’s inputs before it can be searched 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:
What Are the Weaknesses 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 uses “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 definitely would sound a little bit surprising considering the fact that the program can understand the sentence “what is your name”.
How Do We Overcome This Problem?
There are at least 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 allow 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 uses 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 used 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 this 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. Actually, we could combine both methods and just selecting which one to use on each situation.
Finally, there is 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 need also to correct this problem.
So, we are now ready to write our fourth chatterbot, we will simply call it
chatterbot4. View the code for Chatterbot4.
As you may have probably seen, the code for “
chatterbot4” is very similar to the one for “
chatterbot3” but also there were some key changes in it. In particular, the function for searching for keywords inside the database is now a little bit more flexible. So, what next? Don’t worry; there are still a lot of things to be covered.
Here Are Some Ideas
- Since the code for the chatterbots have started to grow, it would be a good thing to encapsulate the implementation of the next chatterbot by using a class.
- Also, the database is still much too small to be capable of handling a real conversation with users, so we will need to add some more entries in it.
- It may happen sometimes that the user will press the enter key without entering anything on the keyboard, we need to handle this situation as well.
- The user might also try to trick the chatterbot by repeating his previous sentence with some slight modification, we need to count this as a repetition from the user.
- And finally, pretty soon, you will also notice that we might need a way for ranking keywords when we have multiple choices of keywords for a given input, we need a way for choosing the best one among them.
That said, we will now start to write the implementation for
chatterbot5. Download Chatterbot5.
Before proceeding 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 verify the changes that have been made in it. As you may have seen, the implementation of the “current chatterbot”, is now encapsulated into a class, also, there have been some new functions added to the new version of the program.
We Will Now Try to Discuss the Implementation of “Chatterbot5”
select_response(): 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 after
save_prev_input(): This function simply saves the current user input into a variable (
m_sPrevInput) before getting some new inputs from the user.
void save_prev_response(): The function
save_prev_response()saves the current response of the chatterbot before the bot has started to search responses for the current input, the current response is save in the variable (
void save_prev_event(): This function simply saves the current event (
m_sEvent) into the variable (
m_sPrevEvent). An event can be when the program has detected a
null inputfrom the user also, when the user repeats himself or even when the chatterbot makes repetitions as well, etc.
void set_event(std::string str): Sets the current event (
void save_input(): Makes a backup of the current input (
m_sIntput) into the variable
void set_input(std::string str): Sets the current input (
void restore_input(): Restores the value of the current input (
m_sInput) that has been saved previously into the variable
void print_response(): Prints the response that has been selected by the
chat roboton the screen.
void preprocess_input(): This function does some preprocessing on the input like removing punctuations, redundant spaces characters and also it converts the input to uppercase.
bool bot_repeat(): Verifies if the chatterbot has started to repeat himself.
bool user_repeat(): Verifies if the user has repeated himself/herself.
bool bot_understand(): Verifies that the bot understands the current user input (
bool null_input(): Verifies if the current user input (
bool null_input_repetition(): Verifies if the user has repeated some
bool user_want_to_quit(): Check to see if the user wants to quit the current session with the chatterbot.
bool same_event(): Verifies if the current event (
m_sEvent) is the same as the previous one (
bool no_response(): Checks to see if the program has no response for the current input.
bool same_input(): Verifies if the current input (
m_sInput) is the same as the previous one (
bool similar_input(): 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 (e.g.: how are you and how are you doing would be considered similar because how are you is a substring of how are you doing.
void get_input(): Gets inputs from the user.
void respond(): Handles all responses of the chat robot whether it is for events or simply the current user input. So, basically, this function controls the behaviour of the program.
find_match(): Finds responses for the current input.
void handle_repetition(): Handles repetitions made by the program.
handle_user_repetition(): Handles repetitions made by the user.
void handle_event(std::string str): This function handles events in general.
You can clearly see that “
chatterbot5” has 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 introduces the concept of “
state“, in this new version of the
Chatterbot, we associate a different “
state” to some of the events that can occur during a conversation. Example, when the user enters a
null input, the chatterbot would set itself into the “
NULL INPUT**” state, when the user repeats the same sentence, it would go into the “
REPETITION T1**” state, etc.
Also, these new chatterbots use a bigger database than the previous chatbot that we have seen so far:
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 definitely be one of the major goals that we might try to achieve into the next versions of the
But however for now, we will concentrate a little problem concerning the current
What Exactly Would Be This Problem?
Well, it’s all about keyword boundaries, suppose the user enters the sentence: “
I think not” during a conversation with the chatbot, naturally the program would look into his database for a keyword that would match the sentence, and it might find the keyword: “
Hi“, which is also a substring of the word “
think“, clearly this is an unwanted behaviour.
How Do We Avoid It?
Simply by putting 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 “
Are There Other Things That We Can Improve in “Chatterbot5”?
Certainly there are. So far, the Chatbot start a “chatting session” with the users without saying anything 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 achieved by introducing “sign on 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 called .
Remember to like,share and subscribe to this channelhttps://www.xprexity.net/store-manager/products/#