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

49
LINES

< > BotCompany Repo | #1000886 // Answer questions 3

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

Libraryless. Click here for Pure Java version (316L/3K/9K).

!747

m {
  static S question = "Is port 80 open?";
  
  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.";
    }
  }
  
  // pattern, class name, method
  static String[][] entries = {
    {"is port * open", "ThePortOracle", "isPort_open"}
  };
  
  static S answerQuestion(S question) {
    L<S> 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[]) m);
      }
    }
    return unknown(question);
  }
  
  static S unknown(S question) {
    return "? Unknown question: " + question;
  }

  p {
    if (args.length != 0)
      question = join(" ", args);
    print("Q: " + question);
    
    S answer = answerQuestion(question);
    print("A: " + answer);
  }
  
  static L<S> parse(S s) {
    return tokensToLowerCase(dropPunctuation(javaTokPlusPeriod(s)));
  }
}

Author comment

Began life as a copy of #1000885

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000886
Snippet name: Answer questions 3
Eternal ID of this version: #1000886/1
Text MD5: 2312f8b135c2df083b06cd0e28d0a78d
Transpilation MD5: a3a62b838f4a3046884589c441829158
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-05 15:26:45
Source code size: 1165 bytes / 49 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 711 / 689
Referenced in: #1000887 - Answer questions 4
#3000190 - Answer for stefanreich(>> t 20 questions)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)