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

129
LINES

< > BotCompany Repo | #1008854 // Load ctx rules + form

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

Libraryless. Click here for Pure Java version (9004L/61K/210K).

!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");
  
  final JTextField tfPat = jtextfield();
  final JTextField tfInput = jtextfield();
  final JTextField tfOutput = jtextfield();
  for (JTextField tf : ll(tfPat, tfInput, tfOutput))
    setFontSize(tf, 20);
  
  showForm(
    "Pattern", tfPat,
    "Input", tfInput,
    "Matches", tfOutput,
    func {
      bool matches = evalPattern(getText(tfPat), getText(tfInput));
      tfOutput.setText(yn(matches));
      false;
    });
  
  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 #1008852

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: #1008854
Snippet name: Load ctx rules + form
Eternal ID of this version: #1008854/4
Text MD5: 4071d030a9121a5b1ddcd80098c85b23
Transpilation MD5: 3d6745a4e73a4911e7d0c8ea60c42c20
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-13 14:49:22
Source code size: 2932 bytes / 129 lines
Pitched / IR pitched: No / No
Views / Downloads: 436 / 524
Version history: 3 change(s)
Referenced in: [show references]