!7 sS data = [[ ---- Rules // Some reasonings that everybody will understand. // Sorry for the curly braces, we have to help out the parser a tiny bit. // First, the 3 different cases of what "pizza with..." can mean. I ate pizza with pepperoni. => {I ate pizza} and {the pizza had pepperoni on it}. I ate pizza with Bob. => {I ate pizza} and {Bob was with me}. I ate pizza with a fork. => I used a fork to eat pizza. // Now some more easy rules. I used a fork to eat pizza. => I used a fork. I used a fork. => A fork is a tool. The pizza had pepperoni on it. => Pepperoni is edible. Bob was with me. => Bob is a person. // Some VERY basic mathematical logic $A and $B. => $A. $A and $B. => $B. // Tell the machine what is not plausible Mom is edible. => fail Mom is a tool. => fail anchovis are a tool. => fail anchovis are a person. => fail ducks are a tool. => fail my hands are edible. => fail my hands are a person. => fail // That's it! Now for the input. ---- Input I ate pizza with mom. I ate pizza with anchovis. I ate pizza with ducks. I ate pizza with my hands. ]]; static int minScore = 50; sbool printMappings, printGroupedData, printRejectedVars; static LPair rules; static Map> ruleVariables = new Map; p-exp { centerBigTTConsole(); S data = mapEachLine ai_groupSimplestNounPhrases(main.data); if (printGroupedData) print(data); SS sections = asCIMap(minusSignsSections(data)); LS rulesRaw = ai_splitEntriesWithMultipleDoubleArrows(splitAtEmptyLines(sections.get("Rules"))); rules = mapNonNulls splitAtDoubleArrow_pair(rulesRaw); for (PairS rule : rules) ruleVariables.put(rule, ai_wordsInBothSidesOfPair_uncurly(rule)); //pnlStruct(ruleVariables); printAsciiHeading("REASONING"); new MultiMap solutions; for (S input : tlft(sections.get("Input"))) { print("\nInput: " + tok_dropCurlyBrackets(input)); temp tempIndent(); LPair interpretations = interpretations(input); for (Pair in : interpretations) { print(); printInterpretations(ll(in), "Interpretation: "); Set out = litciset(); interpretRecursively(in.a, 5, false, out); if (!matchAny("fail", out)) solutions.put(input, in.a); } } printAsciiHeading("RESULTS"); print(formatDoubleArrowPairs_horizontallyAligned(mapPairBoth formatForUser(multiMapToPairs(solutions)))); } svoid interpretRecursively(S input, int level, bool newLines, Set out) { if (level <= 0) ret; input = ai_groupSimplestNounPhrases(input); L> interpretations = interpretations(input); addAll(out, pairsA(interpretations)); temp tempIndent(); for (Pair in : interpretations) { if (newLines) print(); printInterpretations(ll(in), newLines ? "Interpretation: " : " => "); S corrected = ai_superSimpleVerbCorrector(in.a); interpretRecursively(in.a, level-1, false, out); if (neq(corrected, in.a)) interpretRecursively(corrected, level-1, false, out); } } svoid printInterpretations(LPair interpretations, S prefix) { for (Pair p : interpretations) print(prefix + formatForUser(p.a)); } static LS defaultParse(S s) { ret codeTokens_lazy_uncurly(javaTokWithBrackets_cached(s)); } static LPair interpretations(S input) { new MultiSet ms; LS tokI = defaultParse(input); //print("Raw parse: " + tokI); new L> interpretations; for (PairS rule : rules) { LS tokR = defaultParse(rule.a); final SS map = ciMapWithoutKeysEqualToValues(zipTwoListsToCIMap_strict(tokR, tokI)); if (map == null) continue; // Found matching rule int score = l(tokR)-l(map); L nonVars = withoutDollarVars(listMinusSet(keys(map), ruleVariables.get(rule))); if (printRejectedVars && nempty(nonVars)) print("Rejected vars: " + nonVars); if (nempty(nonVars)) score = 0; ms.add(rule, score+1); if (printMappings) print(" " + reverseMapCI_joinDuplicatesWithPlus(map) + " | " + rule.a); // Make consequence S c = rule.b; c = join(mapCodeTokens(javaTok(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) })); // TODO: multiple grouping levels c = join(mapCodeTokens(javaTokWithBrackets(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) })); int percent = ratioToIntPercent(score, l(tokR)); if (percent >= minScore) addPair(interpretations, c, percent); } ret sortByPairBDesc(interpretations); } sS formatForUser(S s) { ret ai_superSimpleVerbCorrector(tok_dropCurlyBrackets(s)); }