The Room: Chatbot Edition
(Also known as Real Talk With Johnny)
What is the problem?
The aim of our project is to explore whether Prolog is well-suited to building a chatbot application and, furthermore, a natural language processor.
The theme of our chatbot is the movie The Room by Tommy Wiseau and Greg Sestero. As such, our chatbot will attempt to emulate what it would be like to interact with the most infamous character of The Room: Johnny. This will not be any ordinary chat. It will be an exclusive, one-on-one chat with Johnny where you have unadulterated access to Johnny's transcendent wisdom of life. Think of it like talking to Dr. Phil on Adderall. You will be able to answer carefully-curated questions from Johnny, who will then analyze your personality based on his groundbreaking new psychological method. Prepare yourself for the journey of a lifetime.
There are two main tasks for this project: (1) build a natural language processor in Prolog and (2) build the underlying logic of the bot itself (i.e. the knowledge system).
We will use the natural language processor to identify key phrases and words in the user's provided input. These key phrases and words will then be input to the chatbot, which will decide how to respond to the input. The chatbot will then respond to the user with a phrase of its own, and then wait for more input from the user.
What is the something extra?
There are a couple of features we added as extras: (1) randomization to the chatbot's responses where it makes sense to and (2) the chatbot remembers some of what the user tells it, for example the user's name and responses to some questions; it then uses the information it remembers to assign the user to a "psychological profile" based on the characters in The Room.
What did we learn from doing this?
What is bottom-line/what did we do?
We used Prolog's Definite Clause Grammar to build the natural language processor, then built a mechanism where the chatbot first asks the user a few questions, remembering the replies through the use of dynamic constants then matching a user to different psychological profiles based on their replies, then leads the user to a free chat stage.
The first stage is the data gathering stage which we implemented with a recursive loop that asks the user questions, saves their replies, then queries the database to retrieve a psychological profile, which we defined for different answers, based on the replies. The user can then read the analysis the chatbot gives of their personality, and has the ability to interact with the chatbot outside of the data gathering stage. This second stage uses a backtracking loop, and we had to learn how to use "repeat" to make sure interaction with the chatbot could continue here until the user chooses to end it. In order for our mechanism to work, we had to define the lexicon for our chatbot, and specific sentence structures that it accepts. Because the DCG and our limited lexicon limited the kinds of sentences and words that would be recognized by the chatbot, we also set up an error messaging system that pulls from a few random phrases that Johnny says in The Room, so that the behavior of the chatbot is still consistent with the character (who acts very randomly and often replies nonsensically to other characters).
Is logic programming suitable for building chatbots?
We don't think we can give a straight answer for this question. Logic programming both is and is not suitable to building chatbots, for different reasons.
Let's go through the evidence that attests to logic programming being well suited to building chatbots. Firstly, logic programming (i.e. specifically Prolog) is very well suited for natural language processing. This is particularly evident with Prolog's Definite Clause Grammar syntax, which provides an extremely quick way to build a natural language processor in an easy, intuitive way. Although Prolog's DCG construct is very useful, we found it slightly restrictive and challenging when we used it, although this is most likely due to our own lack of knowledge, rather than any issue with Prolog itself. To improve this, we could define more complex grammar, which Prolog is very well suited for. Another point in favor of Prolog for chatbots is that it is both a programming language and a database language, and as such we can define large databases for the chatbot to pull knowledge from, and easily query that database to get replies based on user input. Our project is a proof of concept of this, as we have more complex querying for assigning a psychological profile to a user, while simpler queries were used to retrieve replies to user input in the second stage; while we defined limited forms of interaction with our chatbot, it is easy to see how, given more time, we could build up that database and our mechanisms to interact much more flexibly and complexly. The efficiency of querying in Prolog means that really complex chatbots could still get replies efficiently even with very large databases to search through.
Let's now go through the evidence that attests to logic programming not being well suited to building chatbots. Firstly, although Prolog makes natural language processing very easy to accomplish, Prolog also makes it very difficult to accomplish tasks that, in other languages, would be very simple to implement. Building looping structures is challenging, because if you use recursion, any failure could lead to backtracking out of the loop or unintentionally backtracking into a different clause. We used the Prolog dynamic database to store input values, but it would be much, much more simple to simply define a variable and set its value, as you would in almost any other programming language. Furthermore, defining all the atoms required for the application is tedious, to say the least.
Overall, many things that are simple in other languages are made to be hard and tedious to implement in Prolog. However, there are some tasks that Prolog excels in and blows away other languages, such as natural language processing and ease/efficiency of querying. Thus, using Prolog should be reserved to very specific domains of problems. For our own problem domain (i.e. chatbots), we would rule that Prolog is, despite the issues we had, still suitable for building chatbots, simply because natural language processing is made so easy. In the right, knowledgeable hands, Prolog could build an extremely elegant and powerful chatbot.
Links to code etc
https://github.com/dcorrech/the_room_chatbot
TEST SESSIONS. Below is the sequence of inputs from the user to get the result that is described, anything after % is a comment, not meant to be input to SWI Prolog. It is expected that the chatbot ignores any "bye" or "quit" during the data gathering stage (Johnny doesn't care about your time, he wants to get through his psychological analysis!):
% (1) No error messages, just an interaction with the chatbot that will result in being assigned "Denny" as your profile. Different answers will yield different profiles being assigned.
swipl
[nlp].
[chatbot].
[content].
start.
Dama % you can put your own name here when the chatbot asks your name.
I love football % chatbot will register your answer as "football" if you say any sentence (within the sentence structure and lexicon that we recognize) that has the word football in it.
no
no
no
% at this point you will be assigned your profile as Denny, and can choose to take the test again. Below are the inputs to NOT take the test again, then exit the program.
no
bye % chatbot will exit given "bye" or "quit"
% end of session
% (2) No error messages, interaction that results in being assigned "Lisa", then interaction with the chatbot that showcases all defined replies (please note some of the replies have randomization to them, so you won't always get the same answer to your question to the chatbot).
swipl
[nlp].
[chatbot].
[content].
start.
Dama
lacrosse % chatbot will also accept "I like lacrosse" or anything like that
yes
no
no
% now you will be assigned Lisa as your profile. Below are the inputs to NOT take the test again, but continue chatting with the chatbot.
no
how are you
tell me a story
do you like football
do you like pizza
do you like alcohol
do you like chess % or anything that isn't pizza, alcohol, or football (but is also defined in our lexicon)
who is lisa
who is mark
who is peter
who is chris % we have defined the "who is" question for all the characters in The Room
who is lacrosse % or any other word in our lexicon that isn't a character in The Room
I am tired of talking % or any sentence that doesn't trigger the given replies, will return random replies from the chatbot
bye johnny % chatbot will exit given "bye" or "quit"
% end of session
% (3) Triggers some error messages during question stage, returns the profile for Mark, starts new test after profile has been presented, then returns the profile for Peter.
swipl
[nlp].
[chatbot].
[content].
start.
Dama
I like football
asfjsanfkasnga % this will trigger an error message, then ask the same question again
no
I like trees % this will trigger an error message, then ask the same question again
yes
no
% now you will be assigned Mark as your profile. Below are inputs to take the test again, and get Peter as your profile, then to quit the chatbot.
yes
I like chess % or anything other than football
no
no
no
% now you will be assigned Peter as your profile.
no
quit please % chatbot will exit given "bye" or "quit"
% end of session