Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

112
LINES

< > BotCompany Repo | #1003052 // Knoll Text Prediction

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Transpiled version (224L) is out of date.

1  
!752
2  
3  
p {
4  
  new Predictor p;
5  
  S s = "hello hello ";
6  
  
7  
  int score = 0;
8  
  for (int i = 0; i <= l(s); i++) {
9  
    S x = s.substring(0, i);
10  
    S y = s.substring(i, i+1);
11  
    char c = p.predict(y);
12  
    bool correct = i < l(s) && s.charAt(i) == c;
13  
    print((correct ? '*' : ' ') + " " + quote(x) + " => " + quote(c));
14  
    if (correct)
15  
      ++score;
16  
  }
17  
  print("Score: " + score + "/" + l(s));
18  
}
19  
20  
static class Predictor {
21  
    private TreeMap < Character, LinkedList < Integer >> tree = new TreeMap < Character, LinkedList < Integer >> ();
22  
    private int longestMatch = -1;
23  
    /**
24  
     * @return predicted next character of the string
25  
     */
26  
    public char predict(String str) {
27  
        if (str.length() == 0)
28  
            return '0';
29  
        if (str.length() == 1) {
30  
            longestMatch = 0;
31  
            return str.charAt(0);
32  
        }
33  
        if (tree.containsKey(str.charAt(str.length() - 1))) {
34  
            longestMatch++;
35  
            LinkedList < Integer > pred = tree.get(str.charAt(str.length() - 1));
36  
            tree.clear();
37  
            for (int pos: pred) {
38  
                char c = str.charAt(pos + 1);
39  
                LinkedList < Integer > temp;
40  
                if (tree.containsKey(c))
41  
                    temp = tree.get(c);
42  
                else
43  
                    temp = new LinkedList < Integer > ();
44  
                temp.add(pos + 1);
45  
                tree.put(c, temp);
46  
            }
47  
        } else {
48  
            longestMatch = -1;
49  
            int m = 1;
50  
            int i = 0;
51  
            int[] table = createTable(str);
52  
            while (m + i < str.length()) {
53  
                if (str.charAt(str.length() - i - 1) == str.charAt(str.length() - (m + i) - 1))
54  
                    i++;
55  
                else {
56  
                    insertPrediction(str.charAt(str.length() - m), i, str.length() - m);
57  
                    m = m + i - table[i];
58  
                    if (table[i] >= 0)
59  
                        i = table[i];
60  
                }
61  
            }
62  
            if (i > 0)
63  
                insertPrediction(str.charAt(str.length() - m), i, str.length() - m);
64  
        }
65  
        char prediction = '0';
66  
        int maxCount = 0;
67  
        Iterator < Character > it = tree.keySet().iterator();
68  
        while (it.hasNext()) {
69  
            char key = it.next();
70  
            int count = tree.get(key).size();
71  
            if (count > maxCount) {
72  
                prediction = key;
73  
                maxCount = count;
74  
            }
75  
        }
76  
        return prediction;
77  
    }
78  
    private void insertPrediction(char c, int i, int pos) {
79  
        if (i > longestMatch) {
80  
            tree.clear();
81  
            longestMatch = i;
82  
        }
83  
        if (i < longestMatch)
84  
            return;
85  
        LinkedList < Integer > pred = null;
86  
        if (tree.containsKey(c))
87  
            pred = tree.get(c);
88  
        else
89  
            pred = new LinkedList < Integer > ();
90  
        pred.add(pos);
91  
        tree.put(c, pred);
92  
    }
93  
    private int[] createTable(String str) {
94  
        int pos = 2;
95  
        int cnd = 0;
96  
        int[] table = new int[str.length() - 1];
97  
        table[0] = -1;
98  
        while (pos < str.length() - 1) {
99  
            if (str.charAt(str.length() - pos) == str.charAt(str.length() - cnd - 1)) {
100  
                table[pos] = cnd + 1;
101  
                pos++;
102  
                cnd++;
103  
            } else if (cnd > 0)
104  
                cnd = table[cnd];
105  
            else {
106  
                table[pos] = 0;
107  
                pos++;
108  
            }
109  
        }
110  
        return table;
111  
    }
112  
}

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: 498 / 560
Referenced in: [show references]