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

323
LINES

< > BotCompany Repo | #1008086 // Compare Bot 2 [almost works]

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

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (10968L/78K).

1  
!7
2  
3  
sbool killDB = false;
4  
static int defaultMax = 1000; // steps to run
5  
6  
sS facts = [[
7  
  We want to find out what is more correct than what.
8  
  
9  
  "not correct" is a candidate.
10  
  "maybe quite correct" is a candidate.
11  
  "really really correct" is a candidate.
12  
  "buggy" is a candidate.
13  
  "total garbage" is a candidate.
14  
  "incorrect" is a candidate.
15  
  "almost correct" is a candidate.
16  
  "absolutely correct" is a candidate.
17  
  "very correct" is a candidate.
18  
  
19  
  [nfhwfngqkgoqtngs] (no question) and (X is a candidate.) and (Y is a candidate.) and (X <> Y) and (There is no answer to (Is X # more correct than Y?).) => (Ask (Is X # more correct than Y?))
20  
  // Note: The # is just a filler for parsing, we'll get rid of that in time. Also, you will be able to write stuff without brackets of course.
21  
  
22  
  Always apply rule nfhwfngqkgoqtngs.
23  
  
24  
  // Interpret answers
25  
  [iprphzfmwithvdfb] (The answer to (Is X # Y than Z?) is yes.) => (X is Y than Z.)
26  
  Always apply rule iprphzfmwithvdfb.
27  
  [udsmtmbiedmxtxci] (The answer to (Is X # Y than Z?) is no.) => (Z is Y than X.)
28  
  Always apply rule udsmtmbiedmxtxci.
29  
  
30  
  // Transitivity!
31  
  [pglcuchrtxdmlfvh] (X is AA than Y.) and (Y is AA than Z.) => (X is AA than Z.)
32  
  Always apply rule pglcuchrtxdmlfvh.
33  
  
34  
  [ijziyiplzanfwvqx] (Ask (X)) and (The answer to X is Y.) => (Remove (Ask (X)))
35  
  Always apply rule ijziyiplzanfwvqx.
36  
  
37  
  no question :- There is no statement with operator fxxathfdlfzgmaov.
38  
  // Just a pretty-printing statement
39  
  omrvwjslbkffaxoj hszwyiyetuuhcfwz acwmbrxqrlaplyqu
40  
]];
41  
42  
sS concepts = [[
43  
  ufzlismnlhvbjqoj - (X) and (Y) and (Z) and (AA) and (AB) => (AC)
44  
  phkdhqkhzytkwdpb - (X) and (Y) and (Z) and (AA) => (AB)
45  
  txsysipmstxmjhmi - (X) and (Y) => (Z)
46  
  wziyypnyayfphdxs - (X) => (Y)
47  
  xikyminwmeahxiws - There is no answer to (X).
48  
  tlwrjpzuteopednt - The answer to X is Y.
49  
  cdzttdbxadjreorm - We want to find out what is X than what.
50  
  ogqdgzepqbunuuht - X is a candidate.
51  
  hszwyiyetuuhcfwz - Is X # Y than Z?
52  
  acwmbrxqrlaplyqu - Is "X" Y than "Z"?
53  
  qgxjwufpfzkqvmxb - X is Y than Z.
54  
  ylvaoplzguvhegka - X is not Y than Z.
55  
  zxucpbfzmexohoiv - It is not true that (X).
56  
  buahjsodljsaxvaq - X <> Y
57  
  fxxathfdlfzgmaov - Ask (X)
58  
  chjllkocfhtwcgoj - Remove (X)
59  
  mzdvauejerzefagk - I don't know if (X)
60  
  omrvwjslbkffaxoj - Format operator X as template Y.
61  
  houyieutcmfdvgmz - Execute (X)
62  
  hzmtabxcmkrtffgh - Apply X
63  
  zvknqzxlrmuobnzx - Always apply rule X.
64  
  plprefzezbluijrw - no question
65  
  jexyjrtbtilmsmfw - setQuestion(X)
66  
  cuutddpitkuswthu - While (X) => (Y)
67  
  sicrogpdrtkiptun - X :- Y
68  
  zhoulgsatpswstfa - There is no statement with operator X.
69  
  ltyqczktgtjxrtlf - more correct
70  
  zveeggveflecjwvr - total garbage
71  
  wolealpbqkkqkvqq - not correct
72  
  tomkbzlqlpeiekvu - maybe quite correct
73  
  dnxyynujqydgcxcd - really really correct
74  
  trajdgvqepyhsumb - buggy
75  
  lwikikhkydtewtjc - incorrect
76  
  muxjpepnjiyjghwj - almost correct
77  
  wtnpjovqqiswlyay - absolutely correct
78  
  bmliokweerblfxco - very correct
79  
]];
80  
81  
concept MyTruth {
82  
  S globalID;
83  
  Lisp term;
84  
  S madeByRule;
85  
  
86  
  *() {}
87  
  *(S *globalID, Lisp *term, S *madeByRule) { change(); }
88  
}
89  
90  
static Map<S, F1<Lisp, Bool>> evaluators = new HashMap;
91  
static Lisp currentQuestion;
92  
  
93  
p-tt {
94  
  centerHigherConsole();
95  
  substance();
96  
  
97  
  // customize parsing and printing
98  
  set englishToConceptLanguage_unquote;
99  
  set englishToConceptLanguage_simple_noXYZ;
100  
  set formatXYZ_quotedVars;
101  
  
102  
  // load predefined & learned statements
103  
  useConceptsAndStatements(concepts, facts);
104  
  if (killDB) deleteDB();
105  
  db();
106  
  for (MyTruth t)
107  
    lispAddLocalTruth(t.globalID, t.term);
108  
  printLispStatements();
109  
  
110  
  // create evaluators
111  
  
112  
  // X <> Y
113  
  evaluators.put("buahjsodljsaxvaq", new F1<Lisp,Bool>() {
114  
    Bool get(Lisp l) {
115  
      ret l.size() == 2 && neq(l.get(0), l.get(1));
116  
    }
117  
  });
118  
  
119  
  // I don't know if (X)
120  
  evaluators.put("mzdvauejerzefagk", new F1<Lisp,Bool>() {
121  
    Bool get(Lisp l) {
122  
      ret l.size() == 1 && notKnownIf(l.get(0));
123  
    }
124  
  });
125  
  
126  
  // There is no statement with operator X.
127  
  evaluators.put("zhoulgsatpswstfa", new F1<Lisp,Bool>() {
128  
    Bool get(Lisp l) {
129  
      ret l.size() == 1 && !hasTruthWithHead(l.raw(0));
130  
    }
131  
  });
132  
  
133  
  // There is no answer to (X).
134  
  evaluators.put("xikyminwmeahxiws", new F1<Lisp,Bool>() {
135  
    Bool get(Lisp l) {
136  
      bool ok = l.size() == 1 && null == lispForward("tlwrjpzuteopednt", l.get(0));
137  
      print("Checking for answer to " + l.get(0) + " => " + ok);
138  
      ret ok;
139  
    }
140  
  });
141  
  
142  
  // :- rules
143  
  for (final Lisp rule : lispTruth2("sicrogpdrtkiptun"))
144  
    evaluators.put(rule.raw(0), new F1<Lisp,Bool>() {
145  
    Bool get(Lisp l) {
146  
      ret l.isLeaf() && matchCondition(rule.get(1));
147  
    }
148  
  });
149  
  
150  
  bot();
151  
  
152  
  //applyRule("nfhwfngqkgoqtngs");
153  
  //applyAlwaysRules();
154  
  //runInstruction("yrgkrfrcwubpevzu");
155  
}
156  
157  
answer {
158  
  if "lisp" { printLispStatements(); ret "ok"; }
159  
  if "print" { printPrettyFacts(); ret "ok"; }
160  
  if "rule *" { applyRule($1); ret "ok"; }
161  
  if "step" { applyAlwaysRules_step(); ret "ok"; }
162  
  if "run" { applyAlwaysRules(defaultMax); ret "ok"; }
163  
  if "force" { force(defaultMax); ret "ok"; }
164  
}
165  
166  
svoid showQuestion() {
167  
  L<Lisp> l = lispTruth1("fxxathfdlfzgmaov");
168  
  if (l(l) > 1) warn("Multiple open questions");
169  
  if (nempty(l)) {
170  
    final Lisp q = first(l).get(0);
171  
    if (eq(currentQuestion, q)) ret;
172  
    currentQuestion = q;
173  
    print("Question: " + q);
174  
    swing {
175  
      showControls2(
176  
        centerAndSouth(
177  
          withBottomMargin(jcenteredBoldLabel(lispToEnglish_prettier(q))),
178  
          jcenteredline(answerButton("Yes"), answerButton("No"), answerButton("I don't know")));
179  
    }
180  
  }
181  
}
182  
183  
svoid runInstruction(S instructionID) {
184  
  Lisp rule = getLispTruth(instructionID);
185  
  if (rule == null) { print("Instruction not found: " + instructionID); ret; }
186  
  runInstruction(rule);
187  
}
188  
  
189  
svoid runInstruction(Lisp rule) {
190  
  print("runInstruction: " + rule);
191  
  if (rule.is("hzmtabxcmkrtffgh", 1)) // Apply X
192  
    applyRule(rule.raw(0));
193  
  else if (rule.is("cuutddpitkuswthu", 2)) { // While (X) => (Y)
194  
    while (ping() && matchCondition(rule.get(0)))
195  
      runInstruction(rule.get(1));
196  
  } /*else if (rule.is("houyieutcmfdvgmz", 1))
197  
    execute(rule.get(0));
198  
  /*else if (rule.is("jexyjrtbtilmsmfw", 1)) // setQuestion(X)
199  
    setQuestion(rule.get(0));*/
200  
  else print("Unknown instruction type: " + rule);
201  
}
202  
203  
svoid applyRule(S ruleID) {
204  
  // process the rule
205  
  Lisp rule = getLispTruth(ruleID);
206  
  if (rule == null) { print("Rule not found: " + ruleID); ret; }
207  
  //print("Applying rule " + ruleID);
208  
  print_setPrefixForThread(ruleID + "> ");
209  
  try {
210  
    L<Lisp> conditions = dropLast(rule.args);
211  
    Lisp out = last(rule.args);
212  
    //for (Lisp cond : conditions) print("Condition: " + cond);
213  
      
214  
    new Map<S, Lisp> matches;
215  
    int n = matchConditions_random(conditions, matches);
216  
    if (n == -1) {
217  
      print("Yo! " + struct(matches));
218  
      out = lispReplaceVars(out, matches);
219  
      if (lispTrue(out))
220  
        print("Already had: " + out);
221  
      else {
222  
        print("Defining: " + lispToEnglish_prettier(out));
223  
        emit(out, ruleID);
224  
      }
225  
    } else
226  
      print("Only " + n + " of " + n(conditions, "condition") + " met");
227  
  } finally {
228  
    print_setPrefixForThread("");
229  
  }
230  
}
231  
232  
// if match: returns -1
233  
// if no match: returns how many conditions were met
234  
static int matchConditions_random(L<Lisp> conditions, Map<S, Lisp> m) {
235  
  if (empty(conditions)) ret -1;
236  
  Lisp condition = first(conditions);
237  
  Map<S, Lisp> m2 = cloneMap(m);
238  
  if (!matchCondition_random(condition, m2)) {
239  
    print("Condition failed: " + condition);
240  
    ret 0;
241  
  }
242  
  print("Condition matched, " + struct(m2));
243  
  int n = matchConditions_random(dropFirst(conditions), m2);
244  
  if (n == -1) {
245  
    m.putAll(m2);
246  
    ret -1;
247  
  }
248  
  ret n+1; // failure
249  
}
250  
251  
sbool matchCondition(Lisp condition) {
252  
  print("matchCondition " + condition);
253  
  O evaluator = evaluators.get(condition.head);
254  
  if (evaluator != null)
255  
    ret isTrue(callF(evaluator, condition));
256  
  ret lispTrue(condition);
257  
}
258  
259  
static bool matchCondition_random(Lisp condition, Map<S, Lisp> m) {
260  
  L<Lisp> facts = lispTruth();
261  
  
262  
  O evaluator = evaluators.get(condition.head);
263  
  if (evaluator != null)
264  
    ret isTrue(callF(evaluator, lispReplaceVars(condition, m)));
265  
266  
  L<Map<S, Lisp>> candidates = new L;
267  
  for (Lisp fact : facts) {
268  
    Map<S, Lisp> m2 = cloneMap(m);
269  
    if (lispMatchIC_xyzVars_sub(condition, fact, m2))
270  
      candidates.add(m2);
271  
  }
272  
  //print("Have " + n(candidates, "candidate"));
273  
  if (empty(candidates)) false;
274  
  m.putAll(random(candidates));
275  
  true;
276  
}
277  
278  
svoid force(long steps) {
279  
  for (long step = 0; step < steps; step++)
280  
    applyAlwaysRules_step();
281  
}
282  
283  
svoid applyAlwaysRules(long maxSteps) {
284  
  long count = 0, step = 0;
285  
  do {
286  
    count = lispChangeCount();
287  
    applyAlwaysRules_step();
288  
  } while (++step < maxSteps && lispChangeCount() != count);
289  
}
290  
291  
svoid applyAlwaysRules_step() {
292  
  for (Lisp l : lispTruth1("zvknqzxlrmuobnzx"))
293  
    applyRule(l.raw(0)); // TODO: not only random application
294  
  for (Lisp l : lispTruth1("chjllkocfhtwcgoj")) { // Remove (X)
295  
    unemit(l);
296  
    unemit(l.get(0));
297  
  }
298  
  for (Lisp l : lispTruth1("hzmtabxcmkrtffgh")) { // Apply X
299  
    unemit(l);
300  
    applyRule(l.raw(0));
301  
  }
302  
  showQuestion();
303  
}
304  
305  
svoid emit(Lisp l, S madeByRule) {
306  
  LispStatement s = lispAddLocalTruth(l);
307  
  if (s != null)
308  
    new MyTruth(s.globalID, l, madeByRule);
309  
}
310  
311  
svoid unemit(Lisp l) {
312  
  LispStatement s = findLispStatement(l);
313  
  if (s == null) ret;
314  
  removeLispStatement(s);
315  
  deleteConcepts(MyTruth, globalID := s.globalID);
316  
}
317  
318  
static JButton answerButton(fS text) {
319  
  ret jbutton(text, r {
320  
    emit(lisp("tlwrjpzuteopednt", currentQuestion, englishToLisp(text)), "button");
321  
    applyAlwaysRules(defaultMax);
322  
  });
323  
}

Author comment

Began life as a copy of #1008082

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1008086
Snippet name: Compare Bot 2 [almost works]
Eternal ID of this version: #1008086/88
Text MD5: 76ae6ee36f93de77a5ec4cb8c8839384
Transpilation MD5: a10ff1683f4e3d82049c58a4a4da626b
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-04-25 01:24:15
Source code size: 10015 bytes / 323 lines
Pitched / IR pitched: No / No
Views / Downloads: 718 / 1536
Version history: 87 change(s)
Referenced in: [show references]