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

55
LINES

< > BotCompany Repo | #1001092 // Rule-based android

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

Libraryless. Click here for Pure Java version (514L/4K/13K).

!747
!multi-line strings

m {
  static S[] rules = {
    [[ on "hello", say "hello you too" ]]
  };
  
  p {
    answerQuestionsOnPortAndConsole(stringfunc {
      fallback(s, answer(s))
    });
  }
  
  static class Matches {
    S[] m;
  }
  
  static S fallback(S s, S answer) {
    if (answer != null) return answer;
    return "?";
  }
  
  static S answer(S s) {
    new Matches matches;
    for (S rule : rules) {
      if (match("on * say *", rule, matches)) {
        S in = matches.m[0], out = matches.m[1];
        if (match(unquote(in), s))
          return unquote(out);
      }
    }
    return null;
  }
  
  static boolean match(S pat, S s) {
    return match(pat, s, null);
  }
  
  static boolean match(S pat, S s, Matches matches) {
    L<S> tokpat = parse(pat), toks = parse(s);
    S[] m = match2(tokpat, toks);
    //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
    if (m == null)
      return false;
    else {
      if (matches != null) matches.m = m;
      return true;
    }
  }
  
  static L<S> parse(S s) {
    return dropPunctuation(javaTokPlusPeriod(s));
  }
}

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: #1001092
Snippet name: Rule-based android
Eternal ID of this version: #1001092/1
Text MD5: 90f3b4894d43d1401c085574ac126c2a
Transpilation MD5: bcffc289c890dc04b31bf65e3e84d447
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-20 21:50:47
Source code size: 1176 bytes / 55 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 738 / 779
Referenced in: [show references]