Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

AI robot

hi,
I am making a website. I need to put a java ai robot on the website. I am very new to this so i have a few questions.

1) does this robot need to be an applet or can i just put a java program on a website?
2) does anyone have a link to an already made java ai robot, so i don't need to start all over and can just edit it as i need to?

thanks for all the help in advance
[404 byte] By [linexpert] at [2007-11-11 7:32:58]
# 1 Re: AI robot
Has to be an applet to run from a webpage. What do you want the bot to do?
Phaelax at 2007-11-11 22:38:00 >
# 2 Re: AI robot
I am making a tech support site, i want it to be able to talk to users and answer their questions with AI. I have made one in past but i made a java program, i have never made a java applet. Can you help me get started?
linexpert at 2007-11-11 22:39:05 >
# 3 Re: AI robot
update:
here is a simple porgram that i put together, i need someone to make this exact program, wont take long, i made it as small as i could. I need someone to turn this into an applet. If you can post the code for the applet i can then get the idea and make my own applet that's very long and well made.

code:

public class New_bot
{
public static void main(String[] args)
{
EasyReader input=new EasyReader();
String question;

{
System.out.println("Hi, I am the P.A.I.B.(Plaine Artificial Intellegence Bot!)");
System.out.println("What can I do for you today?");
question = input.readLine();

question=question.toLowerCase(); //all lower case

if (question.indexOf("hi")!=-1)
System.out.println("Hello!");

else if (question.indexOf("hello")!=-1)
System.out.println("Hi!");


else
System.out.println("I don't know. I am still learning. Try some other question.");

}

}
}
linexpert at 2007-11-11 22:40:04 >
# 4 Re: AI robot
update:
here is a simple porgram that i put together, i need someone to make this exact program, wont take long, i made it as small as i could. I need someone to turn this into an applet. If you can post the code for the applet i can then get the idea and make my own applet that's very long and well made.

Here is a link to a tutorial that will explain exactly how to create an applet:
http://java.sun.com/docs/books/tutorial/applet/

Good luck.
prometheuzz at 2007-11-11 22:41:04 >
# 5 Re: AI robot
thanks for the help. I have one problem,that link stopped working.
linexpert at 2007-11-11 22:42:03 >
# 6 Re: AI robot
thanks for the help. I have one problem,that link stopped working.

Ok, then I'll post the 2nd hit from Google's ( http://www.google.com/search?q=applet+tutorial) search for you:
http://www.realapplets.com/tutorial/
prometheuzz at 2007-11-11 22:43:07 >
# 7 Re: AI robot
Here ya go... the code is heavily commented to help you ( which it hopefully will ). Oh and by the way, I did this in AWT. It's up to you if you want to use AWT or Swing.

/**
* Robot.java
*
* Robot that responds to questions
*
* @version 1.0 18 Dec 2005
* @author destin / linexpert
*/

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Robot extends Applet implements ActionListener {
/** questionField will be the user input */
TextField questionField;

/**
* askQuestion will be a button the users hits
* to submit the question
*/
Button askQuestion;

/** answer label will be the computer's response */
Label answerLabel;

/** question will be the contents of questionField */
String question;

/** initializing */
public void init() {
/** set initial size */
setSize( 400, 350 ); // x:400 y:350

/** instantiate objects */
questionField = new TextField( 10 ); // create a TextField with 10 columns
// (a column is an average character width)
askQuestion = new Button( "Ask" ); // creates a Button with a label "Ask"
answerLabel = new Label( "Hi, I am the P.A.I.B. (Plaine Artificial Intellegence Bot!)\n"
+ "What can I do for you today?" ); // creates a Label with initial text

/** add the objects to the component */
add( answerLabel ); // adds answerLabel to the component
add( askQuestion ); // adds askQuestion to the component
add( questionField ); // adds questionField to the component

/**
* add an ActionListener to the askQuestion button
* this will "listen" to see if it is clicked
*/
askQuestion.addActionListener( this );
}

/** paint the component */
public void paint( Graphics g ) {
/**
* set the location of the objects
* ( you will probably want to change the location to fit your preference )
*/
answerLabel.setLocation(5, 5); // x:5 y:5
askQuestion.setLocation(5, 50); // x:5 y:50
questionField.setLocation(5, 100); // x:5 y:100

/** set question to the lowercase String value of the questionField */
question = questionField.getText().toLowerCase();
}

/**
* this will only execute when askQuestion is clicked
* because of askQuestion.addActionListener( this ); in init()
*
* this method is required since we are implementing the ActionListener
* interface
*/
public void actionPerformed( ActionEvent ae ) {
/**
* check to see if the button they clicked has the same memory location
* as askQuestion
*/
if ((( Button ) ( ae.getSource() )).equals( askQuestion )) {
/** parse for Strings */
if ( question.indexOf( "hi" ) != -1 ) {
answerLabel.setText( "Hello!" );
} else if ( question.indexOf( "hello" ) != -1 ) {
answerLabel.setText( "Hi!" );
} else {
answerLabel.setText( "I don't know. I am still learning.\n" +
"Try some other question." );
}
/**
* this will repaint the component by
* calling the paint( Graphics ) method
*
* we don't actually need to do this in this program but
* I threw it in so that if you end up adding something
* that you will need to repaint the component, you'll know how
*/
repaint();
}
}
}

Feel free to ask anymore questions. :)
destin at 2007-11-11 22:44:11 >
# 8 Re: AI robot
thanks

