Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

142
LINES

< > BotCompany Repo | #1025404 // Editing the MiniEngine [dev.]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (13991L/78K).

1  
!7
2  
3  
cmodule Timeline > DynPrintLog {
4  
  transient S input = [[
5  
    --- Rules
6  
7  
    Event 1 happens before event 2 :=
8  
      vars {event 1, event 2, X, Y}
9  
      Event 1 happens at day X.
10  
      Event 2 happens at day Y.
11  
      X < Y.
12  
      
13  
    --- Facts
14  
15  
    Enrolling happens before graduation.
16  
    Enrolling happens at day 200.
17  
    Graduation happens at day 100. // Contradiction!
18  
  ]];
19  
  
20  
  transient new L<Rule_StringToStrings> rules;
21  
22  
  start-thread {
23  
    SS sections = minusSignSectionsCI(input);
24  
    for (S s : splitAtEmptyLines(sections.get("rules"))) {
25  
      LS lines = tlft(s);
26  
      S head = dropSuffix_trim(":=", popFirst(lines));
27  
      Rule_StringToStrings rule = new(head);
28  
      new Matches m;
29  
      if (match3_plusBrackets("vars * ", first(lines), m)) {
30  
        rule.vars = asCISet(tok_splitAtComma(uncurly(m.get(0))));
31  
        popFirst(lines);
32  
      }
33  
      rule.out = lines;
34  
      print("Got rule: " + rule);
35  
      printStruct(rule);
36  
      rules.add(rule);
37  
    }
38  
    
39  
    for (S fact : tlft(dropJavaComments(sections.get("facts")))) {
40  
      print("Got fact: " + fact);
41  
    }
42  
  }
43  
}
44  
45  
sclass Gazelle_MiniEngine2 {
46  
  int minScore = 50; // minimal percentage score for rule matching
47  
  
48  
  bool printMappings, printGroupedData, printRejectedVars; // what to debug-print
49  
50  
  // some parsing and formatting macros
51  
  
52  
  F1<S> formatForUser = func(S s) -> S { ai_superSimpleVerbCorrector(tok_dropCurlyBrackets(s)) };
53  
  
54  
  F1<S, LS> parse = func(S s) -> LS { codeTokens_lazy_uncurly(javaTokWithBrackets_cached(s)) };
55  
  
56  
  // syntactic sugar for the macros
57  
  
58  
  S formatForUser(S s) { ret callF(formatForUser, s); }
59  
  LS parse(S s) { ret callF(parse, s); }
60  
  
61  
  // rules are in => out + variables
62  
63  
  LPair<S> rules;
64  
  Map<PairS, Set<S>> ruleVariables = new Map;
65  
  
66  
  // returns solutions
67  
  // data = section "rules" and section "input" (input statements)
68  
  MultiMap<S> run(S _data) {
69  
    // parse rules from _data
70  
    
71  
    S data = mapEachLine ai_groupSimplestNounPhrases(_data);
72  
    if (printGroupedData) print(data);
73  
    SS sections = asCIMap(minusSignsSections(data));
74  
    rules = ai_findDoubleArrowRulesAsPairs(sections.get("Rules"));
75  
    for (PairS rule : rules)
76  
      ruleVariables.put(rule, ai_wordsInBothSidesOfPair_uncurly(rule));
77  
      
78  
    // process input statements from _data and record rewrites that don't end in "fail"
79  
  
80  
    printAsciiHeading("REASONING");
81  
    new MultiMap<S> solutions;
82  
  
83  
    for (S input : tlft(sections.get("Input"))) {
84  
      print("\nInput: " + tok_dropCurlyBrackets(input));
85  
      temp tempIndent();
86  
      for (Pair<S, Int> in : interpretations(input)) {
87  
        print("\nInterpretation: " + formatForUser(in.a));
88  
        Set<S> out = litciset();
89  
        interpretRecursively(in.a, 5, out);
90  
        if (!matchAny("fail", out))
91  
          solutions.put(input, in.a);
92  
      }
93  
    }
94  
    
95  
    // print solutions
96  
    
97  
    printAsciiHeading("RESULTS");
98  
    print(formatDoubleArrowPairs_horizontallyAligned(mapPairBoth(formatForUser, multiMapToPairs(solutions))));
99  
    ret solutions;
100  
  }
101  
  
102  
  void interpretRecursively(S input, int level, Set<S> out) {
103  
    if (level <= 0) ret;
104  
    LS interpretations = pairsA(interpretations(ai_groupSimplestNounPhrases(input)));
105  
    temp tempIndent();
106  
    for (S in : addAllAndReturnNew(out, interpretations)) {
107  
      print(" => " + formatForUser(in));
108  
      for (S s : lithashset(in, ai_superSimpleVerbCorrector(in)))
109  
        interpretRecursively(s, level-1, out);
110  
    }
111  
  }
112  
  
113  
  LPair<S, Int> interpretations(S input) {
114  
    LS tokI = parse(input);
115  
    new LPair<S, Int> interpretations;
116  
      
117  
    for (PairS rule : rules) {
118  
      LS tokR = parse(rule.a);
119  
      final SS map = ciMapWithoutKeysEqualToValues(zipTwoListsToCIMap_strict(tokR, tokI));
120  
      if (map == null) continue;
121  
      
122  
      // Found matching rule
123  
      
124  
      int score = l(tokR)-l(map);
125  
      L<S> nonVars = withoutDollarVars(listMinusSet(keys(map), ruleVariables.get(rule)));
126  
      if (printRejectedVars && nempty(nonVars)) print("Rejected vars: " + nonVars);
127  
      if (nempty(nonVars)) score = 0;
128  
      if (printMappings) print("  " + reverseMapCI_joinDuplicatesWithPlus(map) + " | " + rule.a);
129  
      
130  
      // Make consequence
131  
      
132  
      S c = rule.b;
133  
      c = join(mapCodeTokens(javaTok(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) }));
134  
      c = join(mapCodeTokens(javaTokWithBrackets(c), func(S s) -> S { getOrKeep_tryUncurlied(map, s) }));
135  
      //c = javaTokWithBrackets_recursiveTransform(func(S s) -> S { getOrKeep(map, s) }, c);
136  
      
137  
      addPairIfBAtLeast(interpretations, c, ratioToIntPercent(score, l(tokR)), minScore);
138  
    }
139  
      
140  
    ret interpretations;
141  
  }
142  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1025404
Snippet name: Editing the MiniEngine [dev.]
Eternal ID of this version: #1025404/6
Text MD5: 80314f6d191aa0a174114b59c0788470
Transpilation MD5: 751fdd159f7ea47fac2ef69b2376b726
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-28 17:38:02
Source code size: 4769 bytes / 142 lines
Pitched / IR pitched: No / No
Views / Downloads: 208 / 579
Version history: 5 change(s)
Referenced in: [show references]