Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

More and more companies use chatbots for engaging with their customers. Often the underlying technology is not too sophisticated, yet many people are stunned at how human-like those bots can appear. The earliest example of this was an early natural language processing (NLP) computer program called Eliza created 1966 at the MIT Artificial Intelligence Laboratory by Professor Joseph Weizenbaum.

Eliza was supposed to simulate a psychotherapist and was mainly created as a method to show the superficiality of communication between man and machine. Weizenbaum was surprised by the number of individuals who attributed human-like feelings to the computer program, including his own secretary!

If you want to build a simple Eliza-like chatbot yourself with R read on!

Have a look at the following conversation:

Eliza: Hello, I am Eliza
You: Hello Eliza
Eliza: I am sorry to hear you are depressed
You: Perhaps you can help me
Eliza: You do not seem quite certain
You: Well, you are only a computer
Eliza: Why do you mention computers?
You: Sorry
Eliza: What feelings do you have when you apologize
You: Are you a computer?
Eliza: Do computers worry you?
You: Or are you human?
Eliza: Why are you interested in whether I am human or not?
You: It is not the same
Eliza: What other connections do you see?
You: I am glad I can talk to you
Eliza: What makes you happy just now?
You: That somebody listens to me
Eliza: Can you be more specific?
You: Everybody seems so busy nowadays
Eliza: You are being a bit negative.
You: That was not always the case
Eliza: When?
You: Back in the days when I was a kid and with my mother
You: My father was always working
Eliza: What else comes to mind when you think of your father?
You: I remember disputes
Eliza: Does thinking of disputes bring anything else to mind?
You: I was a shy kid
Eliza: Perhaps I already knew you were a shy kid.
You: You are experienced
Eliza: What makes you think I am experienced?

Quite impressive, isn’t it!

To build such a chatbot we need a language model that consists of keyphrases with potential answers. A simple model can be found here. We translate the data structure from Javascript into a list where the names of the list members are our keyphrases and the content of each member comprises the potential answers:

model <- list(
"hello" = c(
),
"computer" = c(
"Do computers worry you?",
"What do you think about machines?",
"Why do you mention computers?",
"What do you think machines have to do with your problem?"
),
"name" = c(
"I am not interested in names"
),
"sorry" = c(
"Apologies are not necessary",
"What feelings do you have when you apologize"
),
"I remember" = c(
"Do you often think of $?", "Does thinking of$ bring anything else to mind?",
"What else do you remember?",
"Why do you recall $right now?", "What in the present situation reminds you of$?",
"What is the connection between me and $?" ), "do you remember" = c( "Did you think I would forget$?",
"Why do you think I should recall $now?", "What about$?",
"You mentioned $" ), "I want" = c( "What would it mean if you got$?",
"Why do you want $?", "Suppose you got$ soon."
),
"I dreamt" = c(
"How do you feel about $in reality?" ), "dream" = c( "What does this dream suggest to you?", "Do you dream often?", "What persons appear in your dreams?", "Don't you believe that dream has to do with your problem?" ), "my mother" = c( "Tell me more about your family" ), "my father" = c( "Your father?", "Does he influence you strongly?", "What else comes to mind when you think of your father?" ), "I am glad" = c( "How have I helped you to be$?",
"What makes you happy just now?",
"Can you explain why you are suddenly $?" ), "I am sad" = c( "I am sorry to hear you are depressed", "I'm sure it's not pleasant to be sad" ), "alike" = c( "In what way?", "What similarities are there?" ), "same" = c( "What other connections do you see?" ), "no" = c( "Why not?", "You are being a bit negative.", "Are you saying 'No' just to be negative?" ), "I was" = c( "Were you really?", "Perhaps I already knew you were$.",
"Why do you tell me you were $now?" ), "was I" = c( "What if you were$?",
"Do you think you were $?", "What would it mean if you were$?"
),
"I am" = c(
"In what way are you $?", "Do you want to be$?"
),
"am I" = c(
"Do you believe you are $?", "Would you want to be$?",
"You wish I would tell you you are $?", "What would it mean if you were$?"
),
"are you" = c(
"Why are you interested in whether I am $or not?", "Would you prefer if I weren't$?",
"Perhaps I am $in your fantasies" ), "you are" = c( "What makes you think I am$?"
),
"because" = c(
"Is that the real reason?",
"What other reasons might there be?",
"Does that reason seem to explain anything else?"
),
"were you" = c(
"Perhaps I was $?", "What do you think?", "What if I had been$?"
),
"I can't" = c(
"Maybe you could $now", "What if you could$?"
),
"I feel" = c(
"Do you often feel $?" ), "I felt" = c( "What other feelings do you have?" ), "why don't you" = c( "Should you$ yourself?",
"Do you believe I don't $?", "Perhaps I will$ in good time"
),
"yes" = c(
"You seem quite positive",
"You are sure?",
"I understand"
),
"somebody" = c(
"Can you be more specific?"
),
"everybody" = c(
"Surely not everyone",
"Can you think of anyone in particular?",
"Who, for example?",
"You are thinking of a special person"
),
"always" = c(
"Can you think of a specific example?",
"When?",
"What incident are you thinking of?",
"Really--always?"
),
"what" = c(
"Does that question interest you?",
"What is it you really want to know?",
"What do you think?",
),
"perhaps" = c(
"You do not seem quite certain"
),
"are" = c(
"Did you think they might not be $?", "Possibly they are$"
)
)

Additionally, we include some default answers in case no keyphrase is found:

default_model <- c(
"Very interesting",
"I am not sure I understand you fully",
"What does that suggest to you?",
"Go on",
"Do you feel strongly about discussing such things?"
)

The main code for the chatbot is quite short. The pattern matching takes place via regular expressions (with the grep function). If no match is found a default answer is chosen randomly. If we got a match one of the potential answers is also chosen randomly. In some cases, those answers contain a $sign as a placeholder. In this case, the last part of the input text is being extracted (with the substr and regexec function) and put into the answer at this point (with the sub function): Eliza <- function(input) { # match keywords from model pos <- which(lapply(paste0("(.*)?", names(model), "(.*)?"), grep, x = input, ignore.case = TRUE) == 1) output <- unlist(model[pos]) if (length(pos) == 0) { # choose default answer randomly if no keyword is found output <- sample(default_model, 1) } else { # choose applicable answer randomly pos <- ifelse(length (pos) > 1, sample(pos, 1), pos) output <- sample(output, 1) names(output) <- NULL # customize answer tmp <- regexec(names(model)[pos], input, ignore.case = TRUE)[[1]] end_phrase <- substr(input, start = attr(tmp, "match.length") + as.numeric(tmp) + 1, stop = nchar(input)) end_phrase <- trimws(end_phrase, which = "right", whitespace = "[?!.]") output <- sub("\\$", end_phrase, output)
}
output
}

The user interaction is realized by a simple while-loop asking for input (via the readline function) until the user types “quit”:

input <- ""
cat("Eliza: Hello, I am Eliza!\n")
while (TRUE) {
if (input == "quit") break
cat("Eliza:", Eliza(input))
}

You can now start talking to your chatbot! You should also try to modify and extend the language model to make the conversations more sophisticated.