This is an awsome christmas gift.

thank you very very much:)
linexpert at 2007-11-11 22:45:08 >
# 9 Re: AI robot
Here ya go... the code is heavily commented to help you ( which it hopefully will ). Oh and by the way, I did this in AWT. It's up to you if you want to use AWT or Swing.

/**
* Robot.java
*
* Robot that responds to questions
*
* @version 1.0 18 Dec 2005
* @author destin / linexpert
*/

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Robot extends Applet implements ActionListener {
/** questionField will be the user input */
TextField questionField;

/**
* askQuestion will be a button the users hits
* to submit the question
*/
Button askQuestion;

/** answer label will be the computer's response */
Label answerLabel;

/** question will be the contents of questionField */
String question;

/** initializing */
public void init() {
/** set initial size */
setSize( 400, 350 ); // x:400 y:350

/** instantiate objects */
questionField = new TextField( 10 ); // create a TextField with 10 columns
// (a column is an average character width)
askQuestion = new Button( "Ask" ); // creates a Button with a label "Ask"
answerLabel = new Label( "Hi, I am the P.A.I.B. (Plaine Artificial Intellegence Bot!)\n"
+ "What can I do for you today?" ); // creates a Label with initial text

/** add the objects to the component */
add( answerLabel ); // adds answerLabel to the component
add( askQuestion ); // adds askQuestion to the component
add( questionField ); // adds questionField to the component

/**
* add an ActionListener to the askQuestion button
* this will "listen" to see if it is clicked
*/
askQuestion.addActionListener( this );
}

/** paint the component */
public void paint( Graphics g ) {
/**
* set the location of the objects
* ( you will probably want to change the location to fit your preference )
*/
answerLabel.setLocation(5, 5); // x:5 y:5
askQuestion.setLocation(5, 50); // x:5 y:50
questionField.setLocation(5, 100); // x:5 y:100

/** set question to the lowercase String value of the questionField */
question = questionField.getText().toLowerCase();
}

/**
* this will only execute when askQuestion is clicked
* because of askQuestion.addActionListener( this ); in init()
*
* this method is required since we are implementing the ActionListener
* interface
*/
public void actionPerformed( ActionEvent ae ) {
/**
* check to see if the button they clicked has the same memory location
* as askQuestion
*/
if ((( Button ) ( ae.getSource() )).equals( askQuestion )) {
/** parse for Strings */
if ( question.indexOf( "hi" ) != -1 ) {
answerLabel.setText( "Hello!" );
} else if ( question.indexOf( "hello" ) != -1 ) {
answerLabel.setText( "Hi!" );
} else {
answerLabel.setText( "I don't know. I am still learning.\n" +
"Try some other question." );
}
/**
* this will repaint the component by
* calling the paint( Graphics ) method
*
* we don't actually need to do this in this program but
* I threw it in so that if you end up adding something
* that you will need to repaint the component, you'll know how
*/
repaint();
}
}
}

Feel free to ask anymore questions. :)

