Libraryless. Click here for Pure Java version (2289L/18K/55K).
!747 abstract class P { abstract S read(L<S> 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<S> tok; static Set<int> predicted; static class Chain extends P { new L<P> list; *() {} *(L<P> *list) {} *(P... a) { list = asList(a); } void add(P p) { list.add(p); } S read(L<S> 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<L<S>,S> map = new HashMap<L<S>,S>(); int n, seen; *(int *n) { } S read(L<S> tok) { while (tok.size() > seen) { ++seen; if (seen > n) map.put(new ArrayList<S>(tok.subList(seen-n-1, seen-1)), tok.get(seen-1)); } if (tok.size() >= n) return map.get(new ArrayList<S>(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<L<S>,S>(map); return t; } } static class DerivedHashMap<A, B> extends AbstractMap<A, B> { Map<A, B> base; new HashMap<A, B> additions; *(Map<A, B> *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<Map.Entry<A,B>> 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>(); int points = 0, total = 0, lastPercent = 0; new L<S> 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<S> makeCorpusJavaTok(int numSnippets) { SnippetDB db = new SnippetDB(corpusID); List<List<S>> rows = db.rowsOrderedBy("sn_created"); new L<S> 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<S> internAll(L<S> tok) { new L<S> l; for (S t : tok) l.add(t.intern()); return l; } static class Collector { P winner; double bestScore = -1; Set<int> 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; } } } }
Began life as a copy of #1000995
download show line numbers debug dex old transpilations
Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1001000 |
Snippet name: | Token prediction, multiple predictors (improving architecture) |
Eternal ID of this version: | #1001000/1 |
Text MD5: | 69216550a151d4f135d39f4547654aec |
Transpilation MD5: | 5fd815aa086d3473101aa689540506e8 |
Author: | stefan |
Category: | |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-09-15 20:55:52 |
Source code size: | 5669 bytes / 221 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 861 / 949 |
Referenced in: | #1001011 - Token prediction, multiple predictors (adding zip support) #3000382 - Answer for ferdie (>> t = 1, f = 0) #3000383 - Answer for funkoverflow (>> t=1, f=0 okay) |