Important alert: (current site time 7/15/2013 2:56:13 PM EDT)
 

VB icon

A.I Chat Robot v3.0

Email
Submitted on: 4/7/2010 9:08:39 AM
By: Gonzales Cenelia  
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.1), Java (JDK 1.2), Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 3990
author picture
(About the author)
 
     Description: this is an improved version of the previous chatterbot program "chatterbot2" this one will try a littlebit more to understand what the user is trying to say and will also try to avoid repeating himself too much.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: A.I Chat Robot v3.0
// Description:Description: this is an improved version of the previous chatterbot program "chatterbot2"
this one will try a littlebit more to understand what the user is trying to say and will also
try to avoid repeating himself too much. 
// By: Gonzales Cenelia
//
// Inputs:sentence
//
// Returns:sentence
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6613&lngWId=2//for details.//**************************************

//
// Program Name: chatterbot3
// Description: this is an improved version of the previous chatterbot program "chatterbot1"
// this one will try a littlebit more to understand what the user is trying to say and will also
// try to avoid repeating himself too much. 
//
// Author: Gonzales Cenelia
//
import java.io.*;
import java.util.*;
public class Chatterbot3 {
	static String KnowledgeBase[][] = {
			{"WHAT IS YOUR NAME", 
			 "MY NAME IS CHATTERBOT2.",
			 "YOU CAN CALL ME CHATTERBOT2.",
			 "WHY DO YOU WANT TO KNOW MY NAME?"
			},
			{"HI", 
			 "HI THERE!",
			 "HOW ARE YOU?",
			 "HI!"
			},
			
			{"HOW ARE YOU",
			 "I'M DOING FINE!",
			 "I'M DOING WELL AND YOU?",
			 "WHY DO YOU WANT TO KNOW HOW AM I DOING?"
			},
			{"WHO ARE YOU",
			 "I'M AN A.I PROGRAM.",
			 "I THINK THAT YOU KNOW WHO I'M.",
			 "WHY ARE YOU ASKING?"
			},
			{"ARE YOU INTELLIGENT",
			 "YES,OFCORSE.",
			 "WHAT DO YOU THINK?",
			 "ACTUALY,I'M VERY INTELLIGENT!"
			},
			{"ARE YOU REAL",
			 "DOES THAT QUESTION REALLY MATERS TO YOU?",
			 "WHAT DO YOU MEAN BY THAT?",
			 "I'M AS REAL AS I CAN BE."
			}
		};
	
	final static String puncs = "?!.;,";
	
	final static int inputNum = 1;
	final static int respNum = 3;
	
	
	static boolean isPunc(char ch) {
		return puncs.indexOf(ch) != -1;
	}
	
	static Vector findMatch(String str) {
		Vector<String> result = new Vector<String>(respNum);
		for(int i = 0; i < KnowledgeBase.length; ++i) {
			if(KnowledgeBase[i][0].equalsIgnoreCase(str)) {
				for(int j = inputNum; j <= respNum; ++j) {
					result.add(KnowledgeBase[i][j]);
				}
				break;
			}
		}
		return result;
	}
	
	// removes punctuation and redundant
	// spaces from the user's input
	static String cleanString(String str) {
		StringBuffer temp = new StringBuffer(str.length());
		char prevChar = 0;
		for(int i = 0; i < str.length(); ++i) {
			if(str.charAt(i) == ' ' && prevChar == ' ' ) {
				continue;
			} else if(!isPunc(str.charAt(i))) {
				temp.append(str.charAt(i));
			}
			prevChar = str.charAt(i);
		}
		return temp.toString();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		String sResponse = "";
		while(true) {
			System.out.print(">");
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
			String sInput = in.readLine();
			sInput = cleanString(sInput);
			Vector<String> responses = new Vector<String>(respNum);
			responses = findMatch(sInput);
			if(sInput.equalsIgnoreCase("BYE")) {
				System.out.println("IT WAS NICE TALKING TO YOU USER, SEE YOU NEXT TIME!");
				break;
			} else if(responses.size() == 0) {
				System.out.println("I'M NOT SURE IF I UNDERSTAND WHAT YOU ARE TALKING ABOUT.");
			} else {
				Random generator = new Random();
				int nSelection = generator.nextInt(respNum);
				String sPrevResponse = sResponse;
				sResponse = responses.elementAt(nSelection);
				if(sResponse == sPrevResponse) {
					responses.removeElementAt(nSelection);
					nSelection = generator.nextInt(respNum);
					sResponse = responses.elementAt(nSelection);
				}
				System.out.println(sResponse);
			}
		}
	}
}


Other 12 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.