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

89
LINES

< > BotCompany Repo | #1017834 // nlLogic_expandMacros - rewrite macros (e.g. sayIfTrue(...))

JavaX fragment (include)

1  
static SS nlLogic_expandMacros_funcShortNames = litmap(
2  
  'o, 'output,
3  
  'i, 'input);
4  
5  
// changes original rules
6  
static void nlLogic_expandMacros(L<IfThen> rules) {
7  
  for (IfThen rule : unnull(rules)) pcall {
8  
    nlLogic_expandMacros(rule);
9  
  }
10  
}
11  
12  
static IfThen nlLogic_expandMacros(IfThen r) {
13  
  if (r == null) ret r;
14  
  int varCount = 0;
15  
16  
  S rendered;
17  
  
18  
  do {
19  
    ping();
20  
    rendered = str(r);
21  
    nlLogic_visit(r, voidfunc(O o) {
22  
      if (o cast Func) {
23  
        S name = nlLogic_expandMacros_funcShortNames.get(o.name);
24  
        if (name != null) o.name = name;
25  
      }
26  
    });
27  
    
28  
    Exp out = r.out;
29  
    if (out cast Func) {
30  
      S name = out.name;
31  
      if (eq(name, 'sayIfTrue)) {
32  
        r.out = Func('output, out.arg);
33  
        r.in = And(r.in, Func('fact, out.arg));
34  
      } else if (eq(name, 'inputToFact)) {
35  
        r.out = Func('fact, out.arg);
36  
        r.in = nlLogic_and(Func('input, restructure(out.arg)), r.in);
37  
      } else if (eqOneOf(name, 'outputEval, 'evalOutput)) {
38  
        S var = "$_" + ++varCount;
39  
        r.out = Func('output, Sentence2(var));
40  
        r.in = And(r.in, Eq(Sentence2(var), Func('eval, out.arg)));
41  
        //print("Rewritten rule => " + sfu(r));
42  
      } else if (eq(name, 'output)) {
43  
        do {
44  
          // in: output(... $x1, $x2 [...] and $x_n ...)
45  
          S pat = "*, * [...] * *";
46  
          T3<S> t = jextractWithRests(pat, out.argText());
47  
          if (t == null) break;
48  
          new Matches m;
49  
          L<S> tok = javaTok(t.b);
50  
          if (!jmatch(pat, tok, m)) break;
51  
          S x1 = m.get(0);
52  
          if (!isDollarVar(x1) || !endsWith(x1, "1")) break with print("x1");
53  
          S x = dropSuffix("1", x1);
54  
          if (!eq(m.get(1), x + "2")) break with print("x2");
55  
          //S lastSep = m.get(2);
56  
          if (!eq(m.get(3), x + "_n")) break with print("x_n");
57  
          // out: outputEval(joinWithComma_specialLast((LS) call(ai_context(), 'getList, "x"), " and "))
58  
          out.name = 'outputEval;
59  
          out.arg = Sentence2(quote(t.a) + " + joinWithComma_specialLast((LS) call(ai_context(), 'getList, " + quote(x) + "), "
60  
            + quote(joinSubList(tok, l(tok)-5, l(tok)-2)) + ")"
61  
            + " + " + quote(t.c));
62  
        } while (false);
63  
      }
64  
    }
65  
    
66  
    // LHS only
67  
    nlLogic_visit(r.in, voidfunc(O o) {
68  
      // english a/an magic
69  
      if (o cast Func)
70  
        if (eqOneOf(o.name, 'input, 'fact)) {
71  
          if (contains(o.options, 're))
72  
            o.arg = Sentence2(ctxREToJavaRE(ctxExpandMacros(o.arg.text(), ctxMacros())));
73  
          else {
74  
            S s = ai_expandPatternForAAndAn(o.arg.text());
75  
            if (s != null)
76  
              o.arg = Sentence2(s);
77  
          }
78  
        } else if (eq(o.name, 'unresolvedFact)) {
79  
          o.name = 'fact;
80  
          o.arg = Sentence2("Unresolved fact: " + o.arg.text());
81  
        } else if (eq(o.name, 'evalTrue)) {
82  
          o.name = 'eval;
83  
          o.arg = Sentence2(tok_addSemicolon(o.arg.text()) + " ret true;");
84  
        }
85  
    });
86  
  } while (neq(rendered, str(r)));
87  
  
88  
  ret r;
89  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1017834
Snippet name: nlLogic_expandMacros - rewrite macros (e.g. sayIfTrue(...))
Eternal ID of this version: #1017834/28
Text MD5: 16d7cb0441986f1795a9224d2a073628
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-09-28 04:06:32
Source code size: 3116 bytes / 89 lines
Pitched / IR pitched: No / No
Views / Downloads: 405 / 497
Version history: 27 change(s)
Referenced in: [show references]