!7 static JTextArea ta; static L usedConcepts; static new L facts; p-substance { aiEnhancements(); usedConcepts = findAIConcepts( "rplldtbwbbnlrotj kjuwvfidiwmbjsuc xzhtvnqwzuuzixci mibnjwgfxnetaxyz cdtwmxtqspigpyap kdpoqkaoypqenusd"); // set englishToConceptLanguage_xyz_debug; facts = clParse(linesToCL([[ "!" is a typical first word in a program. "Hello" is a typical first word in a letter. cdtwmxtqspigpyap confirms mibnjwgfxnetaxyz. ]])); ta = showText(""); onChangeAndNow(ta, r { fS text = getText(ta); thread { think(text); } }); requestFocus(ta); } sS firstWord(S s) { if (empty(s)) ret ""; if (isLetterOrDigit(first(s))) ret takeCharsWhile(s, f isLetterOrDigit); else ret "" + first(s); } svoid think(S text) { time { S w = firstWord(text); /* w is the first word in x. Is x a letter (1) or a program (2)? */ S riddle = quote(w) + " is the first word in x. Is x a letter (1) or a program (2)?"; L localFacts = concatLists(facts, clParse(sentencesToCL(riddle))); int answer = solveRiddle(localFacts); S type = answer == 1 ? "letter" : answer == 2 ? "program" : null; print("Answer: " + or2(type, "?")); titleStatus_trailing(ta, empty(type) ? null : "Writing a " + type); } } static L sentencesToCL(S text) { ret listToCL(splitIntoSentences(text)); } static L linesToCL(S text) { ret listToCL(toLinesFullTrim(text)); } static L listToCL(L l) { englishToConceptLanguage_concepts.set(usedConcepts); new L out; for (S line : l) { S c = englishToConceptLanguage(line); print(line + " => " + c); out.add(c); } ret out; } static int solveRiddle(L facts) { // Find the question Lisp q = findHead(facts, "rplldtbwbbnlrotj"); if (q == null) fail("No question found"); S pivot = q.s(0); L candidates = ll(q.s(1), q.s(2)); new Best best; for i over candidates: { S candidate = candidates.get(i); new Web web; web.newNode(pivot, candidate); for (Lisp fact : facts) web.relation(fact); int score = countConfirms(web, facts); if (score > 0) best.put(i+1, score); } ret toInt(best.get()); } static int countConfirms(Web web, L facts) { int score = 0; for (Lisp fact : filterByHead(facts, "kdpoqkaoypqenusd")) { S a = fact.s(0), b = fact.s(1); for (WebNode rel : values(web.relations)) if (rel.hasLabel(a) && rel.hasLabel(b)) ++score; } ret score; } sclass WebNode { new L labels; void addLabel(S label) { setAdd(labels, label); } bool hasLabel(S label) { ret labels.contains(label); } } sclass Web { new L nodes; Map, WebNode> relations = new HashMap; void relation(Lisp l) { if (l(l) != 2) ret; getRelation(l.s(0), l.s(1)).addLabel(l.head); } WebNode getRelation(S a, S b) { Pair p = pair(findNode(a), findNode(b)); WebNode r = relations.get(p); if (r == null) relations.put(p, r = new WebNode); ret r; } WebNode findNode(S s) { for (WebNode n : nodes) if (n.labels.contains(s)) ret n; ret newNode(s); } WebNode newNode(S... labels) { new WebNode n; for (S label : labels) n.addLabel(label); nodes.add(n); ret n; } }