!7 !include once #1014750 // CSnippet sS inspiration = [[ snippet titles "[OK]" ]]; static new LL facts; // stores code token parses static Map phrasesWordTree = wordTree(); static Clusters clusters = ciClusters(); static Prolog prolog; sS reasoning = [[ // check inspiration :o) assert(inspired "snippet titles") assert(inspired "\"[OK]\"") // phrases phrase "snippet titles" phrase "a list of strings" phrase "a quoted string" // synonyms "the snippet titles" = "snippet titles" // grammar singular_plural("snippet title", "snippet titles") // test parsing assert_parse("snippet titles are a list of strings", "snippet titles", "are", "a list of strings") // facts snippet titles are a list of strings "[OK]" is a quoted string // rule to answer a question fact_question_answer_upperCaseVars("X are Y", "what are X?", "Y") // check the rule assert_answer("what are snippet titles?", "a list of strings") ]]; p-experiment-tt { go(); } svoid go { prolog = new Prolog; L program = tlftj(reasoning); pnl(program); print(); //set translateUsingWordTree_debug; //set translateUsingWordTreeC_printReplacements; for (S s : tlftj(inspiration)) addFact("inspired " + quote(s)); for (S s : program) { Pair> p = parseFunctionCall(s); if (p != null) { //print("Function call: " + sfu(p)); S f = p.a; L args = p.b; if (eqic(f, "assert")) assertFact(getSingleton(p.b)); else if (swic(f, "assert_")) { if (eqic(f, "assert_answer")) assertAnswer(unquote(first(args)), unquote(second(args))); else if (eqic(f, "assert_parse")) assertParse(args); else fail("Unknown assertion function: " + f); } else if (eqic(f, "fact_question_answer_upperCaseVars")) prolog.code( print("if [" + toProlog(unquote(first(args))) + "] and [" + toProlog(unquote(second(args))) + "] then [answer is [" + toProlog(unquote(third(args))) + "]]")); continue; } L tok = javaTokC(s); if (l(tok) == 2 && isIdentifier(first(tok)) && isQuoted(second(tok))) if (eq(first(tok), "phrase")) { addPhrase(unquote(second(tok))); continue; } if (l(tok) == 3 && eq(second(tok), "=")) { ciClusters_add(clusters, addPhrase(unquote(first(tok))), addPhrase(unquote(third(tok)))); continue; } addFact(s); } printAsciiHeading("Facts"); pnlStruct(facts); print(); print("Clusters: " + sfu(clusters)); print("Phrases: " + sfu(phrasesWordTree)); //loadSnippets(); } svoid loadSnippets { loadMainConceptsFrom_readOnly(#1009918); // get snippets print("Have " + countConcepts(CSnippet) + " snippets"); pnl(takeFirst10(collect(list(CSnippet), 'title)); } static L parseFact(S s) { L tok = map toLowerIfIdentifier(javaTokWithAngleBracketsC(/*dropPunctuation*/(s))); ret nempties(translateUsingWordTreeC(tok, phrasesWordTree)); } svoid assertFact(S fact) { if (contains(facts, parseFact(fact))) print("OK fact: " + fact); else print("FAILED fact assert: " + fact); } svoid assertAnswer(S q, S a) { Prolog p = new(prolog); //p.showStuff = true; p.code(dontPrint("to prolog: ", squareBracket(toProlog(q)))); L out = p.rewrite(); //print("Got " + l(out) + " statement(s)"); L lines = map nlUnparse(out); L answers = startingWithEndingWith_drop(lines, "answer is [", "]"); if (l(answers) == 1 && eqic(first(answers), a)) print("OK answer: " + q + " -> " + a); else { pnl(map nlUnparse(out)); print("FAIL answer: " + q + " -> " + first(answers) + " - expected: " + a); } } sS addPhrase(S s) { wordTreeAdd(phrasesWordTree, javaTokC(s), s); ret s; } svoid addFact(S s) { L tok = parseFact(s); facts.add(tok); prolog.code(dontPrint("to prolog: ", squareBracket(factToProlog(tok)))); } svoid assertParse(L args) { args = unquoteAll(args); S s = first(args); L parsed = parseFact(s); L expected = dropFirst(args); if (eq(parsed, expected)) print("OK parse: " + s + " -> " + sfu(parsed)); else print("FAILED parse: " + s + " -> " + sfu(parsed) + " - expected: " + sfu(expected)); } sS toProlog(S s) { L tok = parseFact(upperCaseVarsToDollarVars(s)); ret factToProlog(tok); } sS factToProlog(L tok) { ret joinWithSpace(map tokenToProlog(tok)); } sS tokenToProlog(S t) { if (!isQuoted(t) && containsSpace(t)) ret squareBracket(t); ret t; }