HateBot

Here is my chatbot, his name is HateBot. He loves to hate.

/*
 * This is HateBot he loves to hate.
 *
 * Author: Jack Kalish, Febuary 2011
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HateBot {

	public static void main(String[] args) {
		System.out.println("Hi, I'm HateBot. And you should probably go kill yourself.");
	    //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 = "[Yy]ou .+ me";
	    	Pattern pattern1 = Pattern.compile(regex1);
	    	Matcher matcher1 = pattern1.matcher(inputText);

	    	//replace you with I
	    	Pattern you = Pattern.compile("you");
    		matcher1 = you.matcher(inputText);
    		String result1 = matcher1.replaceAll("I");

    		//replace "are" with "am"
    		Pattern are = Pattern.compile("are");
    		matcher1 = are.matcher(result1);
    		String result2 = matcher1.replaceFirst("am");

    		//replace "me" with "you"
    		Pattern me = Pattern.compile("me");
    		matcher1 = me.matcher(result2);
    		String result3 = matcher1.replaceAll("you");	

    		//replace "I'm" with "your"
    		Pattern im = Pattern.compile("(I am)|(I'm)");
    		matcher1 = me.matcher(result3);
    		String result4 = matcher1.replaceAll("your");	

	    	//re-assembly rule #2
	    	String regex2 = "[iI] |[iI]'m";
	    	String replacement2 = "I really don't care if ";
	    	Pattern pattern2 = Pattern.compile(regex2);
	    	Matcher matcher2 = pattern2.matcher(result4);

	    	//re-assembly rule #3
	    	String regex3 = "because";
	    	Pattern pattern3 = Pattern.compile(regex3);
	    	Matcher matcher3 = pattern3.matcher(result4);

	    	//re-assembly rule #4
	    	String regex4 = ".+you";
	    	Pattern pattern4 = Pattern.compile(regex4);
	    	Matcher matcher4 = pattern4.matcher(inputText);

	    	//re-assembly rule #5
	    	String regex5 = "[?]";
	    	Pattern pattern5 = Pattern.compile(regex5);
	    	Matcher matcher5 = pattern5.matcher(result4);

	    	//re-assembly rule #6
	    	String regex6 = "[I][my]";
	    	Pattern pattern6 = Pattern.compile(regex6);
	    	Matcher matcher6 = pattern6.matcher(result4);

	    	if (matcher1.find()){
	    		String found = matcher1.group();
	    		//System.out.println(found);
	    		//replace "you" with "I"

	    		finalResult = "I really don't give a crap if " + found + "!";
	    	}
	    	else if(matcher5.find()){
		    	//String result = matcher2.replaceFirst(replacement2);
	    		//String found = matcher3.group();
		    	finalResult = "That is an idiotic queston.";
	    	}
	    	else if(matcher2.find()){
		    	String result = matcher2.replaceFirst(replacement2);
		    	finalResult = replacement2 + result4;
	    	}
	    	else if(matcher3.find()){
		    	//String result = matcher2.replaceFirst(replacement2);
	    	//	String found = matcher3.group();
		    	finalResult = inputText + " why?";
	    	}
	    	else if(matcher4.find()){
		    	//String result = matcher2.replaceFirst(replacement2);
	    		//String found = matcher3.group();
		    	finalResult = inputText + " too!";
	    	}

	    	else{
	    		finalResult = "Oh boo hoo, crybaby.";
	    	}

	    	System.out.println(finalResult);
	    }
	}

}
This entry was posted in Learning Bit by Bit. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>