I have three questions:
1) how do i change the size of the text input box? In the code it only has the location (x,y) for the bar but no size.
2) There is one error in the code. I see the error but don't know how i can solve it. The error is when we run the program and let's say we type in "hi" and click "ask" it says "I don't know. I am still learning. Try some other question." and when you click it one more time it says what it's supposed to "hi". How can i fix this so i don't have to click twice and shows me the correct response the first time. I don't see why we have to click twice.
3) If i want to add a line of code so that insted of clicking "ask" the user can just press enter, what do i have to do?

thank you very very very much :WAVE:
linexpert at 2007-11-11 22:46:05 >
# 10 Re: AI robot
1. /** instantiate objects */
questionField = new TextField( 10 ); // create a TextField with 10 columns
// (a column is an average character width

Just change the contructor's argument to a greater amount.
questionField = new TextField( columns );
where columns is the approx. amount of characters you want the textfield to hold.

If you wanted to make it longer as well as wider, you should use a TextArea instead of a TextField.

2. ahh... this is because i got the value of the TextField in the paint method, which isn't called until after it does repaint(); in the actionPerformed( ActionEvent ) method (fixed code will be posted with the answer to question 3)

3. You'll need to add a KeyListener. here's the code for this:
/**
* Robot.java
*
* Robot that responds to questions
*
* @version 1.0 Dec 2005
* @author destin / linexpert
*/

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/**
* we need to implement ActionListener for the Ask button and
* KeyListener for when they hit enter
*/
public class Robot extends Applet implements ActionListener, KeyListener {
/** questionField will be the user input */
TextField questionField;

/**
* askQuestion will be a button the users hits
* to submit the question
*/
Button askQuestion;

/** answer label will be the computer's response */
Label answerLabel;

/** question will be the contents of questionField */
String question;

/** initializing */
public void init() {
/** set initial size */
setSize( 400, 350 ); // x:400 y:350

/** instantiate objects */
questionField = new TextField( 20 ); // create a TextField with 20 columns
// (a column is an average character width)
askQuestion = new Button( "Ask" ); // creates a Button with a label "Ask"
answerLabel = new Label( "Hi, I am the P.A.I.B. (Plaine Artificial Intellegence Bot!)\n"
+ "What can I do for you today?" ); // creates a Label with initial text

/** add the objects to the component */
add( answerLabel ); // adds answerLabel to the component
add( askQuestion ); // adds askQuestion to the component
add( questionField ); // adds questionField to the component

/**
* add an ActionListener to the askQuestion button
* this will "listen" to see if it is clicked
*/
askQuestion.addActionListener( this );


/**
* add a KeyListener to the questionField textfield
* this will "listen" for keys being pressed
*/
questionField.addKeyListener( this );
}

/** paint the container */
public void paint( Graphics g ) {
/**
* set the location of the objects
* ( you will probably want to change the location to fit your preference )
*/
answerLabel.setLocation( 5, 5 ); // x:5 y:5
askQuestion.setLocation( 5, 50 ); // x:5 y:50
questionField.setLocation( 5, 100 ); // x:5 y:100

/** set question to the lowercase String value of the questionField */
question = questionField.getText().toLowerCase();
}


/**
* added this method since we will have two instances when
* we will be recieving a question ( the button and when they
* hit enter
*/
public void answerQuestion() {
/** set question to the lowercase String value of the questionField */
question = questionField.getText().toLowerCase();

/** parse for Strings */
if ( question.indexOf( "hi" ) != -1 ) {
answerLabel.setText( "Hello!" );
} else if ( question.indexOf( "hello" ) != -1 ) {
answerLabel.setText( "Hi!" );
} else {
answerLabel.setText( "I don't know. I am still learning.\n" +
"Try some other question." );
}
/**
* this will repaint the component by
* calling the paint(Graphics) method
*
* we don't actually need to do this in this program but
* I threw it in so that if you end up adding something
* that you will need to repaint the component, you'll know how
*/
repaint();
}

/**
* this will only execute when askQuestion is clicked
* because of askQuestion.addActionListener( this ); in init()
*
* this method is required since we are implementing the ActionListener
* interface
*/
public void actionPerformed( ActionEvent ae ) {
/**
* check to see if the button they clicked has the same memory location
* as askQuestion
*/
if ((( Button ) ( ae.getSource() )).equals( askQuestion )) {
answerQuestion();
}
}

/**
* these following methods will only execute when a key is pressed
* because of questionField.addKeyListener( this ); in init()
*
* these methods are required since we are implementing the KeyListener
* interface
*/
public void keyPressed( KeyEvent ke ) {
/**
* the following code is the same as:
* if ( ke.getKeyCode == KeyEvent.VK_ENTER ) {
* answerQuestion();
* }
* i just prefer switch-case statements.. seems more organized
*/
switch ( ke.getKeyCode() ) {
/**
* VK_ENTER is a static final int in the KeyEvent class
* it's value is 10 ( which is the number that is read
* when you press enter )
* So you could really just do case 10: but then no one
* would have any idea what that's doing ( which is the
* point of final variables =P )
*/
case KeyEvent.VK_ENTER:
answerQuestion();
break;
/** default is just like else */
default:
break;
}
}

/** required by the interface, but not needed */
public void keyTyped( KeyEvent ke ) { }

/** required by the interface, but not needed */
public void keyReleased( KeyEvent ke ) { }
}

