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

71
LINES

< > BotCompany Repo | #1001035 // CodePatterns (predictor)

JavaX fragment (include)

1  
// TODO
2  
3  
static class CodePatterns extends P {
4  
  Map<L<S>,S> map = new HashMap<L<S>,S>();
5  
  int n;
6  
7  
  *(int *n) {
8  
  }
9  
  
10  
  S read(S file, L<S> tok) {
11  
    prepare(file);
12  
    
13  
    while (tok.size() > seen) {
14  
      ++seen;
15  
      if (seen > n)
16  
        put(new ArrayList<S>(tok.subList(seen-n-1, seen-1)), tok.get(seen-1));
17  
    }
18  
    
19  
    if (tok.size() >= n) {
20  
      L<S> l = new ArrayList<S>(tok.subList(tok.size()-n, tok.size()));
21  
      for (L<S> pl : map.keySet()) {
22  
        S pr = map.get(pl);
23  
        print("pl: " + structure(pl) + ", l: " + structure(l));
24  
        Map<S, S> m = makeMapPrefix(pl, l);
25  
        if (m != null) {
26  
          S result = m.get(pr);
27  
          print("map: " + structure(m) + ", result: " + quote(result));
28  
          ret result;
29  
        }
30  
      }
31  
    }
32  
      
33  
    return null;
34  
  }
35  
  
36  
  void put(L<S> l, S r) {
37  
    if (isPattern(l, r)) {
38  
      new L<S> l2;
39  
      l2.addAll(l);
40  
      l2.add(r);
41  
      print("pattern: " + structure(l2));
42  
      map.put(l, r);
43  
    }
44  
  }
45  
  
46  
  boolean isPattern(L<S> l, S r) {
47  
    /*new Set<S> set;
48  
    set.addAll(l);
49  
    set.add(r);
50  
    return set.size() < l.size()+1;*/
51  
    return l.contains(r) && interestingToken(r);
52  
  }
53  
   
54  
  boolean interestingToken(S r) {
55  
    //return !r.trim().equals("");
56  
    for (int i = 0; i < r.length(); i++)
57  
      if (Character.isLetter(r.charAt(i)))
58  
        ret true;
59  
    ret false;
60  
  }
61  
  
62  
  P derive() {
63  
    Patterns t = new Patterns(n);
64  
    t.map = new DerivedHashMap<L<S>,S>(map);
65  
    return t;
66  
  }
67  
  
68  
  P clear() {
69  
    return new Patterns(n);
70  
  }
71  
}

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: #1001035
Snippet name: CodePatterns (predictor)
Eternal ID of this version: #1001035/1
Text MD5: 50aab9d9c36b603126789cbbe3ff4e73
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-16 21:04:23
Source code size: 1607 bytes / 71 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 703 / 615
Referenced in: [show references]