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

51
LINES

< > BotCompany Repo | #1000492 // LPrefixSuffix (learner)

JavaX fragment (include)

  // learns to exchange common prefixes and suffixes.
  // maybe it's not perfect... see #1000491 for a problem case
  
  static class LPrefixSuffix extends LearnerImpl {
    static boolean debug;
    String prefixIn, suffixIn, prefixOut, suffixOut;
    
    public void processInOut(Object _in, Object _out) {
      String in = (String) _in, out = (String) _out;
      updateIn(in);
      prefixOut = prefixOut == null ? out : commonPrefix(prefixOut, out);
      suffixOut = suffixOut == null ? out : commonSuffix(suffixOut, out);
      if (debug)
        printState("processInOut(" + quote(in) + ", " + quote(out) + ")");
    }
    
    void updateIn(String in) {
      prefixIn = prefixIn == null ? in : commonPrefix(prefixIn, in);
      suffixIn = suffixIn == null ? in : commonSuffix(suffixIn, in);
      if (debug)
        printState("updateIn(" + quote(in) + ")");
    }

    public String processIn(Object _in) {
      String in = (String) _in;
      //System.out.println("[before last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut));
      //System.out.println("[last info] " + quote(in));
      
      // use latest information
      String p = prefixIn, s = suffixIn;
      updateIn(in);
      prefixOut = prefixOut.substring(0, prefixOut.length()-(p.length()-prefixIn.length()));
      suffixOut = suffixOut.substring(s.length()-suffixIn.length());
      
      //System.out.println("[after last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut));
      String core = in.substring(prefixIn.length(), in.length()-suffixIn.length());
      return prefixOut + core + suffixOut;
    }
    
    public void toJava(Code code) {
      if (prefixIn.length() != 0 || suffixIn.length() != 0)
        code.line(code.var + " = ((String) " + code.var + ").substring(" + prefixIn.length() + ", " + code.s() + ".length()-" + suffixIn.length() + ");");
      if (prefixOut.length() != 0 || suffixOut.length() != 0)
        code.line(code.var + " = " + quote(prefixOut) + " + " + code.var + " + " + quote(suffixOut) + ";");
    }
    
    void printState(String text) {
      System.out.println(text);
      printVars();
    }
  }
  

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000492
Snippet name: LPrefixSuffix (learner)
Eternal ID of this version: #1000492/1
Text MD5: 9f08a357926204438d2dd3cbc5ccf057
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-09 15:02:59
Source code size: 2284 bytes / 51 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 506 / 1440
Referenced in: [show references]