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

62
LINES

< > BotCompany Repo | #1001100 // Learning android with persistence

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

Libraryless. Click here for Pure Java version (980L/7K/23K).

!747
!multi-line strings

m {
  static L<S> rules = litlist(
    [[ on "hello", say "hello you too" ]]
  );
  
  p {
    readLocally("rules");
    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 synchronized S answer(S s) {
    if (match("on * say *", s)) {
      rules.add(0, s); // add rule on top to override others
      saveLocally("rules");
      return "ok";
    }
      
    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));
  }
}

Author comment

Began life as a copy of #1001097

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001100
Snippet name: Learning android with persistence
Eternal ID of this version: #1001100/1
Text MD5: ac2bf8fb449c4daf461478065f610bda
Transpilation MD5: a4ffd7a356704a9ca6ae9d2751227ec4
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-20 23:57:57
Source code size: 1385 bytes / 62 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 734 / 771
Referenced in: #1001102 - Learning android that can list its rules
#3000190 - Answer for stefanreich(>> t 20 questions)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)