static class LOneWordChanged extends LearnerImpl { static boolean debug; Function preprocessor; // e.g. JavaTok List 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 l = (List) preprocessor.process(_in); lastIn = l; lastOut = out; } public Object processIn(Object _in) { if (debug) System.out.println("LOneWordChanged: i " + _in); List l = (List) 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 getFindMarkers2(String s) { List l = new ArrayList(); 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 getMarked(String s) { List l = getFindMarkers2(s); new List 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 + "))"); } }