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

69
LINES

< > BotCompany Repo | #1001041 - Patterns (predictor)

JavaX fragment (include)

static class Patterns extends P {
  Map<L<S>,S> map = new HashMap<L<S>,S>();
  int n;

  *(int *n) {
  }
  
  S read(S file, L<S> tok) {
    prepare(file);
    
    while (tok.size() > seen) {
      ++seen;
      if (seen > n)
        put(new ArrayList<S>(tok.subList(seen-n-1, seen-1)), tok.get(seen-1));
    }
    
    if (tok.size() >= n) {
      L<S> l = new ArrayList<S>(tok.subList(tok.size()-n, tok.size()));
      for (L<S> pl : map.keySet()) {
        S pr = map.get(pl);
        //print("pl: " + structure(pl) + ", l: " + structure(l));
        Map<S, S> m = makeMapPrefix(pl, l);
        if (m != null) {
          S result = m.get(pr);
          //print("map: " + structure(m) + ", result: " + quote(result));
          ret result;
        }
      }
    }
      
    return null;
  }
  
  void put(L<S> l, S r) {
    if (isPattern(l, r)) {
      new L<S> l2;
      l2.addAll(l);
      l2.add(r);
      //print("pattern: " + structure(l2));
      map.put(l, r);
    }
  }
  
  boolean isPattern(L<S> l, S r) {
    /*new Set<S> set;
    set.addAll(l);
    set.add(r);
    return set.size() < l.size()+1;*/
    return l.contains(r) && interestingToken(r);
  }
   
  boolean interestingToken(S r) {
    //return !r.trim().equals("");
    for (int i = 0; i < r.length(); i++)
      if (Character.isLetter(r.charAt(i)))
        ret true;
    ret false;
  }
  
  P derive() {
    Patterns t = new Patterns(n);
    t.map = new DerivedHashMap<L<S>,S>(map);
    return t;
  }
  
  P clear() {
    return new Patterns(n);
  }
}

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: #1001041
Snippet name: Patterns (predictor)
Eternal ID of this version: #1001041/1
Text MD5: 687f076ac85170475e259cde04afd9dc
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-16 21:44:06
Source code size: 1598 bytes / 69 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 443 / 620
Referenced in: [show references]