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