!747 abstract class P { abstract S read(L tok); abstract P derive(); // clone & reset counter for actual use } m { static S corpusID = "#1001006"; static int numSnippets = 3000; static boolean showGUI = true; static int maxCharsGUI = 500000; static Collector collector; static L tok; static Set predicted; static class Chain extends P { new L

list; *() {} *(L

*list) {} *(P... a) { list = asList(a); } void add(P p) { list.add(p); } S read(L tok) { for (P p : list) { S s = p.read(tok); if (s != null) return s; } return null; } P derive() { new Chain c; for (P p : list) c.add(p.derive()); return c; } } static class Tuples extends P { Map,S> map = new HashMap,S>(); int n, seen; *(int *n) { } S read(L tok) { while (tok.size() > seen) { ++seen; if (seen > n) map.put(new ArrayList(tok.subList(seen-n-1, seen-1)), tok.get(seen-1)); } if (tok.size() >= n) return map.get(new ArrayList(tok.subList(tok.size()-n, tok.size()))); return null; } // slow... P oldDerive() { Tuples t = new Tuples(n); t.map.putAll(map); // t.seen == 0 which is ok return t; } // fast! P derive() { Tuples t = new Tuples(n); t.map = new DerivedHashMap,S>(map); return t; } } static class DerivedHashMap extends AbstractMap { Map base; new HashMap additions; *(Map *base) {} public B get(Object key) { B b = additions.get(key); if (b != null) return b; return base.get(key); } public B put(A key, B value) { return additions.put(key, value); } public Set> entrySet() { throw fail(); } } // TODO: Put NewX back in p { tok = makeCorpusJavaTok(numSnippets); print("Tokens in corpus: " + tok.size()); print("Learning..."); collector = new Collector; /*test(new Tuples(1)); test(new Tuples(2)); test(new Tuples(3)); test(new Tuples(4)); test(new Chain(new Tuples(2), new Tuples(1)));*/ test(new Chain(new Tuples(4), new Tuples(3), new Tuples(2), new Tuples(1))); print("Learning done."); if (collector.winner != null && showGUI) { predicted = collector.predicted; showColoredText(); } } // test a predictor static void test(P p) { predicted = new TreeSet(); int points = 0, total = 0, lastPercent = 0; new L history; for (int i = 1; i < tok.size(); i += 2) { S t = tok.get(i); S x = p.read(history); boolean correct = t.equals(x); total += t.length(); if (correct) { predicted.add(i); points += t.length(); } history.add(t); int percent = roundUpTo(10, (int) (i*100L/tok.size())); if (percent > lastPercent) { print("Learning " + percent + "% done."); lastPercent = percent; } } double score = points*100.0/total; collector.add(p, score); } static void showColoredText() ctex { JFrame jf = new JFrame("Predicted = green"); Container cp = jf.getContentPane(); JTextPane pane = new JTextPane(); //pane.setFont(loadFont("#1000993", 24)); Document doc = pane.getStyledDocument(); int i = tok.size(), len = 0; while (len <= maxCharsGUI && i > 0) { --i; len += tok.get(i).length(); } for (; i < tok.size(); i++) { if (tok.get(i).length() == 0) continue; boolean green = predicted.contains(i); SimpleAttributeSet set = new SimpleAttributeSet(); StyleConstants.setForeground(set, green ? Color.green : Color.gray); doc.insertString(doc.getLength(), tok.get(i), set); } JScrollPane scrollPane = new JScrollPane(pane); cp.add(scrollPane, BorderLayout.CENTER); jf.setBounds(100, 100, 600, 600); jf.setVisible(true); } !include #1000989 // SnippetDB static L makeCorpusJavaTok(int numSnippets) { SnippetDB db = new SnippetDB(corpusID); List> rows = db.rowsOrderedBy("sn_created"); new L tok; for (int i = 0; i < Math.min(rows.size(), numSnippets); i++) { new StringBuilder buf; S id = db.getField(rows.get(i), "sn_id"); S title = db.getField(rows.get(i), "sn_title"); S text = db.getField(rows.get(i), "sn_text"); buf.append("\n== ID: " + id); buf.append("\n== Title: " + title); buf.append("\n==\n"); buf.append(text).append("\n"); if (tok.size() != 0) tok.remove(tok.size()-1); tok.addAll(javaTok(buf.toString())); ++i; } return internAll(tok); } static L internAll(L tok) { new L l; for (S t : tok) l.add(t.intern()); return l; } static class Collector { P winner; double bestScore = -1; Set predicted; void add(P p, double score) { if (winner == null || score > bestScore) { winner = p; bestScore = score; //S name = shorten(structure(p), 100); S name = p.getClass().getName(); print("New best score: " + formatDouble(score, 2) + "% (" + name + ")"); this.predicted = main.predicted; } } } }