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

!7

concept Rule {
  CtxRule1 source;
  Explain explain;
}

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, explain := e);
    }
  }
  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 (evalRule(rule.explain, s)) {
      //print("Fire! " + rule.source.file);
      print("Fire! " + rule.source.text());
    }
  } catch e {
    printExplainTree(rule.explain);
    rethrow(e);
  }
  ret "";
}

static bool evalRule(Explain e, S input) {
  ret eval(e.sub(1), input);
}

static bool eval(Explain exp, S input) {
  if (exp.is("bracket"))
    ret eval(exp.sub(0), input);
  if (exp.is("and"))
    ret eval(exp.sub(0), input) && eval(exp.sub(1), input);
  if (exp.is("or"))
    ret eval(exp.sub(0), input) || eval(exp.sub(1), input);
  if (exp.is("neg"))
    ret !eval(exp.sub(0), input);
  if (exp.is("quoted")) {
    S pattern = unquoteCtx(exp.tok().get(1));
    ret evalPattern(pattern, input);
  }
  if (exp.singleEqualChild())
    ret eval(exp.sub(0), input);
  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();
}

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: 537 / 857
Version history: 41 change(s)
Referenced in: [show references]