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

!7

abstract sclass CtxExpr {
  abstract bool get(S s);
}

CtxExpr > CtxNeg {
  CtxExpr e;
  
  *() {}
  *(CtxExpr *e) {}
  
  bool get(S s) { ret !e.get(s); }
}

CtxExpr > CtxAnd {  
  CtxExpr a, b;
  
  *() {}
  *(CtxExpr *a, CtxExpr *b) {}

  bool get(S s) { ret a.get(s) && b.get(s); }
}

CtxExpr > CtxOr {  
  CtxExpr a, b;
  
  *() {}
  *(CtxExpr *a, CtxExpr *b) {}
  
  bool get(S s) { ret a.get(s) || b.get(s); }
}

CtxExpr > CtxRegexp {
  S pattern;
  
  *() {}
  *(S *pattern) {}
  
  bool get(S s) { ret evalPattern(pattern, s); }
}

concept Rule {
  CtxRule1 source;
  CtxExpr expr;
}

static new TreeMap<S, S> regexpMacros;
static new TreeSet<S> patterns;

p {
  silentGC();
  L<CtxRule1> rules = ctxFindRules(#1200000);
  regexpMacros.putAll(ctxLoadRegexpMacros(#1200000));
  //pnl(regexpMacros);
  print("Found " + n(rules, "rules") + ", " + n(regexpMacros, "macros"));
  for (CtxRule1 rule : rules) {
    Explain e = explain(rule.text(), ctxParsingRules(), "rule");
    if (e == null)
      print("Bad rule: " + rule.text());
    else {
      cnew(Rule, source := rule, expr := ctxParseExpression(e.sub(1)));
    }
  }
  evalRulesOnString("wie geht es dir");
  save("patterns");
  botSleep();
}

sS answer(S s) {
  ret evalRulesOnString(s);
}

static S evalRulesOnString(S s) {
  for (Rule rule) try {
    if (rule.expr != null && rule.expr.get(s)) {
      //print("Fire! " + rule.source.file);
      print("Fire! " + rule.source.text());
    }
  } catch e {
    //printExplainTree(rule.explain);
    rethrow(e);
  }
  ret "";
}

static CtxExpr ctxParseExpression(Explain exp) {
  if (exp.is("bracket"))
    ret ctxParseExpression(exp.sub(0));
  if (exp.is("and"))
    ret new CtxAnd(ctxParseExpression(exp.sub(0)),
      ctxParseExpression(exp.sub(1)));
  if (exp.is("or"))
    ret new CtxOr(ctxParseExpression(exp.sub(0)),
      ctxParseExpression(exp.sub(1)));
  if (exp.is("neg"))
    ret new CtxNeg(ctxParseExpression(exp.sub(0));
  if (exp.is("quoted"))
    ret new CtxRegexp(unquoteCtx(exp.tok().get(1)));
  if (exp.singleEqualChild())
    ret ctxParseExpression(exp.sub(0));
  throw todo(exp.className());
}

sbool evalPattern(S pat, S input) {
  if (empty(pat)) false; // XXX
  pat = ctxExpandMacros(pat, regexpMacros);
  //print("Pattern: " + pat);
  print("Pattern length: " + l(pat));
  patterns.add(pat);
  ret regexpCtx(pat, input).find();
}

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: 485 / 587
Version history: 4 change(s)
Referenced in: #1008854 - Load ctx rules + form