1 | bool debug; |
2 | bool imagineMode; |
3 | bool collectCheckingLog = true; |
4 | new LinkedHashSet<S> imaginedFacts; |
5 | LS checkingLog; |
6 | long processingTime; |
7 | new L<IfThen> activeTempRules; |
8 | |
9 | visualize {
|
10 | ret withCenteredButtons(super.visualize(), |
11 | "Checking Log", r { showText("Checking Log", lines(checkingLog)) });
|
12 | } |
13 | |
14 | S evalExp(Exp e, NLLogicChecker_v2.Matching m) {
|
15 | S code = nlLogic_text(e); |
16 | print("Eval'ing: " + code);
|
17 | temp tempAdd(hotwire_copyOver_after, voidfunc(Class c) {
|
18 | copyFields(mc(), c, 'telegram_msg_tl, 'telegram_recentHistory_tl) |
19 | }); |
20 | S result = str(evalWithDollarVars(code, m.matches)); |
21 | print("Result: " + shorten(result, 100));
|
22 | ret result; |
23 | } |
24 | |
25 | void thinkAbout(S s) {
|
26 | time "Processing Time For Message"{
|
27 | thinkAbout_impl(s); |
28 | } |
29 | processingTime = lastTiming(); |
30 | } |
31 | |
32 | void thinkAbout_impl(S input) {
|
33 | new Thinking().thinkAbout_impl(input); |
34 | } |
35 | |
36 | void _postMessage(S s) { postMessage(s); }
|
37 | |
38 | class Thinking {
|
39 | long cancelTime = now()+20000; |
40 | new Map<S, Int> ruleScores; |
41 | new LinkedHashSet<S> rewrittenInputs; |
42 | new LinkedHashSet<S> newRewrittenInputs; |
43 | bool posted; |
44 | |
45 | void postMessage(S s) {
|
46 | _postMessage(s); |
47 | posted = true; |
48 | } |
49 | |
50 | void thinkAbout_impl(S input) {
|
51 | if (eq(input, "!initiative")) {
|
52 | L<IfThen> rules = mL_parsedNLRulesWithoutIDs("Initiative rules");
|
53 | for (IfThen rule : shuffledIterator(rules)) {
|
54 | Pair<Exp> p = nlLogic_extractFirstCondition(rule.in); |
55 | if (p == null) continue; |
56 | if (nlLogic_isFunc(p.a, 'initiative)) {
|
57 | postMessage(nlLogic_text(((Func) p.a).arg)); |
58 | IfThen temp = IfThen(p.b, rule.out); |
59 | temp.globalID = aGlobalID(); |
60 | print("Have temp rule: " + sfu(temp));
|
61 | activeTempRules.add(temp); |
62 | ret; |
63 | } |
64 | } |
65 | ret; |
66 | } |
67 | |
68 | ai_tg_collectRuleFeedback(ruleScores, null); |
69 | |
70 | newRewrittenInputs.add(input); |
71 | final Map msg = telegram_msg(); |
72 | |
73 | NLLogicChecker_v3 c = new NLLogicChecker_v3 {
|
74 | bool checkExpression_impl(Exp e, Matching m) {
|
75 | if (e instanceof Literal) {
|
76 | // TEXT CONDITIONS |
77 | |
78 | S text = nlLogic_text(e); |
79 | if (eq(text, "authorized")) |
80 | ret telegram_amIAuthorized(); |
81 | else if(eq(text, "imagineMode")) |
82 | ret imagineMode; |
83 | } |
84 | |
85 | ret super.checkExpression_impl(e, m); |
86 | } |
87 | }; |
88 | |
89 | c.evaluator = func(Exp e, NLLogicChecker_v2.Matching m) -> S { evalExp(e, m) };
|
90 | |
91 | if (collectCheckingLog) {
|
92 | checkingLog = new L; |
93 | nlLogic_collectCheckingLog(c, checkingLog); |
94 | } |
95 | |
96 | Pair<L<IfThen>, LS> rulesAndFacts = getRulesAndFacts(); |
97 | c.facts = reversed(rulesAndFacts.b); |
98 | L<IfThen> rules = concatLists_conservative(activeTempRules, reversed(rulesAndFacts.a)); // latest rules first! |
99 | activeTempRules.clear(); |
100 | |
101 | while (!posted && now() < cancelTime && nempty(newRewrittenInputs)) {
|
102 | fS s = first(newRewrittenInputs); |
103 | rewrittenInputs.add(s); |
104 | newRewrittenInputs.remove(s); |
105 | |
106 | c.input = s; |
107 | c.recentHistory = recentHistory; |
108 | final new L<RuleWithParams> battleSpace; |
109 | |
110 | applyNLLogicFacts_v4_verbose.set(debug); |
111 | applyNLLogicFacts_v4(c, voidfunc(IfThen rule, NLLogicChecker_v2.Matching m) {
|
112 | // Rule fired! |
113 | |
114 | battleSpace.add(new RuleWithParams(rule, m.matches)); |
115 | }, rules); |
116 | |
117 | print("Have " + n2(battleSpace, "possible rule"));
|
118 | |
119 | nlLogic_battleItOut(battleSpace, c.facts); |
120 | |
121 | L<RuleWithParams> winners = nlLogic_highestScore(battleSpace, ruleScores); |
122 | |
123 | fS msgGlobalID = getString(msg, 'globalID); |
124 | //print("msgGlobalID=" + msgGlobalID);
|
125 | for (RuleWithParams r : winners) {
|
126 | print("Firing rule " + r.rule.globalID);
|
127 | |
128 | // Save rule fire |
129 | if (nempty(msgGlobalID) && nempty(r.rule.globalID)) {
|
130 | S fact = format("Rule * fired on message * at *", r.rule.globalID, msgGlobalID, localDateWithMilliseconds());
|
131 | if (nempty(r.matches)) |
132 | fact += " with vars " + dropPrefix("lhm", struct(r.matches));
|
133 | print("Saving rule fire. Mech mode: " + mechMode());
|
134 | print(addToMechList("Telegram Rule Fires", print(fact)));
|
135 | } |
136 | |
137 | executeRule(c, r); |
138 | } |
139 | |
140 | if (collectCheckingLog) |
141 | print("Checking log length: " + l(checkingLog));
|
142 | } |
143 | } |
144 | |
145 | void executeRule(NLLogicChecker_v3 c, RuleWithParams r) {
|
146 | // Execute rule |
147 | for (Exp e : nlLogic_unrollAnd(r.rule.out)) {
|
148 | e = c.apply(e, r.matches); |
149 | if (e cast Func) {
|
150 | S name = e.name; |
151 | if (eqOneOf(name, 'output, 'say)) |
152 | postMessage(nlLogic_text(e.arg)); |
153 | else if (eq(name, 'fact)) {
|
154 | S fact = nlLogic_text(e.arg); |
155 | if (imagineMode) {
|
156 | if (!containsNL(imaginedFacts, fact)) {
|
157 | imaginedFacts.add(fact); |
158 | postMessage("Storing imaginary fact: " + fact);
|
159 | } |
160 | } else if (!ai_isQuestion_2(fact) && ai_storeActualFact(fact)) |
161 | postMessage("Storing fact: " + fact);
|
162 | } else if (eq(name, 'storeRule)) {
|
163 | S rule = nlLogic_text(e.arg); |
164 | if (!telegram_authorizedToStoreRule(rule)) continue; |
165 | rule = nlLogic_addGlobalID(rule); |
166 | if (mech_changed(appendToMechList_noUniq("NL Logic Examples", "\n" + rule)))
|
167 | postMessage("Rule stored");
|
168 | } else if (eq(name, 'imagineMode)) {
|
169 | imagineMode = match("true", nlLogic_text(e.arg));
|
170 | if (!imagineMode) imaginedFacts.clear(); |
171 | } else if (eq(name, 'input)) {
|
172 | S x = nlLogic_text(e.arg); |
173 | if (!rewrittenInputs.contains(x) && newRewrittenInputs.add(x)) |
174 | print("New rewritten input: " + x);
|
175 | } else |
176 | ret with print("Skipping rule with unknown RHS func: " + e.name);
|
177 | } else |
178 | ret with print("Skipping rule with unknown RHS element: " + e);
|
179 | } |
180 | } |
181 | } // end class Thinking |
182 | |
183 | Pair<L<IfThen>, LS> getRulesAndFacts() {
|
184 | ret ai_activeRulesAndFacts(imagineMode ? asList(imaginedFacts) : null); |
185 | } |
Began life as a copy of #1017946
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: | #1017968 |
| Snippet name: | Telegram Facts Bot [Include, backup, pre plan] |
| Eternal ID of this version: | #1017968/1 |
| Text MD5: | a02bebc5d08172318744454e32d5b134 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2018-08-23 16:48:39 |
| Source code size: | 6589 bytes / 185 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 539 / 531 |
| Referenced in: | [show references] |