Uses 911K of libraries. Click here for Pure Java version (13988L/72K).
1 | !7 |
2 | |
3 | !include once #1027167 // Compact Module Include 2 v2 |
4 | |
5 | set flag AllPublic. |
6 | |
7 | module WinogradSolver1 > DynSingleFunctionWithPrintLog { |
8 | transient PhilosophyBot1 engine; |
9 | transient O multiStage; // unused |
10 | |
11 | S prelude = "import bool"; |
12 | |
13 | S input = "Bob paid for Charlie's college education. He is very [generous/grateful]. Who is [generous/grateful]?"; |
14 | |
15 | Map<S, SS> answers = syncMap(); |
16 | |
17 | delegate LogicRule to PhilosophyBot1. |
18 | replace GroupingAndPatterns with Pair<S, Set<S>>. |
19 | |
20 | void doIt() { |
21 | // Stage 3a: Select the right rules for the task |
22 | Cl<S> rulesToUse = dm_getUnclearList("Rules"); // take all for now |
23 | LS patternsToUse = dm_getUnclearList("Patterns"); |
24 | |
25 | doIt(rulesToUse, patternsToUse); |
26 | } |
27 | |
28 | void doIt(Cl<S> rulesToUse, LS patternsToUse) { |
29 | clear(answers); change(); |
30 | if (empty(input)) ret; |
31 | |
32 | print("Using patterns: " + sfu(patternsToUse)); |
33 | print("Using rules: " + sfu(rulesToUse)); |
34 | |
35 | // Stage 1: take one option out of square brackets |
36 | for (S variant : winograd_variants(input)) { |
37 | print(variant); |
38 | SS _answers = ciMap(); |
39 | dm_call(multiStage, 'addStage, "Variant", variant); |
40 | |
41 | // Stage 1b: Split off 's |
42 | S input2b = ai_splitOffApostropheS(variant); |
43 | dm_call(multiStage, 'addStage, "Split off 's", input2b); |
44 | |
45 | // Stage 1c: Split into sentences |
46 | LS sentences = map dropQuestionMark(sentences(input2b)); |
47 | dm_call(multiStage, 'addStage, "Split into sentences", sentences); |
48 | |
49 | // Stage 2: Parser |
50 | S parser = dm_makeModule("#1027108/TopDownParsing"); |
51 | //L<virtual TryPatterns> parsed = map(sentences, s -> dm_call(parser, 'parse, s)); |
52 | L<Cl<GroupingAndPatterns>> groupingsWithPatterns = map(sentences, |
53 | s -> (Cl<GroupingAndPatterns>) dm_callWithImport(parser, 'parseToGroupingsWithUsedPatterns, s, |
54 | patterns := patternsToUse)); |
55 | L<GroupingAndPatterns> groupingsWithPatterns2 = concatLists(groupingsWithPatterns); |
56 | Map<S, Set<S>> groupingToUsedPatterns = pairsToMap(groupingsWithPatterns2); |
57 | |
58 | //L<Cl<S>> parsed = map(sentences, s -> (Cl<S>) dm_call(parser, 'parseToGroupings, s)); |
59 | //pnl(parsed); |
60 | Set<S> allGroupings = toCISet(pairsA(groupingsWithPatterns2)); |
61 | print("Have " + n2(allGroupings, "grouping")); |
62 | //dm_call(multiStage, 'addStage, "Parser", parsed); |
63 | |
64 | // Stage 3b: Run logic engine |
65 | S program = joinWithEmptyLines(concatLists(ll(prelude), allGroupings, rulesToUse)); |
66 | engine = PhilosophyBot1(program); |
67 | engine.standardImports(); |
68 | engine.enableTrails(); |
69 | engine.parseProgram(); |
70 | for (S s : allGroupings) { |
71 | //print("Setting trail for " + quote(s)); |
72 | engine.setTrail(s, "grouped original input"); |
73 | } |
74 | engine.printFactsAfterThinking = false; |
75 | L<LogicRule> initialRules = engine.logicRules(); |
76 | engine.think(); |
77 | engine.printDeducedFactsWithTrails(); |
78 | |
79 | Set<S> allOriginalGroupings = cloneSet(allGroupings); |
80 | |
81 | // gather simplified versions of questions so we find their answers too |
82 | LPairS simplifications = philosophyBot1_allSimplifications_gen(engine); |
83 | for (PairS p : simplifications) |
84 | if (allGroupings.contains(p.a)) allGroupings.add(p.b); |
85 | |
86 | for (Pair<S, SS> p : engine.matchFacts2("the answer to $x is $y")) { |
87 | S fact = p.a; |
88 | SS map = p.b; |
89 | if (allGroupings.contains($x(map))) { |
90 | printAfterNewLine("ANSWER>> " + $x(map) + ": " + $y(map)); |
91 | //print("Trail: " + engine.getTrail(fact)); |
92 | //print("Question trail: " + engine.getTrail($x(map))); |
93 | |
94 | Set set = engine.thingsUsedForMaking(fact); |
95 | Set<S> strings = asCISet(stringsOnly(set)); |
96 | Set<LogicRule> sourceRules = setIntersection(set, initialRules); |
97 | Set<S> usedRules = setIntersection(strings, rulesToUse); |
98 | strings = setMinusSet(strings, usedRules); |
99 | Set<S> usedGroupings = setIntersection(strings, allOriginalGroupings); |
100 | Set<S> remainingStrings = setMinusSet(strings, allGroupings); |
101 | |
102 | Set<S> usedPatterns = joinCISets(map(usedGroupings, s -> groupingToUsedPatterns.get(s))); |
103 | LS usedRules_prettier = map(usedRules, s -> replacePrefixIC("add simplifier ", "simplifier ", s)); |
104 | |
105 | /*for (O o : sourceRules) { |
106 | print(" RULE >> " + o); |
107 | print(" Trail: " + engine.getTrail(o)); |
108 | } |
109 | for (O o : usedRules) { |
110 | print(" FACT >> " + o); |
111 | print(" Trail: " + engine.getTrail(o)); |
112 | }*/ |
113 | pnl(" PATTERN >> ", usedPatterns); |
114 | pnl(" GROUPED INPUT >> ", usedGroupings); |
115 | pnl(" TOOL >> ", usedRules_prettier); |
116 | _answers.put($x(map), $y(map)); |
117 | } |
118 | } |
119 | answers.put(variant, _answers); change(); |
120 | print(); |
121 | } |
122 | } |
123 | |
124 | visual northCenterAndSouthWithMargins( |
125 | jCenteredSection("Input", dm_centeredTextField input()), |
126 | super, |
127 | jCenteredSection("Answers", |
128 | //dm_calculatedTextArea(() -> pnlToString(answers))) |
129 | dm_calculatedCenteredLabel(() -> joinWithSlash( |
130 | map(values(answers), map -> joinWithComma(values(map))))) |
131 | )); |
132 | |
133 | start { |
134 | dm_registerAs winogradSolver(); |
135 | } |
136 | |
137 | // API |
138 | |
139 | O getTrailForFact(S fact) { |
140 | ret engine == null ? null : engine.getTrail(fact); |
141 | } |
142 | } |
download show line numbers debug dex old transpilations
Travelled to 8 computer(s): bhatertpkbcr, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1027165 |
Snippet name: | Winograd Solver [dev.] |
Eternal ID of this version: | #1027165/80 |
Text MD5: | 3ddbfe5f12c07a7e04e9852e6932b3d5 |
Transpilation MD5: | 66e546022db979d3ff1db82d65995295 |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-02-24 22:48:22 |
Source code size: | 5407 bytes / 142 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 317 / 1674 |
Version history: | 79 change(s) |
Referenced in: | [show references] |