!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 ducks are a person. => 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; // minimal percentage score for rule matching 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)); rules = ai_findDoubleArrowRulesAsPairs(sections.get("Rules")); for (PairS rule : rules) ruleVariables.put(rule, ai_wordsInBothSidesOfPair_uncurly(rule)); printAsciiHeading("REASONING"); new MultiMap solutions; for (S input : tlft(sections.get("Input"))) { print("\nInput: " + tok_dropCurlyBrackets(input)); temp tempIndent(); for (Pair in : interpretations(input)) { print("\nInterpretation: " + formatForUser(in.a)); Set out = litciset(); interpretRecursively(in.a, 5, 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, Set out) { if (level <= 0) ret; LS interpretations = pairsA(interpretations(ai_groupSimplestNounPhrases(input))); temp tempIndent(); for (S in : addAllAndReturnNew(out, interpretations)) { print(" => " + formatForUser(in)); for (S s : lithashset(in, ai_superSimpleVerbCorrector(in))) interpretRecursively(s, level-1, out); } } static LS defaultParse(S s) { ret codeTokens_lazy_uncurly(javaTokWithBrackets_cached(s)); } static LPair interpretations(S input) { LS tokI = defaultParse(input); //print("Raw parse: " + tokI); new LPair 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; 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) })); c = join(mapCodeTokens(javaTokWithBrackets(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) })); //c = javaTokWithBrackets_recursiveTransform(func(S s) -> S { getOrKeep(map, s) }, c); addPairIfBAtLeast(interpretations, c, ratioToIntPercent(score, l(tokR)), minScore); } ret interpretations; } sS formatForUser(S s) { ret ai_superSimpleVerbCorrector(tok_dropCurlyBrackets(s)); }