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

158
LINES

< > BotCompany Repo | #1017752 // Telegram Facts Bot v4 [Dyn Module, with entities, old]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 408K of libraries. Compilation Failed (22400L/168K).

1  
!7
2  
3  
set flag DynModule.
4  
5  
sclass TelegramFactsBot > AbstractTelegramThinkBot {
6  
  S evalExp(Exp e, NLLogicChecker_v2.Matching m) {
7  
    S code = nlLogic_text(e);
8  
    print("Eval'ing: " + code);
9  
    temp tempAdd(hotwire_copyOver_after, voidfunc(Class c) { copyFields(mc(), c, 'telegram_msg_tl) });
10  
    S result = str(evalWithDollarVars(code, m.matches));
11  
    print("Result: " + shorten(result, 100));
12  
    ret result;
13  
  }
14  
15  
  void thinkAbout(fS s) {
16  
    final Map msg = telegram_msg();
17  
    
18  
    NLLogicChecker_v2 c = new NLLogicChecker_v2 {
19  
      L<S> entities;
20  
      
21  
      bool checkExpression(Exp e, Matching m) {
22  
        new Matches mm;
23  
        if (e cast Func) {
24  
          // FUNCTION CONDITIONS
25  
          
26  
          if (eq(e.name, "verbPhraseFromThirdPerson"))
27  
            ret nlLogic_stringFunction(f ai_verbPhraseFromThirdPerson, e, m.matches);
28  
          else if (eq(e.name, "singular"))
29  
            ret nlLogic_stringFunction(f singular, e, m.matches);
30  
          else if (eq(e.name, 'eval))
31  
            ret eq("true", evalExp(e.arg, m));
32  
          else if (eq(e.name, 'entity)) {
33  
            if (entities == null) {
34  
              long time = sysNow();
35  
              fS switched = switcheroo(s);
36  
              print("Switched >> " + switched);
37  
              entities = evalWithTimeoutOrNull(5000, func -> LS {
38  
                mapMethod('text, ai_extractEntities_v1(switched))
39  
              });
40  
              print("Entities (" + elapsedMS(time) + " ms): " + joinWithComma(entities));
41  
              if (entities == null) entities = new L;
42  
            }
43  
            for (S entity : entities)
44  
              if (matcher.match(nlLogic_text(e.arg), entity, m.matches)) true;
45  
          } else if (startsWith(e.name, "line", mm) && isInteger(mm.rest())) {
46  
            int n = parseInt(mm.rest())-nlLogic_numberOfLinesReferenced(checkingRule->in);
47  
            S line = n == 0 ? s : getString(get(recentHistory, l(recentHistory)+n), 'text);
48  
            print("Recent: " + recentHistory);
49  
            S pat = nlLogic_text(e.arg);
50  
            print("n=" + n + ", Matching " + e + " with " + line);
51  
            ret matcher.match(pat, line, m.matches);
52  
          } else if (eq(e.name, 'unknownIf)) {
53  
            S statement = nlLogic_text(apply(e.arg, m));
54  
            print("Checking statement: " + statement);
55  
            ret !cic(facts, statement) && !cic(facts, "Untrue: " + statement);
56  
          } else if (eq(e.name, 'inputContainsTokens))
57  
            ret call_verbose jcontains(s, nlLogic_text(e.arg));
58  
          else if (eq(e.name, 'inputStartsWith))
59  
            ret call_verbose startsWith(s, nlLogic_text(e.arg));
60  
          else if (eq(e.name, 'followingUpOn)) {
61  
            S text = nlLogic_text(e.arg);
62  
            // e.g. "lhnshnhcklhabvmu with input=$input"
63  
            if (!match("* with input=*", text, mm) || !isDollarVar(mm.get(1))) false;
64  
            S ruleID = mm.unq(0), dollarInput = mm.get(1);
65  
            Map /*prev = last(recentHistory),*/ prevPrev = nextToLast(recentHistory);
66  
            S msgID = getString(prevPrev, 'globalID);
67  
            S input = getString(prevPrev, 'text);
68  
            print("followingUpOn: msgID=" + msgID + ", input=" + input);
69  
            printStruct(+prevPrev);
70  
            //printStruct(+prev);
71  
            S pat = "Rule " + ruleID + " fired on message " + msgID + " ";
72  
            print("pat=" + pat);
73  
            for (S ruleFired : mL("Telegram Rule Fires"))
74  
              if (swic(ruleFired, pat))
75  
                ret print("result=", strictPutIC(m.matches, dollarInput, input));
76  
            false;
77  
          }
78  
        }
79  
        
80  
        if (e cast Eq) {
81  
          // EQUATION CONDITIONS
82  
          
83  
          Exp r = e.right;
84  
          S var = nlLogic_text(e.left);
85  
          if (r cast Func) pcall {
86  
            if (eq(r.name, 'eval))
87  
              ret new NLStringMatcher_singleDollarVar().match(var, evalExp(r.arg, m), m.matches);
88  
          }
89  
        }
90  
        
91  
        if (e instanceof Literal) {
92  
          // TEXT CONDITIONS
93  
          
94  
          S text = nlLogic_text(e);
95  
          if (eq(text, "authorized"))
96  
            ret telegram_amIAuthorized();
97  
        }
98  
        
99  
        ret super.checkExpression(e, m);
100  
      }
101  
    };
102  
    c.matcher = new NLStringMatcher_dollarVars_underscores;
103  
    c.input = s;
104  
    c.facts = mL_facts();
105  
    
106  
    LS unparsedRules = ai_unparsedTalkRules();
107  
    
108  
    for (S listName : mL("Rule & Fact Lists"))
109  
      for (S x : splitAtEmptyLines(mL_raw(listName)))
110  
        if (anyJavaTokens(x))
111  
          if (nlLogic_parseRule(x) != null)
112  
            unparsedRules.add(x);
113  
          else
114  
            c.facts.add(x);
115  
116  
    unparsedRules = sortedByCalculatedField(unparsedRules, func(S s) { jcontains(s, "entity(") });
117  
    L<IfThen> rules = map_pcall nlLogic_parseRule(unparsedRules);
118  
    
119  
    for (IfThen r : rules) {
120  
      continue if r == null;
121  
      Exp out = r.out;
122  
      if (out cast Func)
123  
        if (eq(out.name, "sayIfTrue")) {
124  
          r.out = Func('output, out.arg);
125  
          r.in = And(r.in, Func('fact, out.arg));
126  
          print(r);
127  
        }
128  
    }
129  
    
130  
    fS msgGlobalID = getString(msg, 'globalID);
131  
    if (nempty(msgGlobalID))
132  
      applyNLLogicFacts_v3_onRuleFired.set(voidfunc(IfThen rule, NLLogicChecker_v2.Matching m) {
133  
        if (empty(rule.globalID)) ret;
134  
        S fact = format("Rule * fired on message * at *", rule.globalID, msgGlobalID, localDateWithMilliseconds());
135  
         if (nempty(m.matches))
136  
           fact += " with vars " + dropPrefix("lhm", struct(m.matches));
137  
        addToMechList("Telegram Rule Fires", fact);
138  
      });
139  
140  
    applyNLLogicFacts_v3(c, voidfunc(Exp e) {
141  
      // Rule fired!
142  
      
143  
      if (e cast Func) {
144  
        if (eq(e.name, 'output))
145  
          postMessage(nlLogic_text(e.arg));
146  
        else if (eq(e.name, 'fact)) {
147  
          S fact = nlLogic_text(e.arg);
148  
          if (!contains(mL("Random facts"), fact) && mech_changed(addToMechList("Derived facts", fact)))
149  
            postMessage("Storing fact: " + fact);
150  
        } else if (eq(e.name, 'storeRule)) {
151  
          S rule = nlLogic_text(e.arg);
152  
          if (mech_changed(appendToMechList_noUniq("NL Logic Examples", "\n" + rule)))
153  
            postMessage("Rule stored");
154  
        }
155  
      }
156  
    }, rules);
157  
  }
158  
}

Author comment

Began life as a copy of #1017701

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1017752
Snippet name: Telegram Facts Bot v4 [Dyn Module, with entities, old]
Eternal ID of this version: #1017752/59
Text MD5: 74cdb48e93b8615e689af20c00000d74
Transpilation MD5: 71f1de8dea77719f4851de143bdef3b7
Author: stefan
Category: javax / telegram
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-08-14 12:04:49
Source code size: 6354 bytes / 158 lines
Pitched / IR pitched: No / No
Views / Downloads: 421 / 1071
Version history: 58 change(s)
Referenced in: [show references]