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

129
LINES

< > BotCompany Repo | #1028445 // Transformers Spike 3, parsing a rule [almost OK]

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

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

1  
!7
2  
3  
/* Input:
4  
5  
  LineByUser("a * is something like *") => Assumption("* is a noun")
6  
7  
Result: 
8  
9  
  ToParse2. LineByUser(QuotedString("a * is something like *")) => Assumption("* is a noun")
10  
  ToParse3. LineByUser("a * is something like *") => Assumption(QuotedString("* is a noun"))
11  
  ToParse4. LineByUser(StarPattern(a * is something like *)) => Assumption("* is a noun")
12  
  ToParse5. LineByUser(StarPattern(a * is something like *)) => Assumption(QuotedString("* is a noun"))
13  
  ToParse6. LineByUser("a * is something like *") => Assumption(StarPattern(* is a noun))
14  
  ToParse7. LineByUser(QuotedString("a * is something like *")) => Assumption(StarPattern(* is a noun))
15  
16  
...which is very good but missing the final combination
17  
18  
  LineByUser(StarPattern("a * is something like *")) => Assumption(StarPattern("* is a noun"))
19  
20  
HOWEVER, in a previous transpilation today, THIS WORKED.
21  
22  
Persisting the whole TransformersOnObjects object would work if we didn't make all these anonymous classes in the main script.
23  
*/ 
24  
25  
srecord ToParse(O text) {}
26  
srecord StarPattern(O text) {}
27  
srecord QuotedString(O text) { toString { ret recordToString_quoted(this); }}
28  
srecord PossibleReplacement(O a, O b) {}
29  
30  
cprint {
31  
  delegate TransformationTrail to TransformersOnObjects.
32  
  transient new TransformersOnObjects too;
33  
  switchable long maxSteps = 10000;
34  
  
35  
  transient F1 starPatternWrapper, quotedStringMaker;
36  
37  
  start-thread {
38  
    too.autoUnpackIterables = false;
39  
    
40  
    S ruleText = [[LineByUser("a * is something like *") => Assumption("* is a noun")]];
41  
    too.add(ToParse(ruleText));
42  
    
43  
    // unpack ToParse
44  
    too.addTransformer(func(ToParse x) { x.text }, func(O o) { ToParse(o) });
45  
46  
    too.addTransformer(f<S, O> splitAtDoubleArrow_pair, f<Pair, O> joinPairWithDoubleArrow);
47  
    too.addTransformer(f<S, O> parseFunctionCall, func(Pair<S, LS> x) { unparseFunctionCall(x) });
48  
    too.addTransformer(f<S, O> unquoteOrNull, f<S, S> quote);
49  
    too.addTransformer(f<Pair, O> pairA, (p, o) -> replacePairA((Pair) p, o));
50  
    too.addTransformer(f<Pair, O> pairB, (p, o) -> replacePairB((Pair) p, o));
51  
    
52  
    // TODO: generalize to more elements. requires a change to
53  
    // how backtransformers are found?
54  
    too.addTransformer(
55  
      f<L, O> first,
56  
      (list, element) -> cloneListReplacingElementAtIndex((L) list, 0, element));
57  
    too.addTransformer(f<L, O> second, (list, element) -> cloneListReplacingElementAtIndex((L) list, 1, element));
58  
    
59  
    // Suggest enclosing a star pattern string with StarPattern()
60  
    too.addTransformer(starPatternWrapper = func(QuotedString s) {
61  
      s.text instanceof S && isStarPattern((S) s.text) ? StarPattern(s.text) : null
62  
    });
63  
    
64  
    too.addTransformer(quotedStringMaker = func(S s) { isQuoted(s) ? QuotedString(unquote(s)) : null },
65  
      // The backtransformer may take a replacement thing
66  
      // (so e.g. an instance of StarPattern). In that case
67  
      // we just put it in verbatim
68  
      func(O o) { o instanceof QuotedString ? quote(o/QuotedString.text) : o });
69  
    
70  
    // Reintegrate replaced star patterns into rule text
71  
    too.addTransformer(func(StarPattern s) {
72  
      O madeFrom = too.getTrailBy(s, starPatternWrapper).argument;
73  
      ret WithProbability(0.9, PossibleReplacement(madeFrom, s));
74  
    });
75  
    
76  
    // Same with QuotedString
77  
    too.addTransformer(func(QuotedString s) {
78  
      O madeFrom = too.getTrailBy(s, quotedStringMaker).argument;
79  
      ret WithProbability(0.8, PossibleReplacement(madeFrom, s));
80  
    });
81  
    
82  
    too.addTransformer(func(PossibleReplacement r) {
83  
      replaceInSource(r.a, r.b);
84  
      null;
85  
    });
86  
      
87  
    stepAllWithStats(too, maxSteps);
88  
    //too.printWithTrails();
89  
    print();
90  
    //too.printTransformers();
91  
    printStuff();
92  
93  
    /*print("\nRestructuring.\n");
94  
    too = restructure_checkForProblems(too);
95  
    printStuff();*/
96  
  }
97  
  
98  
  void printStuff {
99  
    pnl("ToParse", map(instancesOf ToParse(too.getObjects()),
100  
      t -> WithProbability(too.getProbability(t), t.text)));
101  
  }
102  
  
103  
  // replace a in b inside of where a came from
104  
  void replaceInSource(O a, O b) {
105  
    print(formatFunctionCall_struct replaceInSource(a, b));
106  
    
107  
    // Find trails of a
108  
    Cl<TransformationTrail> trails2 = too.getTrails(a);
109  
    
110  
    fOr (TransformationTrail trail2 : trails2) {
111  
      F1 back = trail2.backTransformer; 
112  
      IF2 back2 = too.getBackTransformer(trail2.transformer);
113  
      print("  Path back: " + trail2);
114  
      if (back == null && back2 == null) print("NO BACK TRANSFORMER for " + trail2.transformer); else {
115  
        // Backtransform b (if possible) and put in pool
116  
        O c = back != null
117  
          ? callFIfActuallyCallable(back, b)
118  
          : callFIfActuallyCallable(back2, trail2.argument, b);
119  
        if (c == null) continue;
120  
        //print("  Made: " + sfu(c));
121  
        print("  Transformed " + sfu(trail2.argument) + " into " + sfu(c));
122  
        too.addObject(c, "replaceInSource");
123  
        
124  
        // Follow up with more replacements back in the creation chain of a
125  
        too.addObject(PossibleReplacement(trail2.argument, c), "replaceInSource");
126  
      }
127  
    }
128  
  }
129  
}

Author comment

Began life as a copy of #1028395

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1028445
Snippet name: Transformers Spike 3, parsing a rule [almost OK]
Eternal ID of this version: #1028445/111
Text MD5: 680f670be82985407eb6f3f3976e96c1
Transpilation MD5: b66ecad4be3dc0d480004c8ee52e95eb
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-06-21 01:29:37
Source code size: 5218 bytes / 129 lines
Pitched / IR pitched: No / No
Views / Downloads: 471 / 1431
Version history: 110 change(s)
Referenced in: [show references]