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

52
LINES

< > BotCompany Repo | #1000465 // LCertainElement (learner)

JavaX fragment (include)

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  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
808 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

Snippet ID: #1000465
Snippet name: LCertainElement (learner)
Eternal ID of this version: #1000465/1
Text MD5: 1f1f3c3a12867d4a483db0e8aea29414
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-09 20:23:09
Source code size: 1518 bytes / 52 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 769 / 1964
Referenced in: [show references]