The new stuff is in bold.
destin at 2007-11-11 22:47:11 >
# 11 Re: AI robot
thank you very much :)
linexpert at 2007-11-11 22:48:15 >
# 12 Re: AI robot
anytime :p
destin at 2007-11-11 22:49:07 >
# 13 Re: AI robot
I need your help with one more thing...
How do i clear the text box everytime they press enter or click 'ask'?

thank you
linexpert at 2007-11-11 22:50:14 >
# 14 Re: AI robot
questionField.setText("");

Stick that at the end of the answerQuestion() method.
destin at 2007-11-11 22:51:14 >
# 15 Re: AI robot
Thanks for your help.
I've added that to my program.

but i now have a new question and here it is:
I am going to have many questions in my program so here is how i want to organize them:

There are catagorized by who, what , when, where,why, and how. Each of these will have a sub catagory. Here is an example:

-Who
-bill gates
-george bush
-someone else
-what
-computer
-printer
-tv
...and so on

Once the user puts in a question it goes and looks for the question words such as who what and so on. Let's say that the question is "What is a computer?"
The program first finds the word "what" so it goes down to the what catagory and then it finds the word "computer" so it prints out what a computer is.
How can i put this in the program..In my java program i had if statements but don't know what to do in an applet.

The second question was currently if you say "hi" it says "hello" but if you say "aaaaaahi" it still says "hello" since "aaaaaahi" has "hi" in it. How can i fix this?

thanks for all your help, once i'm done with the site, i'll put your name in the credits for this program. :D
linexpert at 2007-11-11 22:52:17 >
# 16 Re: AI robot
In the following code:

