Advertisement
Guest User

Quasi AI

a guest
Apr 22nd, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. //Sure we have enough import functions yet?
  2. import javax.swing.JFrame;
  3. import javax.swing.JPanel;
  4. import javax.swing.JTextArea;
  5. import javax.swing.JScrollPane;
  6. import java.awt.Color;
  7. import java.awt.event.KeyListener;
  8. import java.awt.event.KeyEvent;
  9. import java.lang.Math;
  10. import java.util.*;
  11. import java.io.*;
  12. /**
  13. * our quasi AI
  14. * Version 2.1
  15. *March 21 (The date doesnt really matter, cuz we work on this like every day...)
  16. */
  17. public class Hal9000 extends JFrame implements KeyListener{
  18. //declares p for JPanel
  19. JPanel p=new JPanel();
  20. JTextArea dialog=new JTextArea(20,50);
  21. JTextArea input=new JTextArea(1,50);
  22. JScrollPane scroll=new JScrollPane(
  23. dialog,
  24. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  25. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
  26. );
  27.  
  28. //The hardcoded responces that we start with
  29. public static String[][] chatBot={
  30. //standard greetings - row 0
  31. {"hi","hello","hola","ola","howdy","hey","greetings","greetings traveller"},
  32. {"hi","hello","hey"},//row 1
  33. //question greetings
  34. {"how are you","how r you","how r u","how are u"},//row 2
  35. {"good, and yourself?","doing well, and you?","meh, I have been better. what about yourself?"},//row 3
  36. //responce version 1 to "question greetings"
  37. {"meh","alright i guess","fine i guess"},
  38. {"Wow, look at you, being all cool and all"},
  39. //responce line above
  40. {"i try"},
  41. {"You know what, I could really tell. Keep up the good work!"},
  42. //responce line above
  43. {"i will","thanks for the support"},
  44. {"Anyway, whats new with you?","Anyway, anything new with you?"},
  45. //responce version 2 to "question greetings"
  46. {"im doing well","fine","good","not too bad","well","well, thanks for asking","well thanks for asking","great","amazing"},
  47. {"That's good. So, whats new?","That's good. So, anything new with you?"},
  48. //responces to line above
  49. {"nothing special","not much","nothing really","nothing much","nothing","not much really","nothing much really"},
  50. {"Well, I recently got an upgrade, so I'm doing pretty well","Your not very talkative, are you?"},
  51. //responces to line above
  52. {"sorry","well, pardon me","pardon me"},
  53. {"Its all good, no worries","No problem"},
  54.  
  55. //Just messing around a bit
  56. {"my mom died","my dad died","my cat died","my sister died","my brother died","my family died"},
  57. {"We all die... I won't though"},
  58.  
  59. {"really","really now","for real"},
  60. {"Ya, no joke","dead serious","for real"},
  61.  
  62. //the defalt responces. if the search query doesnt match with anything else, it outputs this message
  63. //This doesnt save the data, it just is a message at the moment
  64. {"Im sorry, I havent heard that before. Could you try wording that in a different way?"}
  65. };
  66.  
  67.  
  68.  
  69.  
  70. public static void main(String[] args)throws IOException{
  71. //The saving files out of the program
  72. //NOT DONE YET
  73. new Hal9000();
  74.  
  75.  
  76. FileWriter fw = new FileWriter("greetings.txt");
  77. PrintWriter outFile = new PrintWriter(fw);
  78.  
  79. for(int rows = 0; rows < chatBot.length; rows++)
  80. {
  81. for(int cols = 0; cols < chatBot[rows].length; cols++)
  82. {
  83. outFile.print(chatBot[rows][cols] + "//");
  84. }
  85. }
  86.  
  87.  
  88.  
  89.  
  90. outFile.close();//We opened a file above so close it when finished.
  91. fw.close();
  92.  
  93.  
  94.  
  95. }
  96.  
  97. public Hal9000(){
  98. //sets up the little window where the user can actually interact with Hal9000
  99. super("Chatting with Hal9000");
  100. setSize(600,400);
  101. setResizable(false);
  102. setDefaultCloseOperation(EXIT_ON_CLOSE);
  103.  
  104. dialog.setEditable(false);
  105. input.addKeyListener(this);
  106.  
  107. p.add(scroll);
  108. p.add(input);
  109. p.setBackground(new Color(255,200,0));
  110. add(p);
  111.  
  112. setVisible(true);
  113. }
  114.  
  115. public void keyPressed(KeyEvent e){
  116. if(e.getKeyCode()==KeyEvent.VK_ENTER){
  117. input.setEditable(false);
  118. //-----grab quote-----------
  119. String quote=input.getText();
  120. input.setText("");
  121. addText("\n-->You: "+quote);
  122. quote.trim();
  123. while(
  124. quote.charAt(quote.length()-1)=='!' ||
  125. quote.charAt(quote.length()-1)=='.' ||
  126. quote.charAt(quote.length()-1)=='?'
  127. ){
  128. quote=quote.substring(0,quote.length()-1);
  129. }
  130. quote.trim();
  131. byte response=0;
  132. /*
  133. 0:we're searching through chatBot[][] for matches
  134. 1:we didn't find anything
  135. 2:we did find something
  136. */
  137. //-----check for matches----
  138. int j=0;//which group we're checking
  139. while(response==0){
  140. if(inArray(quote.toLowerCase(),chatBot[j*2])){
  141. response=2;
  142. //randomly selects a responce from the 2D array
  143. //later will be changed to select a pre programmed "mood"
  144. int r=(int)Math.floor(Math.random()*chatBot[(j*2)+1].length);
  145. addText("\n -->Hal9000 "+chatBot[(j*2)+1][r]);
  146. }
  147. j++;
  148. if(j*2==chatBot.length-1 && response==0){
  149. response=1;
  150. }
  151. }
  152.  
  153. //-----default--------------
  154. if(response==1){
  155. //randomly selects a responce from the 2D array
  156. //later will be changed to select a pre programmed "mood"
  157. int r=(int)Math.floor(Math.random()*chatBot[chatBot.length-1].length);
  158. addText("\n -->Hal9000 "+chatBot[chatBot.length-1][r]);
  159. }
  160. addText(" ");
  161. }
  162. }
  163.  
  164. public void keyReleased(KeyEvent e){
  165. if(e.getKeyCode()==KeyEvent.VK_ENTER){
  166. input.setEditable(true);
  167. }
  168. }
  169.  
  170. public void keyTyped(KeyEvent e){}
  171.  
  172. public void addText(String str){
  173. dialog.setText(dialog.getText()+str);
  174. }
  175.  
  176. public boolean inArray(String in,String[] str){
  177. boolean match=false;
  178. for(int i=0;i<str.length;i++){
  179. if(str[i].equals(in)){
  180. match=true;
  181. }
  182. }
  183. return match;
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement