!636 !quickmain !auto-import !standard functions interface Learner { public void processInOut(String in, String out); public String processIn(String in); public void toJava(Code code); } class Code { StringBuilder buf = new StringBuilder(); void line(String line) { buf.append(line).append('\n'); } } main { static List fullExamples = new ArrayList(); static List halfExamples = new ArrayList(); static List examples1, examples2; psvm { parse(args.length != 0 ? args[0] : null); calculate(); } static void parse(String arg) tex { String text; if (arg != null) text = loadSnippet(arg); else { text = loadTextFile("input/input.txt", null); if (text == null) text = loadSnippet("#2000455"); // example input } System.out.println(text); String in = null, out = null; for (String line : toLines(text)) { if (line.startsWith("I")) { // "In: " or "I: " if (in != null) halfExamples.add(in); in = unquote(line.substring(line.indexOf(':')+1).trim()); out = null; } else if (line.startsWith("O")) { // "Out: " or "O: " out = unquote(line.substring(line.indexOf(':')+1).trim()); System.out.println(quote(in) + " => " + quote(out)); fullExamples.add(new String[] {in, out}); in = out = null; } } if (in != null) halfExamples.add(in); } static void calculate() tex { if (fullExamples.size() < 2) throw new RuntimeException("Too few examples (" + fullExamples.size() + ")"); int splitPoint = fullExamples.size()-1; System.out.println("Full examples: " + fullExamples.size() + ", splitPoint: " + splitPoint); examples1 = fullExamples.subList(0, splitPoint); examples2 = fullExamples.subList(splitPoint, fullExamples.size()); Learner learner = findOKLearner(); if (learner == null) print "\nProblem not solved" else { print "\nSolved!\n" Code code = new Code(); learner.toJava(code); System.out.println(code.buf); for (String in : halfExamples) { String out = learner.processIn(in); System.out.println(quote(in) + " =>! " + quote(out)); } } } static Learner findOKLearner() { for (Learner learner : makeLearners()) try { if (learnerOK(learner)) return learner; } catch (Throwable e) { e.printStackTrace(); } return null; } static boolean learnerOK(Learner learner) { for (String[] e : examples1) { learner.processInOut(e[0], e[1]); } for (String[] e : fullExamples) { String out = learner.processIn(e[0]); if (!e[1].equals(out)) { System.out.println("[fail] " + learner + " on " + quote(e[0]) + " - " + quote(out) + " vs " + quote(e[1])); return false; } } return true; // all test examples passed } static Iterable makeLearners() { List list = new ArrayList(); //list.add(new LId()); // subsumed by trivial case of PrefixSuffix list.add(new LPrefixSuffix()); list.add(new LSplitInput(new LOutPattern())); list.add(new LInputPattern()); return list; } public static String unquote(String s) { if (s.startsWith("\"") && s.endsWith("\"") && s.length() > 1) return s.substring(1, s.length()-1).replace("\\\"", "\"").replace("\\\\", "\\"); // SHOULD work... else return s; // Return SOMETHING } public static String quote(String s) { if (s == null) return "null"; return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"") + "\""; } // splits the input at some point, takes only one part static class LSplitInput implements Learner { int splitIdx = 1; // split after first character Learner baseLearner; LSplitInput(Learner baseLearner) { this.baseLearner = baseLearner; } public void processInOut(String in, String out) { in = in.substring(splitIdx); baseLearner.processInOut(in, out); } public String processIn(String in) { in = in.substring(splitIdx); return baseLearner.processIn(in); } public void toJava(Code code) { code.line("in = in.substring(" + splitIdx + ");"); baseLearner.toJava(code); } } // if input appears in output in fixed pattern static class LOutPattern implements Learner { String pattern = "%!%"; public void processInOut(String in, String out) { pattern = out.replace(in, "%!%"); } public String processIn(String in) { return pattern.replace("%!%", in); } public void toJava(Code code) { code.line("in = " + quote(pattern) + ".replace(" + quote("%!%") + ", in);"); } } // learns to exchange common prefixes and suffixes static class LPrefixSuffix implements Learner { String prefixIn, suffixIn, prefixOut, suffixOut; public void processInOut(String in, String out) { updateIn(in); prefixOut = prefixOut == null ? out : commonPrefix(prefixOut, out); suffixOut = suffixOut == null ? out : commonSuffix(suffixOut, out); } void updateIn(String in) { prefixIn = prefixIn == null ? in : commonPrefix(prefixIn, in); suffixIn = suffixIn == null ? in : commonSuffix(suffixIn, in); } public String processIn(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) { throw new RuntimeException("TODO"); } } // for "find" tasks (e.g. "abcde" to "[[abc]]de") static class LInputPattern implements Learner { String regexp = ""; public void processInOut(String in, String out) { int i = out.indexOf("[["), j = out.indexOf("]]", i+1); if (j < 0) return; String s = out.substring(i+2, j); regexp = s.replaceAll("\\d+", Matcher.quoteReplacement("\\d+")); System.out.println("regexp: " + regexp); } public String processIn(String in) { if (regexp.length() == 0) return in; else return in.replaceAll("(" + regexp + ")", "[[$1]]"); } public void toJava(Code code) { code.line("in = in.replaceAll(" + quote("(" + regexp + ")") + ", \"[[$1]]\");"); } } }