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

76
LINES

< > BotCompany Repo | #1008840 // Try parsing ctx rules

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

Libraryless. Click here for Pure Java version (8168L/55K/180K).

1  
!7
2  
3  
concept Rule {
4  
  CtxRule1 source;
5  
  Explain explain;
6  
}
7  
8  
static new TreeMap<S, S> regexpMacros;
9  
static new TreeSet<S> patterns;
10  
11  
p {
12  
  silentGC();
13  
  L<CtxRule1> rules = ctxFindRules(#1200000);
14  
  regexpMacros.putAll(ctxLoadRegexpMacros(#1200000));
15  
  //pnl(regexpMacros);
16  
  print("Found " + n(rules, "rules") + ", " + n(regexpMacros, "macros"));
17  
  for (CtxRule1 rule : rules) {
18  
    Explain e = explain(rule.text(), ctxParsingRules(), "rule");
19  
    if (e == null)
20  
      print("Bad rule: " + rule.text());
21  
    else {
22  
      cnew(Rule, source := rule, explain := e);
23  
    }
24  
  }
25  
  evalRulesOnString("wie geht es dir");
26  
  save("patterns");
27  
  botSleep();
28  
}
29  
30  
sS answer(S s) {
31  
  ret evalRulesOnString(s);
32  
}
33  
34  
static S evalRulesOnString(S s) {
35  
  for (Rule rule) try {
36  
    if (evalRule(rule.explain, s)) {
37  
      //print("Fire! " + rule.source.file);
38  
      print("Fire! " + rule.source.text());
39  
    }
40  
  } catch e {
41  
    printExplainTree(rule.explain);
42  
    rethrow(e);
43  
  }
44  
  ret "";
45  
}
46  
47  
static bool evalRule(Explain e, S input) {
48  
  ret eval(e.sub(1), input);
49  
}
50  
51  
static bool eval(Explain exp, S input) {
52  
  if (exp.is("bracket"))
53  
    ret eval(exp.sub(0), input);
54  
  if (exp.is("and"))
55  
    ret eval(exp.sub(0), input) && eval(exp.sub(1), input);
56  
  if (exp.is("or"))
57  
    ret eval(exp.sub(0), input) || eval(exp.sub(1), input);
58  
  if (exp.is("neg"))
59  
    ret !eval(exp.sub(0), input);
60  
  if (exp.is("quoted")) {
61  
    S pattern = unquoteCtx(exp.tok().get(1));
62  
    ret evalPattern(pattern, input);
63  
  }
64  
  if (exp.singleEqualChild())
65  
    ret eval(exp.sub(0), input);
66  
  throw todo(exp.className());
67  
}
68  
69  
sbool evalPattern(S pat, S input) {
70  
  if (empty(pat)) false; // XXX
71  
  pat = ctxExpandMacros(pat, regexpMacros);
72  
  //print("Pattern: " + pat);
73  
  print("Pattern length: " + l(pat));
74  
  patterns.add(pat);
75  
  ret regexpCtx(pat, input).find();
76  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1008840
Snippet name: Try parsing ctx rules
Eternal ID of this version: #1008840/42
Text MD5: f94cc51f7b2d9ef804a27976d6d7938d
Transpilation MD5: 3f3a061f829165e17b37edfe25554535
Author: stefan
Category: javax / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-06-12 19:06:37
Source code size: 1883 bytes / 76 lines
Pitched / IR pitched: No / No
Views / Downloads: 538 / 860
Version history: 41 change(s)
Referenced in: [show references]