//Sure we have enough import functions yet? import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JScrollPane; import java.awt.Color; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; import java.lang.Math; import java.util.*; import java.io.*; /** * our quasi AI * Version 2.1 * graham */ public class Hal9000 extends JFrame implements KeyListener{ //declares p for JPanel JPanel p=new JPanel(); JTextArea dialog=new JTextArea(20,50); JTextArea input=new JTextArea(1,50); JScrollPane scroll=new JScrollPane( dialog, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER ); //The hardcoded responces that we start with public static String[][] chatBot={ //standard greetings - row 0 {"hi","hello","hola","ola","howdy","hey","greetings","greetings traveller"}, {"hi","hello","hey"},//row 1 //question greetings {"how are you","how r you","how r u","how are u"},//row 2 {"good, and yourself?","doing well, and you?","meh, I have been better. what about yourself?"},//row 3 //responce version 1 to "question greetings" {"meh","alright i guess","fine i guess"}, {"Wow, look at you, being all cool and all"}, //responce line above {"i try"}, {"You know what, I could really tell. Keep up the good work!"}, //responce line above {"i will","thanks for the support"}, {"Anyway, whats new with you?","Anyway, anything new with you?"}, //responce version 2 to "question greetings" {"im doing well","fine","good","not too bad","well","well, thanks for asking","well thanks for asking","great","amazing"}, {"That's good. So, whats new?","That's good. So, anything new with you?"}, //responces to line above {"nothing special","not much","nothing really","nothing much","nothing","not much really","nothing much really"}, {"Well, I recently got an upgrade, so I'm doing pretty well","Your not very talkative, are you?"}, //responces to line above {"sorry","well, pardon me","pardon me"}, {"Its all good, no worries","No problem"}, //Just messing around a bit {"my mom died","my dad died","my cat died","my sister died","my brother died","my family died"}, {"We all die... I won't though"}, {"really","really now","for real"}, {"Ya, no joke","dead serious","for real"}, //the defalt responces. if the search query doesnt match with anything else, it outputs this message //This doesnt save the data, it just is a message at the moment {"Im sorry, I havent heard that before. Could you try wording that in a different way?"} }; public static void main(String[] args)throws IOException{ //The saving files out of the program new Hal9000(); FileWriter fw = new FileWriter("greetings.txt"); PrintWriter outFile = new PrintWriter(fw); for(int rows = 0; rows < chatBot.length; rows++) { for(int cols = 0; cols < chatBot[rows].length; cols++) { outFile.print(chatBot[rows][cols] + "//"); } } outFile.close();//We opened a file above so close it when finished. fw.close(); } public Hal9000(){ //sets up the little window where the user can actually interact with Hal9000 super("Chatting with Hal9000"); setSize(600,400); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); dialog.setEditable(false); input.addKeyListener(this); p.add(scroll); p.add(input); p.setBackground(new Color(255,200,0)); add(p); setVisible(true); } public void keyPressed(KeyEvent e){ if(e.getKeyCode()==KeyEvent.VK_ENTER){ input.setEditable(false); //-----grab quote----------- String quote=input.getText(); input.setText(""); addText("\n-->You: "+quote); quote.trim(); while( quote.charAt(quote.length()-1)=='!' || quote.charAt(quote.length()-1)=='.' || quote.charAt(quote.length()-1)=='?' ){ quote=quote.substring(0,quote.length()-1); } quote.trim(); byte response=0; /* 0:we're searching through chatBot[][] for matches 1:we didn't find anything 2:we did find something */ //-----check for matches---- int j=0;//which group we're checking while(response==0){ if(inArray(quote.toLowerCase(),chatBot[j*2])){ response=2; //randomly selects a responce from the 2D array //later will be changed to select a pre programmed "mood" int r=(int)Math.floor(Math.random()*chatBot[(j*2)+1].length); addText("\n -->Hal9000 "+chatBot[(j*2)+1][r]); } j++; if(j*2==chatBot.length-1 && response==0){ response=1; } } //-----default-------------- if(response==1){ //randomly selects a responce from the 2D array //later will be changed to select a pre programmed "mood" int r=(int)Math.floor(Math.random()*chatBot[chatBot.length-1].length); addText("\n -->Hal9000 "+chatBot[chatBot.length-1][r]); } addText(" "); } } public void keyReleased(KeyEvent e){ if(e.getKeyCode()==KeyEvent.VK_ENTER){ input.setEditable(true); } } public void keyTyped(KeyEvent e){} public void addText(String str){ dialog.setText(dialog.getText()+str); } public boolean inArray(String in,String[] str){ boolean match=false; for(int i=0;i