Libraryless. Click here for Pure Java version (12914L/92K).
// used e.g. to solve the "Pizza with..." example sclass Gazelle_MiniEngine1 { int minScore = 50; // minimal percentage score for rule matching bool printMappings, printGroupedData, printRejectedVars; // what to debug-print // some parsing and formatting macros F1<S> formatForUser = func(S s) -> S { ai_superSimpleVerbCorrector(tok_dropCurlyBrackets(s)) }; F1<S, LS> parse = func(S s) -> LS { codeTokens_lazy_uncurly(javaTokWithBrackets_cached(s)) }; // syntactic sugar for the macros S formatForUser(S s) { ret callF(formatForUser, s); } LS parse(S s) { ret callF(parse, s); } // rules are in => out + variables LPair<S> rules; Map<PairS, Set<S>> ruleVariables = new Map; // returns solutions // data = section "rules" and section "input" (input statements) MultiMap<S> run(S _data) { // parse rules from _data S data = mapEachLine ai_groupSimplestNounPhrases(_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)); // process input statements from _data and record rewrites that don't end in "fail" printAsciiHeading("REASONING"); new MultiMap<S> solutions; for (S input : tlft(sections.get("Input"))) { print("\nInput: " + tok_dropCurlyBrackets(input)); temp tempIndent(); for (Pair<S, Int> in : interpretations(input)) { print("\nInterpretation: " + formatForUser(in.a)); Set<S> out = litciset(); interpretRecursively(in.a, 5, out); if (!matchAny("fail", out)) solutions.put(input, in.a); } } // print solutions printAsciiHeading("RESULTS"); print(formatDoubleArrowPairs_horizontallyAligned(mapPairBoth(formatForUser, multiMapToPairs(solutions)))); ret solutions; } void interpretRecursively(S input, int level, Set<S> 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); } } LPair<S, Int> interpretations(S input) { LS tokI = parse(input); new LPair<S, Int> interpretations; for (PairS rule : rules) { LS tokR = parse(rule.a); final SS map = ciMapWithoutKeysEqualToValues(zipTwoListsToCIMap_strict(tokR, tokI)); if (map == null) continue; // Found matching rule int score = l(tokR)-l(map); L<S> 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; } }
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1021597 |
Snippet name: | Gazelle_MiniEngine1 |
Eternal ID of this version: | #1021597/2 |
Text MD5: | 1087fb8c8a22b29335459c851304a121 |
Transpilation MD5: | a39590b0c4fb60497978a7eaaa7fe607 |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-09-28 14:45:15 |
Source code size: | 3681 bytes / 99 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 290 / 855 |
Version history: | 1 change(s) |
Referenced in: | [show references] |