Libraryless. Click here for Pure Java version (2134L/17K/51K).
1 | !747 |
2 | |
3 | abstract class P { |
4 | abstract void write(S tok); |
5 | abstract S read(); |
6 | } |
7 | |
8 | m { |
9 | static class Pairs extends P { |
10 | new Map<S,S> map; |
11 | S last; |
12 | |
13 | void write(S tok) { |
14 | if (last != null) |
15 | map.put(last, tok); |
16 | last = tok; |
17 | } |
18 | |
19 | S read() { |
20 | return last == null ? null : map.get(last); |
21 | } |
22 | } |
23 | |
24 | p { |
25 | P p = new Pairs; |
26 | S corpus = makeCorpus(10); |
27 | L<S> tok = javaTok(corpus); |
28 | print("Tokens in corpus: " + tok.size()); |
29 | |
30 | new L<Boolean> predicted; |
31 | int points = 0; |
32 | for (int i = 1; i < tok.size(); i += 2) { |
33 | S t = tok.get(i); |
34 | S x = p.read(); |
35 | boolean correct = t.equals(x); |
36 | while (predicted.size() < i) predicted.add(false); |
37 | predicted.add(correct); |
38 | if (correct) ++points; |
39 | p.write(t); |
40 | } |
41 | double score = points*100.0/(tok.size()/2); |
42 | print(points + "/"+ tok.size()/2 + " = " + formatDouble(score, 2) + "%"); |
43 | |
44 | showColoredText(tok, predicted); |
45 | } |
46 | |
47 | static void showColoredText(L<S> tok, L<Boolean> predicted) ctex { |
48 | JFrame jf = new JFrame("Predicted = green"); |
49 | Container cp = jf.getContentPane(); |
50 | |
51 | JTextPane pane = new JTextPane(); |
52 | //pane.setFont(loadFont("#1000993", 24)); |
53 | Document doc = pane.getStyledDocument(); |
54 | |
55 | for (int i = 0; i < tok.size(); i++) { |
56 | if (tok.get(i).length() == 0) continue; |
57 | boolean green = i < predicted.size() && predicted.get(i); |
58 | SimpleAttributeSet set = new SimpleAttributeSet(); |
59 | StyleConstants.setForeground(set, green ? Color.green : Color.gray); |
60 | doc.insertString(doc.getLength(), tok.get(i), set); |
61 | } |
62 | |
63 | JScrollPane scrollPane = new JScrollPane(pane); |
64 | cp.add(scrollPane, BorderLayout.CENTER); |
65 | |
66 | jf.setBounds(100, 100, 600, 600); |
67 | jf.setVisible(true); |
68 | } |
69 | |
70 | !include #1000989 // SnippetDB |
71 | |
72 | static S makeCorpus(int numSnippets) { |
73 | new StringBuilder buf; |
74 | SnippetDB db = new SnippetDB("#1000673"); |
75 | List<List<S>> rows = db.rowsOrderedBy("sn_created"); |
76 | for (int i = 0; i < Math.min(rows.size(), numSnippets); i++) { |
77 | S id = db.getField(rows.get(i), "sn_id"); |
78 | S title = db.getField(rows.get(i), "sn_title"); |
79 | S text = db.getField(rows.get(i), "sn_text"); |
80 | buf.append("\n== ID: " + id); |
81 | buf.append("\n== Title: " + title); |
82 | buf.append("\n==\n"); |
83 | buf.append(text).append("\n"); |
84 | ++i; |
85 | } |
86 | return buf.toString(); |
87 | } |
88 | } |
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1000991 |
Snippet name: | Token prediction with graphical display |
Eternal ID of this version: | #1000991/1 |
Text MD5: | 7c65fe7ebea79b2f6d745411ac79759f |
Transpilation MD5: | 9aebadd15ce2d040cde1bcbbd035f2a3 |
Author: | stefan |
Category: | |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-09-14 18:23:50 |
Source code size: | 2481 bytes / 88 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 651 / 744 |
Referenced in: | [show references] |