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

72
LINES

< > BotCompany Repo | #1001102 // Learning android that can list its rules

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

Libraryless. Click here for Pure Java version (990L/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) {
    new Matches matches;
    
    if (match("on * say *", s)) {
      rules.add(0, s); // add rule on top to override others
      saveLocally("rules");
      return "ok";
    }
    
    /*if (match("what are your *?", s, matches)) {
      O value = getOpt(main.class, matches.m[0].toLowerCase());
      if (value != null)
        return structure(value);
    }*/
    
    if (match("what are your rules?", s, matches))
      return structure(rules);
      
    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 #1001100

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: #1001102
Snippet name: Learning android that can list its rules
Eternal ID of this version: #1001102/1
Text MD5: a6846e777184f487514f6820d40b4a97
Transpilation MD5: ae19dc3f10e2897ea6ad60f3923a834d
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-21 00:13:01
Source code size: 1673 bytes / 72 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 626 / 665
Referenced in: [show references]