Creating a Simple Chat Bot

July 24, 2013

Chat bots are web applications which use artificial intelligence to carry out believable conversations with human users. I have always found chat bots interesting, and decided to create my own using Java. The chat bot I created was very simple and did not work terribly well, but gave reasonable responses to common conversations. While it could in some sense learn, the bot I made had no understanding of the English language and was effectively hardwired to give certain responses. In this article I will briefly outline how it worked. It is interesting how simple it can be and still give sensible responses. Below is a conversation I had with the chat bot. The percentages correspond to how certain the bot is that his replies make sense.

A chat bot is not directly programmed to talk in and understand English. It would be almost impossible to do that. Instead, the bot is created in a way where it can learn to converse with humans. Therefore it begins not really being able to do anything until it learns from examples. The set of examples that the bot is given to learn from is called the training set. The training set for a chat bot would probably be a set of conversations that demonstrate sensible responses to questions the chat bot is likely to be asked.

My chat bot stored a set of pieces of dialog in a file on the computer. Specifically it stored a set of statements and responses to the statements. For example, a statement might be "How are you?" and the response might be "Good." I called each such pair of sentences a dialog fragment. A set of dialog fragments can tell the computer what to say in many situations. For example, if someone says "What is your name?" to the bot, it can look up the dialog fragment that contains "What is your name?" and will know that "My name is ChatBot" is an appropriate response. The bot can learn how to respond to different phrases by being told appropriate responses, and can save them as dialog fragments. The bot could even learn new dialog fragments on the fly.

There are a few small optimizations that can make this simple technique work reasonably well. I will outline exactly how the bot I created works. After having saved a large sample of dialog fragments that are reflective of common conversation, the chat bot is ready to talk to an actual user. The user says some statement to the bot. The bot reads the statement and breaks it up into separate words. The bot then searches all of the dialog fragments it has saved with similar initial statements. It determines which fragment contains an initial statement that is most similar to the one the user entered (has most words in common). Finally, the bot prints the response it has associated with this statement out.

It is also worth noting that the percent similarity between the initial statement in the fragment and the user's statement can be used to gauge how confident the bot is that it has a sensible answer. The confidence is less than 50% or so the bot probably does not know what to say, and can say something else like "I don't know what you mean."

Using the technique I described the chat bot can produce reasonable responses for very common statements like "How are you?" I hope you found it interesting to know how you can do it! Chat bots can of course be much more sophisticated than this and demonstrate a much deeper understanding of the conversation. My chat bot did not do any "natural language processing", so was not very well equipped to understand statements. I did find it fun to make though. I have not trained my chat bot to respond to many different things. It also does not format it's output properly, and seems to have a few bugs. It certainly isn't finished. However, if you are interested you can download the source code here and try talking to it. It is just a console application. Maybe in the future I will improve the bot more, give it a GUI, and train it to respond to more things. If and when the program is more capable I will put it under the projects section of my website.

Return to Blog