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).

1  
!747
2  
!multi-line strings
3  
4  
m {
5  
  static L<S> rules = litlist(
6  
    [[ on "hello", say "hello you too" ]]
7  
  );
8  
  
9  
  p {
10  
    readLocally("rules");
11  
    answerQuestionsOnPortAndConsole(stringfunc {
12  
      fallback(s, answer(s))
13  
    });
14  
  }
15  
  
16  
  static class Matches {
17  
    S[] m;
18  
  }
19  
  
20  
  static S fallback(S s, S answer) {
21  
    if (answer != null) return answer;
22  
    return "?";
23  
  }
24  
  
25  
  static synchronized S answer(S s) {
26  
    new Matches matches;
27  
    
28  
    if (match("on * say *", s)) {
29  
      rules.add(0, s); // add rule on top to override others
30  
      saveLocally("rules");
31  
      return "ok";
32  
    }
33  
    
34  
    /*if (match("what are your *?", s, matches)) {
35  
      O value = getOpt(main.class, matches.m[0].toLowerCase());
36  
      if (value != null)
37  
        return structure(value);
38  
    }*/
39  
    
40  
    if (match("what are your rules?", s, matches))
41  
      return structure(rules);
42  
      
43  
    for (S rule : rules) {
44  
      if (match("on * say *", rule, matches)) {
45  
        S in = matches.m[0], out = matches.m[1];
46  
        if (match(unquote(in), s))
47  
          return unquote(out);
48  
      }
49  
    }
50  
    return null;
51  
  }
52  
  
53  
  static boolean match(S pat, S s) {
54  
    return match(pat, s, null);
55  
  }
56  
  
57  
  static boolean match(S pat, S s, Matches matches) {
58  
    L<S> tokpat = parse(pat), toks = parse(s);
59  
    S[] m = match2(tokpat, toks);
60  
    //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
61  
    if (m == null)
62  
      return false;
63  
    else {
64  
      if (matches != null) matches.m = m;
65  
      return true;
66  
    }
67  
  }
68  
  
69  
  static L<S> parse(S s) {
70  
    return dropPunctuation(javaTokPlusPeriod(s));
71  
  }
72  
}

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: 625 / 664
Referenced in: [show references]