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

84
LINES

< > BotCompany Repo | #1000468 // LOneWordChanged (learner)

JavaX fragment (include)

static class LOneWordChanged extends LearnerImpl {
  static boolean debug;
  Function preprocessor; // e.g. JavaTok
  List<String> lastIn; // only while learning
  String lastOut; // only while learning
  int index;
  String outPattern;

  *(Function *preprocessor) {}
  
  public void processInOut(Object _in, Object _out) {
    if (debug)
      System.out.println("LOneWordChanged: i/o " + _in + " " + _out);
    String in = (String) _in, out = (String) _out;
    List<String> l = (List<String>) preprocessor.process(_in);
    lastIn = l;
    lastOut = out;
  }
  
  public Object processIn(Object _in) {
    if (debug)
      System.out.println("LOneWordChanged: i " + _in);
    List<String> l = (List<String>) preprocessor.process(_in);
    
    if (outPattern == null) {
      if (l.size() != lastIn.size()) fail();
      for (int i = 0; i < l.size(); i++)
        if (!l.get(i).equals(lastIn.get(i))) {
          index = i;
          String word = lastIn.get(index);
          outPattern = lastOut.replace(word, "{*}");
          
          // delete learning data
          lastIn = null;
          lastOut = null;
          break;
        }
        
      if (outPattern == null) {
        if (debug)
          System.out.println("LOneWordChanged: last=" + lastIn + ", this=" + l);
        fail("not applicable - all words identical");
      }
    }
    if (debug)
      System.out.println("LOneWordChanged: index=" + index + ", outPattern=" + quote(outPattern));

    String word = l.get(index);
    return outPattern.replace("{*}", word);
  }
  
  static List<Integer> getFindMarkers2(String s) {
    List<Integer> l = new ArrayList<Integer>();
    int i = 0;
    while (i < s.length()) {
      int j = s.indexOf("[[", i);
      if (j < 0) break;
      int k = s.indexOf("]]", j+2);
      if (k < 0) break;
      l.add(j);
      l.add(k+2);
      i = k+2;
    }
    return l;
  }
  
  static List<String> getMarked(String s) {
    List<Integer> l = getFindMarkers2(s);
    new List<String> result;
    for (int i = 0; i < l.size(); i += 2)
      result.add(s.substring(l.get(i)+2, l.get(i+1)-2));
    return result;
  }
  
  public void toJava(Code code) {
    preprocessor.toJava_process(code);
    String word = code.list() + ".get(" + index + ")";
    if (outPattern.equals("{*}"))
      code.assign(word);
    else
      code.assign(javaQuote(outPattern) + ".replace(" + javaQuote("{*}") + ", (String) (" + word + "))");
  }
}

Author comment

Began life as a copy of #1000465

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

2 comment(s) hidden. show

Snippet ID: #1000468
Snippet name: LOneWordChanged (learner)
Eternal ID of this version: #1000468/1
Text MD5: 410fa431d4124c533c3273cf6b98e5ca
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-21 21:42:10
Source code size: 2523 bytes / 84 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1027 / 2126
Referenced in: [show references]