Feb 11
11
Chatterbot – The Interview
Leave a comment »
I’m still playing with this and getting the code to work as an applet embedded in a WordPress page is proving more complex than I thought. But, it is working and compiling and was a total hoot to play with.
For those, like me, wrestling with RegEx, there are several extremely good websites on it and additional tools that will assist in developing RegEx statements, including:
http://erik.eae.net/playground/regexp/regexp.html
http://www.ultrapico.com/Expresso.htm
My code uses five pattern matchers and attempts to recreate the excruciating, non-emotive, dronelike interview process we’ve all gone through.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | /* Dave Boyhan - david.boyhan@gmail.com 2/11/11 * A "job intervew" chatterbot, based (OK, stolen from) * Heather Dewey-Hagborg's Elixa code found here --> http://itp.nyu.edu/varwiki/uploads/Eliza * I've added 5 replacement rules and tried to formulate this as a classic, dronelike job interview */ package class2.regexes; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.regex.Matcher; import java.util.regex.Pattern; public class JobInterview { public static void main(String[] args) { System.out.println("I understand you want to work here."); System.out.println("Tell me about your skills.Tell me what makes you a good employee?"); //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 = "you .+ me"; Pattern pattern1 = Pattern.compile(regex1); Matcher matcher1 = pattern1.matcher(inputText); //re-assembly rule #2 String regex2 = "[iI] am|[iI]'m (skilled)?"; String replacement2 = "Tell me more about your skills"; Pattern pattern2 = Pattern.compile(regex2); Matcher matcher2 = pattern2.matcher(inputText); //re-assembly rule #3 String regex3 = "[iI] am|[iI]'m (good)?"; String replacement3 = "OK, why do you think you're good"; Pattern pattern3 = Pattern.compile(regex3); Matcher matcher3 = pattern3.matcher(inputText); //re-assembly rule #4 String regex4 = "[iI] can[iI]?"; String replacement4 = "How did you learn to"; Pattern pattern4 = Pattern.compile(regex4); Matcher matcher4 = pattern4.matcher(inputText); //re-assembly rule #5 String regex5 = "[iI] am|[iI]'m (able)?"; String replacement5 = "How long have you been able to"; Pattern pattern5 = Pattern.compile(regex5); Matcher matcher5 = pattern5.matcher(inputText); if (matcher1.find()){ String found = matcher1.group(); //System.out.println(found); //replace "you" with "I" Pattern you = Pattern.compile("you"); matcher1 = you.matcher(found); String result1 = matcher1.replaceFirst("I"); //replace "me" with "you" Pattern me = Pattern.compile("me"); matcher1 = me.matcher(result1); String result2 = matcher1.replaceFirst("you"); finalResult = "why do you think " + result2 + "?"; } else if(matcher2.find()){ String result = matcher2.replaceFirst(replacement2); finalResult = result + " ?"; } else if(matcher3.find()){ String result = matcher3.replaceFirst(replacement3); finalResult = result + " here?"; } else if(matcher4.find()){ String result = matcher4.replaceFirst(replacement4); finalResult = result + "?"; } else if(matcher5.find()){ String result = matcher5.replaceFirst(replacement5); finalResult = result + " now?"; } else{ finalResult = "How did you come to this conclusion?"; } System.out.println(finalResult); } } } |