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).

!7

/* Input:

  LineByUser("a * is something like *") => Assumption("* is a noun")

Result: 

  ToParse2. LineByUser(QuotedString("a * is something like *")) => Assumption("* is a noun")
  ToParse3. LineByUser("a * is something like *") => Assumption(QuotedString("* is a noun"))
  ToParse4. LineByUser(StarPattern(a * is something like *)) => Assumption("* is a noun")
  ToParse5. LineByUser(StarPattern(a * is something like *)) => Assumption(QuotedString("* is a noun"))
  ToParse6. LineByUser("a * is something like *") => Assumption(StarPattern(* is a noun))
  ToParse7. LineByUser(QuotedString("a * is something like *")) => Assumption(StarPattern(* is a noun))

...which is very good but missing the final combination

  LineByUser(StarPattern("a * is something like *")) => Assumption(StarPattern("* is a noun"))

HOWEVER, in a previous transpilation today, THIS WORKED.

Persisting the whole TransformersOnObjects object would work if we didn't make all these anonymous classes in the main script.
*/ 

srecord ToParse(O text) {}
srecord StarPattern(O text) {}
srecord QuotedString(O text) { toString { ret recordToString_quoted(this); }}
srecord PossibleReplacement(O a, O b) {}

cprint {
  delegate TransformationTrail to TransformersOnObjects.
  transient new TransformersOnObjects too;
  switchable long maxSteps = 10000;
  
  transient F1 starPatternWrapper, quotedStringMaker;

  start-thread {
    too.autoUnpackIterables = false;
    
    S ruleText = [[LineByUser("a * is something like *") => Assumption("* is a noun")]];
    too.add(ToParse(ruleText));
    
    // unpack ToParse
    too.addTransformer(func(ToParse x) { x.text }, func(O o) { ToParse(o) });

    too.addTransformer(f<S, O> splitAtDoubleArrow_pair, f<Pair, O> joinPairWithDoubleArrow);
    too.addTransformer(f<S, O> parseFunctionCall, func(Pair<S, LS> x) { unparseFunctionCall(x) });
    too.addTransformer(f<S, O> unquoteOrNull, f<S, S> quote);
    too.addTransformer(f<Pair, O> pairA, (p, o) -> replacePairA((Pair) p, o));
    too.addTransformer(f<Pair, O> pairB, (p, o) -> replacePairB((Pair) p, o));
    
    // TODO: generalize to more elements. requires a change to
    // how backtransformers are found?
    too.addTransformer(
      f<L, O> first,
      (list, element) -> cloneListReplacingElementAtIndex((L) list, 0, element));
    too.addTransformer(f<L, O> second, (list, element) -> cloneListReplacingElementAtIndex((L) list, 1, element));
    
    // Suggest enclosing a star pattern string with StarPattern()
    too.addTransformer(starPatternWrapper = func(QuotedString s) {
      s.text instanceof S && isStarPattern((S) s.text) ? StarPattern(s.text) : null
    });
    
    too.addTransformer(quotedStringMaker = func(S s) { isQuoted(s) ? QuotedString(unquote(s)) : null },
      // The backtransformer may take a replacement thing
      // (so e.g. an instance of StarPattern). In that case
      // we just put it in verbatim
      func(O o) { o instanceof QuotedString ? quote(o/QuotedString.text) : o });
    
    // Reintegrate replaced star patterns into rule text
    too.addTransformer(func(StarPattern s) {
      O madeFrom = too.getTrailBy(s, starPatternWrapper).argument;
      ret WithProbability(0.9, PossibleReplacement(madeFrom, s));
    });
    
    // Same with QuotedString
    too.addTransformer(func(QuotedString s) {
      O madeFrom = too.getTrailBy(s, quotedStringMaker).argument;
      ret WithProbability(0.8, PossibleReplacement(madeFrom, s));
    });
    
    too.addTransformer(func(PossibleReplacement r) {
      replaceInSource(r.a, r.b);
      null;
    });
      
    stepAllWithStats(too, maxSteps);
    //too.printWithTrails();
    print();
    //too.printTransformers();
    printStuff();

    /*print("\nRestructuring.\n");
    too = restructure_checkForProblems(too);
    printStuff();*/
  }
  
  void printStuff {
    pnl("ToParse", map(instancesOf ToParse(too.getObjects()),
      t -> WithProbability(too.getProbability(t), t.text)));
  }
  
  // replace a in b inside of where a came from
  void replaceInSource(O a, O b) {
    print(formatFunctionCall_struct replaceInSource(a, b));
    
    // Find trails of a
    Cl<TransformationTrail> trails2 = too.getTrails(a);
    
    fOr (TransformationTrail trail2 : trails2) {
      F1 back = trail2.backTransformer; 
      IF2 back2 = too.getBackTransformer(trail2.transformer);
      print("  Path back: " + trail2);
      if (back == null && back2 == null) print("NO BACK TRANSFORMER for " + trail2.transformer); else {
        // Backtransform b (if possible) and put in pool
        O c = back != null
          ? callFIfActuallyCallable(back, b)
          : callFIfActuallyCallable(back2, trail2.argument, b);
        if (c == null) continue;
        //print("  Made: " + sfu(c));
        print("  Transformed " + sfu(trail2.argument) + " into " + sfu(c));
        too.addObject(c, "replaceInSource");
        
        // Follow up with more replacements back in the creation chain of a
        too.addObject(PossibleReplacement(trail2.argument, c), "replaceInSource");
      }
    }
  }
}

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: 470 / 1429
Version history: 110 change(s)
Referenced in: #1028472 - Older version of #1028445, not fully working either (working transpilation is probably older than this)