!747 m { static S question = "Is port 80 open?"; static S answer; static Throwable error; // pattern, class name, method static String[][] entries = { {"is port * open", "ThePortOracle", "isPort_open"}, {"what time is it", "TheTimeOracle", "whatTimeIsIt"}, {"is * java compilable", "TheJavaOracle", "is_JavaCompilable"}, {"is * java-compilable", "TheJavaOracle", "is_JavaCompilable"}, {"is * a snippet", "TheSnippetOracle", "is_aSnippet"}, }; static class ThePortOracle { static S isPort_open(S s) { int port = parseInt(s); print("I'll check port " + port + "..."); boolean b = isPortOpen(port); return b ? "Yes, it is open!" : "No, it is not open."; } } static class TheTimeOracle { static S whatTimeIsIt() { return "It is " + new java.text.SimpleDateFormat("HH:mm").format(new Date(now())) + "."; } } static class TheJavaOracle { static S is_JavaCompilable(S s) { boolean b = isJavaCompilable(s); // Caveat not needed anymore - now function is actually safe /*if (b) print("This answer is only 99% safe. (See: #1000888)");*/ return b ? "Yes." : "No."; } } static S answerQuestion(S question) { L tok = parse(question); for (S[] e : entries) { S[] m = match2(parse(e[0]), tok); if (m != null) ctex { Class c = Class.forName("main$" + e[1]); return "" + call(c, e[2], (Object[]) unquoteAll(m)); } } return unknown(question); } static S unknown(S question) { return "? Unknown question: " + question; } p { if (args.length != 0) question = smartJoin(args); print("Q: " + question); answer = null; error = null; try { answer = answerQuestion(question); print("A: " + answer); } catch (Throwable e) { error = e; print(getStackTrace(e)); print("Internal error!"); } } static L parse(S s) { return tokensToLowerCase(dropPunctuation(javaTokPlusPeriod(s))); } static S smartJoin(S[] args) { if (args.length == 1) return args[0]; S[] a = new S[args.length]; for (int i = 0; i < a.length; i++) a[i] = args[i].indexOf(' ') >= 0 ? quote(args[i]) : args[i]; return join(" ", a); } static S[] unquoteAll(S[] tokens) { S[] a = new S[tokens.length]; for (int i = 0; i < a.length; i++) a[i] = unquote(tokens[i]); return a; } }