Libraryless. Click here for Pure Java version (12914L/92K).
1 | // used e.g. to solve the "Pizza with..." example |
2 | sclass Gazelle_MiniEngine1 { |
3 | int minScore = 50; // minimal percentage score for rule matching |
4 | |
5 | bool printMappings, printGroupedData, printRejectedVars; // what to debug-print |
6 | |
7 | // some parsing and formatting macros |
8 | |
9 | F1<S> formatForUser = func(S s) -> S { ai_superSimpleVerbCorrector(tok_dropCurlyBrackets(s)) }; |
10 | |
11 | F1<S, LS> parse = func(S s) -> LS { codeTokens_lazy_uncurly(javaTokWithBrackets_cached(s)) }; |
12 | |
13 | // syntactic sugar for the macros |
14 | |
15 | S formatForUser(S s) { ret callF(formatForUser, s); } |
16 | LS parse(S s) { ret callF(parse, s); } |
17 | |
18 | // rules are in => out + variables |
19 | |
20 | LPair<S> rules; |
21 | Map<PairS, Set<S>> ruleVariables = new Map; |
22 | |
23 | // returns solutions |
24 | // data = section "rules" and section "input" (input statements) |
25 | MultiMap<S> run(S _data) { |
26 | // parse rules from _data |
27 | |
28 | S data = mapEachLine ai_groupSimplestNounPhrases(_data); |
29 | if (printGroupedData) print(data); |
30 | SS sections = asCIMap(minusSignsSections(data)); |
31 | rules = ai_findDoubleArrowRulesAsPairs(sections.get("Rules")); |
32 | for (PairS rule : rules) |
33 | ruleVariables.put(rule, ai_wordsInBothSidesOfPair_uncurly(rule)); |
34 | |
35 | // process input statements from _data and record rewrites that don't end in "fail" |
36 | |
37 | printAsciiHeading("REASONING"); |
38 | new MultiMap<S> solutions; |
39 | |
40 | for (S input : tlft(sections.get("Input"))) { |
41 | print("\nInput: " + tok_dropCurlyBrackets(input)); |
42 | temp tempIndent(); |
43 | for (Pair<S, Int> in : interpretations(input)) { |
44 | print("\nInterpretation: " + formatForUser(in.a)); |
45 | Set<S> out = litciset(); |
46 | interpretRecursively(in.a, 5, out); |
47 | if (!matchAny("fail", out)) |
48 | solutions.put(input, in.a); |
49 | } |
50 | } |
51 | |
52 | // print solutions |
53 | |
54 | printAsciiHeading("RESULTS"); |
55 | print(formatDoubleArrowPairs_horizontallyAligned(mapPairBoth(formatForUser, multiMapToPairs(solutions)))); |
56 | ret solutions; |
57 | } |
58 | |
59 | void interpretRecursively(S input, int level, Set<S> out) { |
60 | if (level <= 0) ret; |
61 | LS interpretations = pairsA(interpretations(ai_groupSimplestNounPhrases(input))); |
62 | temp tempIndent(); |
63 | for (S in : addAllAndReturnNew(out, interpretations)) { |
64 | print(" => " + formatForUser(in)); |
65 | for (S s : lithashset(in, ai_superSimpleVerbCorrector(in))) |
66 | interpretRecursively(s, level-1, out); |
67 | } |
68 | } |
69 | |
70 | LPair<S, Int> interpretations(S input) { |
71 | LS tokI = parse(input); |
72 | new LPair<S, Int> interpretations; |
73 | |
74 | for (PairS rule : rules) { |
75 | LS tokR = parse(rule.a); |
76 | final SS map = ciMapWithoutKeysEqualToValues(zipTwoListsToCIMap_strict(tokR, tokI)); |
77 | if (map == null) continue; |
78 | |
79 | // Found matching rule |
80 | |
81 | int score = l(tokR)-l(map); |
82 | L<S> nonVars = withoutDollarVars(listMinusSet(keys(map), ruleVariables.get(rule))); |
83 | if (printRejectedVars && nempty(nonVars)) print("Rejected vars: " + nonVars); |
84 | if (nempty(nonVars)) score = 0; |
85 | if (printMappings) print(" " + reverseMapCI_joinDuplicatesWithPlus(map) + " | " + rule.a); |
86 | |
87 | // Make consequence |
88 | |
89 | S c = rule.b; |
90 | c = join(mapCodeTokens(javaTok(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) })); |
91 | c = join(mapCodeTokens(javaTokWithBrackets(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) })); |
92 | //c = javaTokWithBrackets_recursiveTransform(func(S s) -> S { getOrKeep(map, s) }, c); |
93 | |
94 | addPairIfBAtLeast(interpretations, c, ratioToIntPercent(score, l(tokR)), minScore); |
95 | } |
96 | |
97 | ret interpretations; |
98 | } |
99 | } |
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: | 291 / 856 |
Version history: | 1 change(s) |
Referenced in: | [show references] |