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

1  
!747
2  
!multi-line strings
3  
4  
m {
5  
  static S[] rules = {
6  
    [[ on "hello", say "hello you too" ]]
7  
  };
8  
  
9  
  p {
10  
    answerQuestionsOnPortAndConsole(stringfunc {
11  
      fallback(s, answer(s))
12  
    });
13  
  }
14  
  
15  
  static class Matches {
16  
    S[] m;
17  
  }
18  
  
19  
  static S fallback(S s, S answer) {
20  
    if (answer != null) return answer;
21  
    return "?";
22  
  }
23  
  
24  
  static S answer(S s) {
25  
    new Matches matches;
26  
    for (S rule : rules) {
27  
      if (match("on * say *", rule, matches)) {
28  
        S in = matches.m[0], out = matches.m[1];
29  
        if (match(unquote(in), s))
30  
          return unquote(out);
31  
      }
32  
    }
33  
    return null;
34  
  }
35  
  
36  
  static boolean match(S pat, S s) {
37  
    return match(pat, s, null);
38  
  }
39  
  
40  
  static boolean match(S pat, S s, Matches matches) {
41  
    L<S> tokpat = parse(pat), toks = parse(s);
42  
    S[] m = match2(tokpat, toks);
43  
    //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
44  
    if (m == null)
45  
      return false;
46  
    else {
47  
      if (matches != null) matches.m = m;
48  
      return true;
49  
    }
50  
  }
51  
  
52  
  static L<S> parse(S s) {
53  
    return dropPunctuation(javaTokPlusPeriod(s));
54  
  }
55  
}

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