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

112
LINES

< > BotCompany Repo | #1008852 // Load ctx rules

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

Libraryless. Click here for Pure Java version (8182L/55K/181K).

1  
!7
2  
3  
abstract sclass CtxExpr {
4  
  abstract bool get(S s);
5  
}
6  
7  
CtxExpr > CtxNeg {
8  
  CtxExpr e;
9  
  
10  
  *() {}
11  
  *(CtxExpr *e) {}
12  
  
13  
  bool get(S s) { ret !e.get(s); }
14  
}
15  
16  
CtxExpr > CtxAnd {  
17  
  CtxExpr a, b;
18  
  
19  
  *() {}
20  
  *(CtxExpr *a, CtxExpr *b) {}
21  
22  
  bool get(S s) { ret a.get(s) && b.get(s); }
23  
}
24  
25  
CtxExpr > CtxOr {  
26  
  CtxExpr a, b;
27  
  
28  
  *() {}
29  
  *(CtxExpr *a, CtxExpr *b) {}
30  
  
31  
  bool get(S s) { ret a.get(s) || b.get(s); }
32  
}
33  
34  
CtxExpr > CtxRegexp {
35  
  S pattern;
36  
  
37  
  *() {}
38  
  *(S *pattern) {}
39  
  
40  
  bool get(S s) { ret evalPattern(pattern, s); }
41  
}
42  
43  
concept Rule {
44  
  CtxRule1 source;
45  
  CtxExpr expr;
46  
}
47  
48  
static new TreeMap<S, S> regexpMacros;
49  
static new TreeSet<S> patterns;
50  
51  
p {
52  
  silentGC();
53  
  L<CtxRule1> rules = ctxFindRules(#1200000);
54  
  regexpMacros.putAll(ctxLoadRegexpMacros(#1200000));
55  
  //pnl(regexpMacros);
56  
  print("Found " + n(rules, "rules") + ", " + n(regexpMacros, "macros"));
57  
  for (CtxRule1 rule : rules) {
58  
    Explain e = explain(rule.text(), ctxParsingRules(), "rule");
59  
    if (e == null)
60  
      print("Bad rule: " + rule.text());
61  
    else {
62  
      cnew(Rule, source := rule, expr := ctxParseExpression(e.sub(1)));
63  
    }
64  
  }
65  
  evalRulesOnString("wie geht es dir");
66  
  save("patterns");
67  
  botSleep();
68  
}
69  
70  
sS answer(S s) {
71  
  ret evalRulesOnString(s);
72  
}
73  
74  
static S evalRulesOnString(S s) {
75  
  for (Rule rule) try {
76  
    if (rule.expr != null && rule.expr.get(s)) {
77  
      //print("Fire! " + rule.source.file);
78  
      print("Fire! " + rule.source.text());
79  
    }
80  
  } catch e {
81  
    //printExplainTree(rule.explain);
82  
    rethrow(e);
83  
  }
84  
  ret "";
85  
}
86  
87  
static CtxExpr ctxParseExpression(Explain exp) {
88  
  if (exp.is("bracket"))
89  
    ret ctxParseExpression(exp.sub(0));
90  
  if (exp.is("and"))
91  
    ret new CtxAnd(ctxParseExpression(exp.sub(0)),
92  
      ctxParseExpression(exp.sub(1)));
93  
  if (exp.is("or"))
94  
    ret new CtxOr(ctxParseExpression(exp.sub(0)),
95  
      ctxParseExpression(exp.sub(1)));
96  
  if (exp.is("neg"))
97  
    ret new CtxNeg(ctxParseExpression(exp.sub(0));
98  
  if (exp.is("quoted"))
99  
    ret new CtxRegexp(unquoteCtx(exp.tok().get(1)));
100  
  if (exp.singleEqualChild())
101  
    ret ctxParseExpression(exp.sub(0));
102  
  throw todo(exp.className());
103  
}
104  
105  
sbool evalPattern(S pat, S input) {
106  
  if (empty(pat)) false; // XXX
107  
  pat = ctxExpandMacros(pat, regexpMacros);
108  
  //print("Pattern: " + pat);
109  
  print("Pattern length: " + l(pat));
110  
  patterns.add(pat);
111  
  ret regexpCtx(pat, input).find();
112  
}

Author comment

Began life as a copy of #1008840

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: #1008852
Snippet name: Load ctx rules
Eternal ID of this version: #1008852/5
Text MD5: 1f618b4c6d95d62ba5cc6e8f85ce078b
Transpilation MD5: 7eccb4b2e46dd20ff28b635afbd369e2
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:15:45
Source code size: 2482 bytes / 112 lines
Pitched / IR pitched: No / No
Views / Downloads: 418 / 497
Version history: 4 change(s)
Referenced in: [show references]