1 | // finds a fixed element, e.g. a Java token
|
2 |
|
3 | static class LCertainElement extends LearnerImpl {
|
4 | static boolean debug;
|
5 | ReversibleFunction f; // e.g. JavaTok
|
6 | String element;
|
7 |
|
8 | *(ReversibleFunction *f) {}
|
9 |
|
10 | public void processInOut(Object _in, Object _out) {
|
11 | String in = (String) _in, out = (String) _out;
|
12 | Set<String> set = new HashSet<String>(getMarked(out));
|
13 | if (debug)
|
14 | System.out.println("LCertainElement: set=" + set);
|
15 | if (set.size() == 1)
|
16 | element = set.iterator().next();
|
17 | if (debug)
|
18 | System.out.println("LCertainElement: element=" + element);
|
19 | }
|
20 |
|
21 | public Object processIn(Object _in) {
|
22 | List<String> l = (List<String>) f.process(_in);
|
23 | new List<String> l2;
|
24 | for (String x : l)
|
25 | l2.add(x.equals(element) ? "[[" + x + "]]" : x);
|
26 | return f.unprocess(l2);
|
27 | }
|
28 |
|
29 | // returns positions in marked string (from/to/from/to...)
|
30 | static List<Integer> getFindMarkers2(String s) {
|
31 | List<Integer> l = new ArrayList<Integer>();
|
32 | int i = 0;
|
33 | while (i < s.length()) {
|
34 | int j = s.indexOf("[[", i);
|
35 | if (j < 0) break;
|
36 | int k = s.indexOf("]]", j+2);
|
37 | if (k < 0) break;
|
38 | l.add(j);
|
39 | l.add(k+2);
|
40 | i = k+2;
|
41 | }
|
42 | return l;
|
43 | }
|
44 |
|
45 | static List<String> getMarked(String s) {
|
46 | List<Integer> l = getFindMarkers2(s);
|
47 | new List<String> result;
|
48 | for (int i = 0; i < l.size(); i += 2)
|
49 | result.add(s.substring(l.get(i)+2, l.get(i+1)-2));
|
50 | return result;
|
51 | }
|
52 | } |