Transpiled version (224L) is out of date.
!752 p { new Predictor p; S s = "hello hello "; int score = 0; for (int i = 0; i <= l(s); i++) { S x = s.substring(0, i); S y = s.substring(i, i+1); char c = p.predict(y); bool correct = i < l(s) && s.charAt(i) == c; print((correct ? '*' : ' ') + " " + quote(x) + " => " + quote(c)); if (correct) ++score; } print("Score: " + score + "/" + l(s)); } static class Predictor { private TreeMap < Character, LinkedList < Integer >> tree = new TreeMap < Character, LinkedList < Integer >> (); private int longestMatch = -1; /** * @return predicted next character of the string */ public char predict(String str) { if (str.length() == 0) return '0'; if (str.length() == 1) { longestMatch = 0; return str.charAt(0); } if (tree.containsKey(str.charAt(str.length() - 1))) { longestMatch++; LinkedList < Integer > pred = tree.get(str.charAt(str.length() - 1)); tree.clear(); for (int pos: pred) { char c = str.charAt(pos + 1); LinkedList < Integer > temp; if (tree.containsKey(c)) temp = tree.get(c); else temp = new LinkedList < Integer > (); temp.add(pos + 1); tree.put(c, temp); } } else { longestMatch = -1; int m = 1; int i = 0; int[] table = createTable(str); while (m + i < str.length()) { if (str.charAt(str.length() - i - 1) == str.charAt(str.length() - (m + i) - 1)) i++; else { insertPrediction(str.charAt(str.length() - m), i, str.length() - m); m = m + i - table[i]; if (table[i] >= 0) i = table[i]; } } if (i > 0) insertPrediction(str.charAt(str.length() - m), i, str.length() - m); } char prediction = '0'; int maxCount = 0; Iterator < Character > it = tree.keySet().iterator(); while (it.hasNext()) { char key = it.next(); int count = tree.get(key).size(); if (count > maxCount) { prediction = key; maxCount = count; } } return prediction; } private void insertPrediction(char c, int i, int pos) { if (i > longestMatch) { tree.clear(); longestMatch = i; } if (i < longestMatch) return; LinkedList < Integer > pred = null; if (tree.containsKey(c)) pred = tree.get(c); else pred = new LinkedList < Integer > (); pred.add(pos); tree.put(c, pred); } private int[] createTable(String str) { int pos = 2; int cnd = 0; int[] table = new int[str.length() - 1]; table[0] = -1; while (pos < str.length() - 1) { if (str.charAt(str.length() - pos) == str.charAt(str.length() - cnd - 1)) { table[pos] = cnd + 1; pos++; cnd++; } else if (cnd > 0) cnd = table[cnd]; else { table[pos] = 0; pos++; } } return table; } }
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1003052 |
Snippet name: | Knoll Text Prediction |
Eternal ID of this version: | #1003052/1 |
Text MD5: | b032c75c00f61206bde65a6002fce31b |
Author: | stefan |
Category: | eleu / nl |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-04-26 21:07:13 |
Source code size: | 3595 bytes / 112 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 557 / 638 |
Referenced in: | #3000382 - Answer for ferdie (>> t = 1, f = 0) #3000383 - Answer for funkoverflow (>> t=1, f=0 okay) |