sclass RuleEngine1 { int minScore = 50; // minimal percentage score for rule matching LPair rules; bool printMappings, printGroupedData, printRejectedVars; Map> ruleVariables = new Map; O preprocess = f id; // f ai_groupSimplestNounPhrases *() {} *(S _rules) { addRules(_rules); } void addRules(S _rules) { _rules = mapEachLine(preprocess, _rules); if (printGroupedData) print(_rules); rules = ai_findDoubleArrowRulesAsPairs(_rules); for (PairS rule : rules) ruleVariables.put(rule, ai_wordsInBothSidesOfPair_uncurly(rule)); } void processInput(S input) { print("\nInput: " + tok_dropCurlyBrackets(input)); temp tempIndent(); for (S in : interpretations(input)) { print("\nInterpretation: " + formatForUser(in)); Set out = litciset(); interpretRecursively(in, 5, out); } } void interpretRecursively(S input, int level, Set out) { if (level <= 0) ret; LS interpretations = interpretations((S) callF(preprocess, 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); } } LS defaultParse(S s) { ret codeTokens_lazy_uncurly(javaTokNPunctuationWithBrackets_cached(s)); } LS interpretations(S input) { LS tokI = defaultParse(input); //print("Raw parse: " + tokI); new LS interpretations; for (PairS rule : rules) { LS tokR = defaultParse(rule.a); final SS map = zipTwoListsToCIMap_strict_withoutEquals(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; int percentScore = ratioToIntPercent(score, l(tokR)); if (printMappings) print(" " + percentScore + "% " + reverseMapCI_joinDuplicatesWithPlus(map) + " | " + rule.a); if (percentScore < minScore) continue; // 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); interpretations.add(c); } ret interpretations; } S formatForUser(S s) { ret ai_superSimpleVerbCorrector(tok_dropCurlyBrackets(s)); } }