if ( question.indexOf( "hi" ) != -1 ) {

if you added an extra conditional specifying the length of the string, it should fix the problem, thus the if statement should read:

if ( question.indexOf( "hi" ) != -1 && question.length==2 ) {

I'm not actually sure how to get the length of the string in a textfield, that you will have to find out your self, but the method itself should work, all you need to do is make sure the length of the text entered is equal to the number of letters of the input that you are testing for.
hddd12345678910 at 2007-11-11 22:53:16 >
# 17 Re: AI robot
@There are catagorized by who, what , when, where,why, and how. Each of these will have a sub catagory.

I'm going to think of the best way to do this. I will get back to you later.

@The second question was currently if you say "hi" it says "hello" but if you say "aaaaaahi" it still says "hello" since "aaaaaahi" has "hi" in it. How can i fix this?

Here's a bit of (kind of ugly looking) code that will check if a word contains a certain word (explained how it works in the comment).

/**
* returns whether the str contains word or not
* it does this by checking if it either (has a space before it OR
* if it is the first word in the string) AND (if it has a space after it
* OR it is the last word in the string).
*/
public boolean containsWord(String word, String str) {
if (str.indexOf(word) == -1) {
return false;
}

if (((str.indexOf(word) - 1 == -1) ||
(str.charAt(str.indexOf(word) - 1) == ' ')) &&
((str.indexOf(word) + word.length() == str.length()) ||
(str.charAt(str.indexOf(word) + word.length()) == ' '))) {
return true;
}
return false;
}
destin at 2007-11-11 22:54:13 >
# 18 Re: AI robot
I assume askQuestion is a button instance, so this code:

if ((( Button ) ( ae.getSource() )).equals( askQuestion ))

is easier on the eye (and brain) if you write it like:

if (ae.getSource()==askQuestion)
sjalle at 2007-11-11 22:55:12 >
# 19 Re: AI robot
any updates?
linexpert at 2007-11-11 22:56:15 >
# 20 Re: AI robot
any updates?
Oh, sorry! I forgot!

Okay, I would probably go with HashMaps. You would have to make one for who, what, where, etc.

HashMap<String, String> whoMap = new HashMap<String, String>();

whoMap.put("Gates", "rich microsoft guy");
whoMap.put("Bush", "president of US");

System.out.println(whoMap.get("George Bush"));
That prints "president of US". Basically, what you would do is search for who, what, where, when, etc. So let's say they enetered "who is George Bush".
Here's some code:

HashMap<String, String> whoMap = new HashMap<String, String>();

whoMap.put("Gates", "rich microsoft guy");
whoMap.put("Bush", "president of US");

String answer = "";
if (containsWord("who", "who is George Bush")) {
Scanner scanner = new Scanner("who is George Bush");
while (scanner.hasNext()) {
String current = scanner.next();
if (whoMap.containsKey(current)) {
answer = whoMap.get(current);
break;
}
}
}
System.out.println(answer);

Sorry for the lousy response, but I'm in a hurry.
destin at 2007-11-11 22:57:17 >
# 21 Re: AI robot
I am new here and stuff but in somewhat of a hurry so here is a code that you might find usefull. No comments though so have them explain it to you if anything gets you confused. Edit at your will.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Robot extends Applet implements ActionListener, KeyListener
{
TextField questionField;
Button askQuestion;
Label answerLabel;
String question;
Answers answer;

public void init()
{
setSize( 400, 350 );

questionField = new TextField(20);
askQuestion = new Button("Ask");
askQuestion.setActionCommand("ASK");
answerLabel = new Label("Hi, I am the P.A.I.B. (Plaine Artificial Intellegence Bot!)\n"
+ "What can I do for you today?");
add(answerLabel);
add(askQuestion);
add(questionField);
askQuestion.addActionListener(this);
questionField.addKeyListener(this);
answer = new Answers();
answer.addWho("Bill Gates","Rich Dude");
}

public void paint( Graphics g )
{
answerLabel.setLocation(5,5);
askQuestion.setLocation(5,50);
questionField.setLocation(5,100);

question = questionField.getText().toLowerCase();
}

public void answerQuestion()
{
question = questionField.getText().toLowerCase();

if(question.indexOf("hi") != -1)
{
answerLabel.setText("Hello!");
}else if(question.indexOf("hello") != -1 )
{
answerLabel.setText("Hi!");
}else
{
answerLabel.setText(answer.reply(question));
}
repaint();
}

public void actionPerformed(ActionEvent ae)
{
String temp = ae.getActionCommand();
if(temp == "ASK")
{
answerQuestion();
}
}

public void keyPressed(KeyEvent ke)
{
switch(ke.getKeyCode())
{
case KeyEvent.VK_ENTER: answerQuestion(); break;
default:break;
}
}

public void keyTyped( KeyEvent ke ) { }

public void keyReleased( KeyEvent ke ) { }
}

class Answers
{
String[][] who,what,when,where,why,how;
int whol,whatl,whenl,wherel,whyl,howl;

public Answers()
{
who = new String[0][2];
what = new String[0][2];
when = new String[0][2];
where = new String[0][2];
why = new String[0][2];
how = new String[0][2];

whol = 0;
whatl = 0;
whenl = 0;
wherel = 0;
whyl = 0;
howl = 0;
}

public void addWho(String a,String b)
{
if(whol == who.length)
{
String[][] temp;
temp = (who.length == 0) ? new String[1][2] : new String[whol * 2][2];
for(int i = 0; i < who.length; i++)
{
System.out.println(i);
temp[i][1] = who[i][0];
temp[i][2] = who[i][1];
}
whol++;
temp[whol-1][0] = a;
temp[whol-1][1] = b;
who = temp;
}
else
{
whol++;
who[whol][0] = a;
who[whol][1] = b;
}
}

public void addWhat(String a,String b)
{
if(whatl == what.length)
{
String[][] temp;
temp = (what.length == 0) ? new String[1][2] : new String[whatl * 2][2];
for(int i = 0; i < what.length; i++)
{
temp[i][1] = what[i][0];
temp[i][2] = what[i][1];
}
whatl++;
temp[whatl][0] = a;
temp[whatl][1] = b;
what = temp;
}
else
{
whatl++;
what[whatl][0] = a;
what[whatl][1] = b;
}
}

public void addWhen(String a,String b)
{
if(whenl == when.length)
{
String[][] temp;
temp = (when.length == 0) ? new String[1][2] : new String[whenl* 2][2];
for(int i = 0; i < when.length; i++)
{
temp[i][1] = when[i][0];
temp[i][2] = when[i][1];
}
whenl++;
temp[whenl][0] = a;
temp[whenl][1] = b;
when = temp;
}
else
{
whenl++;
when[whenl][0] = a;
when[whenl][1] = b;
}
}

public void addWhere(String a,String b)
{
if(wherel == where.length)
{
String[][] temp;
temp = (where.length == 0) ? new String[1][2] : new String[wherel * 2][2];
for(int i = 0; i < where.length; i++)
{
temp[i][1] = where[i][0];
temp[i][2] = where[i][1];
}
wherel++;
temp[wherel][0] = a;
temp[wherel][1] = b;
where = temp;
}
else
{
wherel++;
where[wherel][0] = a;
where[wherel][1] = b;
}
}

public void addwhy(String a,String b)
{
if(whyl == why.length)
{
String[][] temp;
temp = (why.length == 0) ? new String[1][2] : new String[whyl * 2][2];
for(int i = 0; i < why.length; i++)
{
temp[i][1] = why[i][0];
temp[i][2] = why[i][1];
}
whyl++;
temp[whyl][0] = a;
temp[whyl][1] = b;
why = temp;
}
else
{
whyl++;
why[whyl][0] = a;
why[whyl][1] = b;
}
}

public void addHow(String a,String b)
{
if(howl == how.length)
{
String[][] temp;
temp = (how.length == 0) ? new String[1][2] : new String[howl * 2][2];
for(int i = 0; i < how.length; i++)
{
temp[i][1] = how[i][0];
temp[i][2] = how[i][1];
}
howl++;
temp[howl][0] = a;
temp[howl][1] = b;
how = temp;
}
else
{
howl++;
how[howl][0] = a;
how[howl][1] = b;
}
}

public String reply(String a)
{
String ret;
switch(type(a))
{
case 0: ret = "I don't know. I am still learning.\nTry some other question."; break;
case 1: ret = checkWho(a); break;
case 2: ret = checkWhat(a); break;
case 3: ret = checkWhen(a); break;
case 4: ret = checkWhere(a); break;
case 5: ret = checkWhy(a); break;
case 6: ret = checkHow(a); break;
default: ret = "I don't know. I am still learning.\nTry some other question."; break;
}

return ret;
}

public int type(String a)
{
int ret = 0;
if(a.trim().toLowerCase().indexOf("who") != -1) ret = 1;
if(a.trim().toLowerCase().indexOf("what") != -1) ret = 2;
if(a.trim().toLowerCase().indexOf("when") != -1) ret = 3;
if(a.trim().toLowerCase().indexOf("where") != -1) ret = 4;
if(a.trim().toLowerCase().indexOf("why") != -1) ret = 5;
if(a.trim().toLowerCase().indexOf("how") != -1) ret = 6;
return ret;
}

public String checkWho(String a)
{
for(int i = 0; i < whol; i++)
{
if(a.trim().toLowerCase().indexOf(who[i][0].toLowerCase()) != -1) return who[i][1];
}
return "I don't know. I am still learning.\nTry some other question.";
}

public String checkWhat(String a)
{
for(int i = 0; i < whatl; i++)
{
if(a.trim().toLowerCase().indexOf(what[i][0].toLowerCase()) != -1) return what[i][1];
}
return "I don't know. I am still learning.\nTry some other question.";
}

public String checkWhen(String a)
{
for(int i = 0; i < whenl; i++)
{
if(a.trim().toLowerCase().indexOf(when[i][0].toLowerCase()) != -1) return when[i][1];
}
return "I don't know. I am still learning.\nTry some other question.";
}

public String checkWhere(String a)
{
for(int i = 0; i < wherel; i++)
{
if(a.trim().toLowerCase().indexOf(where[i][0].toLowerCase()) != -1) return where[i][1];
}
return "I don't know. I am still learning.\nTry some other question.";
}

public String checkWhy(String a)
{
for(int i = 0; i < whyl; i++)
{
if(a.trim().toLowerCase().indexOf(why[i][0].toLowerCase()) != -1) return why[i][1];
}
return "I don't know. I am still learning.\nTry some other question.";
}

public String checkHow(String a)
{
for(int i = 0; i < howl; i++)
{
if(a.trim().toLowerCase().indexOf(how[i][0].toLowerCase()) != -1) return how[i][1];
}
return "I don't know. I am still learning.\nTry some other question.";
}
}
Kura_Kai at 2007-11-11 22:58:17 >
# 22 Re: AI robot
I am new here and stuff but in somewhat of a hurry so here is a code that you might find usefull. No comments though so have them explain it to you if anything gets you confused. Edit at your will.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Robot extends Applet implements ActionListener, KeyListener
{
TextField questionField;
Button askQuestion;
Label answerLabel;
String question;
Answers answer;

public void init()
{
setSize( 400, 350 );

questionField = new TextField(20);
askQuestion = new Button("Ask");
askQuestion.setActionCommand("ASK");
answerLabel = new Label("Hi, I am the P.A.I.B. (Plaine Artificial Intellegence Bot!)\n"
+ "What can I do for you today?");
add(answerLabel);
add(askQuestion);
add(questionField);
askQuestion.addActionListener(this);
questionField.addKeyListener(this);
answer = new Answers();
answer.addWho("Bill Gates","Rich Dude");
}

public void paint( Graphics g )
{
answerLabel.setLocation(5,5);
askQuestion.setLocation(5,50);
questionField.setLocation(5,100);

question = questionField.getText().toLowerCase();
}

public void answerQuestion()
{
question = questionField.getText().toLowerCase();

if(question.indexOf("hi") != -1)
{
answerLabel.setText("Hello!");
}else if(question.indexOf("hello") != -1 )
{
answerLabel.setText("Hi!");
}else
{
answerLabel.setText(answer.reply(question));
}
repaint();
}

public void actionPerformed(ActionEvent ae)
{
String temp = ae.getActionCommand();
if(temp == "ASK")
{
answerQuestion();
}
}

public void keyPressed(KeyEvent ke)
{
switch(ke.getKeyCode())
{
case KeyEvent.VK_ENTER: answerQuestion(); break;
default:break;
}
}

public void keyTyped( KeyEvent ke ) { }

public void keyReleased( KeyEvent ke ) { }
}

class Answers
{
String[][] who,what,when,where,why,how;
int whol,whatl,whenl,wherel,whyl,howl;

public Answers()
{
who = new String[0][2];
what = new String[0][2];
when = new String[0][2];
where = new String[0][2];
why = new String[0][2];
how = new String[0][2];

whol = 0;
whatl = 0;
whenl = 0;
wherel = 0;
whyl = 0;
howl = 0;
}

public void addWho(String a,String b)
{
if(whol == who.length)
{
String[][] temp;
temp = (who.length == 0) ? new String[1][2] : new String[whol * 2][2];
for(int i = 0; i < who.length; i++)
{
System.out.println(i);
temp[i][1] = who[i][0];
temp[i][2] = who[i][1];
}
whol++;
temp[whol-1][0] = a;
temp[whol-1][1] = b;
who = temp;
}
else
{
whol++;
who[whol][0] = a;
who[whol][1] = b;
}
}

public void addWhat(String a,String b)
{
if(whatl == what.length)
{
String[][] temp;
temp = (what.length == 0) ? new String[1][2] : new String[whatl * 2][2];
for(int i = 0; i < what.length; i++)
{
temp[i][1] = what[i][0];
temp[i][2] = what[i][1];
}
whatl++;
temp[whatl][0] = a;
temp[whatl][1] = b;
what = temp;
}
else
{
whatl++;
what[whatl][0] = a;
what[whatl][1] = b;
}
}

public void addWhen(String a,String b)
{
if(whenl == when.length)
{
String[][] temp;
temp = (when.length == 0) ? new String[1][2] : new String[whenl* 2][2];
for(int i = 0; i < when.length; i++)
{
temp[i][1] = when[i][0];
temp[i][2] = when[i][1];
}
whenl++;
temp[whenl][0] = a;
temp[whenl][1] = b;
when = temp;
}
else
{
whenl++;
when[whenl][0] = a;
when[whenl][1] = b;
}
}

public void addWhere(String a,String b)
{
if(wherel == where.length)
{
String[][] temp;
temp = (where.length == 0) ? new String[1][2] : new String[wherel * 2][2];
for(int i = 0; i < where.length; i++)
{
temp[i][1] = where[i][0];
temp[i][2] = where[i][1];
}
wherel++;
temp[wherel][0] = a;
temp[wherel][1] = b;
where = temp;
}
else
{
wherel++;
where[wherel][0] = a;
where[wherel][1] = b;
}
}

public void addwhy(String a,String b)
{
if(whyl == why.length)
{
String[][] temp;
temp = (why.length == 0) ? new String[1][2] : new String[whyl * 2][2];
for(int i = 0; i < why.length; i++)
{
temp[i][1] = why[i][0];
temp[i][2] = why[i][1];
}
whyl++;
temp[whyl][0] = a;
temp[whyl][1] = b;
why = temp;
}
else
{
whyl++;
why[whyl][0] = a;
why[whyl][1] = b;
}
}

public void addHow(String a,String b)
{
if(howl == how.length)
{
String[][] temp;
temp = (how.length == 0) ? new String[1][2] : new String[howl * 2][2];
for(int i = 0; i < how.length; i++)
{
temp[i][1] = how[i][0];
temp[i][2] = how[i][1];
}
howl++;
temp[howl][0] = a;
temp[howl][1] = b;
how = temp;
}
else
{
howl++;
how[howl][0] = a;
how[howl][1] = b;
}
}

public String reply(String a)
{
String ret;
switch(type(a))
{
case 0: ret = "I don't know. I am still learning.\nTry some other question."; break;
case 1: ret = checkWho(a); break;
case 2: ret = checkWhat(a); break;
case 3: ret = checkWhen(a); break;
case 4: ret = checkWhere(a); break;
case 5: ret = checkWhy(a); break;
case 6: ret = checkHow(a); break;
default: ret = "I don't know. I am still learning.\nTry some other question."; break;
}

return ret;
}

public int type(String a)
{
int ret = 0;
if(a.trim().toLowerCase().indexOf("who") != -1) ret = 1;
if(a.trim().toLowerCase().indexOf("what") != -1) ret = 2;
if(a.trim().toLowerCase().indexOf("when") != -1) ret = 3;
if(a.trim().toLowerCase().indexOf("where") != -1) ret = 4;
if(a.trim().toLowerCase().indexOf("why") != -1) ret = 5;
if(a.trim().toLowerCase().indexOf("how") != -1) ret = 6;
return ret;
}

public String checkWho(String a)
{
for(int i = 0; i < whol; i++)
{
if(a.trim().toLowerCase().indexOf(who[i][0].toLowerCase()) != -1) return who[i][1];
}
return "I don't know. I am still learning.\nTry some other question.";
}

public String checkWhat(String a)
{
for(int i = 0; i < whatl; i++)
{
if(a.trim().toLowerCase().indexOf(what[i][0].toLowerCase()) != -1) return what[i][1];
}
return "I don't know. I am still learning.\nTry some other question.";
}

public String checkWhen(String a)
{
for(int i = 0; i < whenl; i++)
{
if(a.trim().toLowerCase().indexOf(when[i][0].toLowerCase()) != -1) return when[i][1];
}
return "I don't know. I am still learning.\nTry some other question.";
}

public String checkWhere(String a)
{
for(int i = 0; i < wherel; i++)
{
if(a.trim().toLowerCase().indexOf(where[i][0].toLowerCase()) != -1) return where[i][1];
}
return "I don't know. I am still learning.\nTry some other question.";
}

public String checkWhy(String a)
{
for(int i = 0; i < whyl; i++)
{
if(a.trim().toLowerCase().indexOf(why[i][0].toLowerCase()) != -1) return why[i][1];
}
return "I don't know. I am still learning.\nTry some other question.";
}

public String checkHow(String a)
{
for(int i = 0; i < howl; i++)
{
if(a.trim().toLowerCase().indexOf(how[i][0].toLowerCase()) != -1) return how[i][1];
}
return "I don't know. I am still learning.\nTry some other question.";
}
}

Meh. I would go with a HashMap. They're much easier to work with than two-dimensional arrays.
destin at 2007-11-11 22:59:21 >
# 23 Re: AI robot
Ok then you can edit it to use hash maps though i never really used them myself
Kura_Kai at 2007-11-11 23:00:27 >