This is the source code for a simple chat bot. It was modeled after the way children ask infinite questions by turning every answer into a new question. It works by shortening a given input to a core statement and preceding this statements with a “Why is”, “Why are”, “Why do”, “Why does” accordingly. It is pretty efficient for the fact that it bases on such few rules. On the other hand, it won’t work with too many types of input. More refined rules would need to be added. When the program does not recognize any input it replies with the standard answer “Why is that?”
See the code below…
package com.lingpipe.book.intro;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Eliza {
public static void main(String[] args) {
System.out.println(“Teach me something!”);
//open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputText = “”;
while (!inputText.equals(“exit”)){
try {
inputText = br.readLine();
} catch (IOException ioe) {
System.out.println(“IO error trying to read input”);
System.exit(1);
}
String finalResult = “”;
//re-assembly rule #1 * you * me -> * I * you
String regex1 = “you .+ me”;
Pattern pattern1 = Pattern.compile(regex1);
Matcher matcher1 = pattern1.matcher(inputText);
//re-assembly rule #2
String regex2 = “because|is”;
String replacement2 = “”;
Pattern pattern2 = Pattern.compile(regex2);
Matcher matcher2 = pattern2.matcher(inputText);
//re-assembly rule #3
String regex3 = “because|are”;
String replacement3 = “”;
Pattern pattern3 = Pattern.compile(regex3);
Matcher matcher3 = pattern3.matcher(inputText);
//re-assembly rule #4
String regex4 = “because|has”;
String replacement4 = “have”;
Pattern pattern4 = Pattern.compile(regex4);
Matcher matcher4 = pattern4.matcher(inputText);
//re-assembly rule #5
String regex5 = “because|have”;
String replacement5 = “have”;
Pattern pattern5 = Pattern.compile(regex5);
Matcher matcher5 = pattern5.matcher(inputText);
//re-assembly rule #6 * I * you -> * you * me
String regex6 = “[I] .+ you”;
Pattern pattern6 = Pattern.compile(regex6);
Matcher matcher6 = pattern6.matcher(inputText);
if (matcher1.find()){
String found = matcher1.group();
//System.out.println(found);
//replace “you” with “I”
Pattern you = Pattern.compile(“you”);
matcher1 = you.matcher(found);
String result1 = matcher1.replaceFirst(“I”);
//replace “me” with “you”
Pattern me = Pattern.compile(“me”);
matcher1 = me.matcher(result1);
String result2 = matcher1.replaceFirst(“you”);
finalResult = “why do ” + result2 + “?”;
}
else if (matcher6.find()){
String found = matcher6.group();
//System.out.println(found);
//replace “I” with “you”
Pattern you = Pattern.compile(“I”);
matcher1 = you.matcher(found);
String result1 = matcher1.replaceFirst(“you”);
//replace “you” with “me”
Pattern me = Pattern.compile(“you”);
matcher1 = me.matcher(result1);
String result2 = matcher1.replaceFirst(“me”);
finalResult = “why do ” + result2 + “?”;
}
else if(matcher2.find()){
String result = matcher2.replaceAll(replacement2);
finalResult = “Why is ” + result + “?”;
}
else if(matcher3.find()){
String result = matcher3.replaceAll(replacement3);
finalResult = “Why are ” + result + “?”;
}
else if(matcher4.find()){
String result = matcher4.replaceAll(replacement4);
finalResult = “Why does ” + result + “?”;
}
else if(matcher5.find()){
String result = matcher5.replaceAll(replacement5);
finalResult = “Why do ” + result + “?”;
}
else{
finalResult = “Why is that?”;
}
System.out.println(finalResult);
}
}
}