Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

95
LINES

< > BotCompany Repo | #1000887 // Answer questions 4

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (2069L/17K/49K).

1  
!747
2  
3  
m {
4  
  static S question = "Is port 80 open?";
5  
  static S answer;
6  
  static Throwable error;
7  
  
8  
  // pattern, class name, method
9  
  static String[][] entries = {
10  
    {"is port * open", "ThePortOracle", "isPort_open"},
11  
    {"what time is it", "TheTimeOracle", "whatTimeIsIt"},
12  
    {"is * java compilable", "TheJavaOracle", "is_JavaCompilable"},
13  
    {"is * java-compilable", "TheJavaOracle", "is_JavaCompilable"},
14  
    {"is * a snippet", "TheSnippetOracle", "is_aSnippet"},
15  
  };
16  
  
17  
  static class ThePortOracle {
18  
    static S isPort_open(S s) {
19  
      int port = parseInt(s);
20  
      print("I'll check port " + port + "..."); 
21  
      boolean b = isPortOpen(port);
22  
      return b ? "Yes, it is open!" : "No, it is not open.";
23  
    }
24  
  }
25  
  
26  
  static class TheTimeOracle {
27  
    static S whatTimeIsIt() {
28  
      return "It is " + new java.text.SimpleDateFormat("HH:mm").format(new Date(now())) + ".";
29  
    }
30  
  }
31  
  
32  
  static class TheJavaOracle {
33  
    static S is_JavaCompilable(S s) {
34  
      boolean b = isJavaCompilable(s);
35  
      // Caveat not needed anymore - now function is actually safe
36  
      /*if (b)
37  
        print("This answer is only 99% safe. (See: #1000888)");*/
38  
      return b ? "Yes." : "No.";
39  
    }
40  
  }
41  
  
42  
  static S answerQuestion(S question) {
43  
    L<S> tok = parse(question);
44  
    
45  
    for (S[] e : entries) {
46  
      S[] m = match2(parse(e[0]), tok);
47  
      if (m != null) ctex {
48  
        Class c = Class.forName("main$" + e[1]);
49  
        return "" + call(c, e[2], (Object[]) unquoteAll(m));
50  
      }
51  
    }
52  
    return unknown(question);
53  
  }
54  
  
55  
  static S unknown(S question) {
56  
    return "? Unknown question: " + question;
57  
  }
58  
59  
  p {
60  
    if (args.length != 0)
61  
      question = smartJoin(args);
62  
    print("Q: " + question);
63  
    answer = null;
64  
    error = null;
65  
    
66  
    try {
67  
      answer = answerQuestion(question);
68  
      print("A: " + answer);
69  
    } catch (Throwable e) {
70  
      error = e;
71  
      print(getStackTrace(e));
72  
      print("Internal error!");
73  
    }
74  
  }
75  
  
76  
  static L<S> parse(S s) {
77  
    return tokensToLowerCase(dropPunctuation(javaTokPlusPeriod(s)));
78  
  }
79  
  
80  
  static S smartJoin(S[] args) {
81  
    if (args.length == 1) return args[0];
82  
    
83  
    S[] a = new S[args.length];
84  
    for (int i = 0; i < a.length; i++)
85  
      a[i] = args[i].indexOf(' ') >= 0 ? quote(args[i]) : args[i];
86  
    return join(" ", a);
87  
  }
88  
  
89  
  static S[] unquoteAll(S[] tokens) {
90  
    S[] a = new S[tokens.length];
91  
    for (int i = 0; i < a.length; i++)
92  
      a[i] = unquote(tokens[i]);
93  
    return a;
94  
  }
95  
}

Author comment

Began life as a copy of #1000886

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000887
Snippet name: Answer questions 4
Eternal ID of this version: #1000887/1
Text MD5: 68b4422ec994a686484479ca3da3316b
Transpilation MD5: 99aec342cff27a987ca4a2781485f7c1
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-05 16:23:11
Source code size: 2571 bytes / 95 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 678 / 1432
Referenced in: [show references]