1 | // adapters are immutable! (plus returns a modified clone) |
2 | abstract sclass Adapter { |
3 | abstract bool canMatch(S in, S out); |
4 | Adapter plus(S in, S out) { ret this; } |
5 | S get(S in) { ret in; } |
6 | double size() { ret 0; } |
7 | } |
8 | |
9 | sclass WordAdapter extends Adapter { |
10 | new Map<S, S> wordMap; |
11 | |
12 | L<S> tok(S s) { |
13 | ret nlTok2(dropPunctuation2(s)); |
14 | } |
15 | |
16 | bool canMatch(S in, S out) { |
17 | L<S> t1 = tok(in), t2 = tok(out); |
18 | ret l(t1) == l(t2); |
19 | } |
20 | |
21 | Adapter plus(S in, S out) { |
22 | L<S> t1 = tok(in), t2 = tok(out); |
23 | if (l(t1) != l(t2)) ret this; |
24 | |
25 | WordAdapter a = cast nuObject(getClass()); |
26 | a.wordMap = cloneMap(wordMap); |
27 | for (int i = 1; i < l(t1); i += 2) { |
28 | S w1 = t1.get(i), w2 = t2.get(i); |
29 | if (!eqic(w1, w2)) |
30 | // just overwrite - be flexible! |
31 | a.wordMap.put(w1.toLowerCase(), w2.toLowerCase()); |
32 | } |
33 | ret a; |
34 | } |
35 | |
36 | S get(S s) { |
37 | L<S> tok = nlTok2(s); |
38 | for (int i = 1; i < l(tok); i += 2) { |
39 | S w = lookupToken(tok.get(i)); |
40 | if (nempty(w)) |
41 | tok.set(i, w); |
42 | } |
43 | ret join(tok); |
44 | } |
45 | |
46 | S lookupToken(S s) { |
47 | ret wordMap.get(s.toLowerCase()); |
48 | } |
49 | |
50 | double size() { |
51 | ret l(wordMap); |
52 | } |
53 | |
54 | public S toString() { |
55 | ret structure(wordMap); |
56 | } |
57 | } |
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
No comments. add comment
Snippet ID: | #1004027 |
Snippet name: | Adapter + WordAdapter |
Eternal ID of this version: | #1004027/1 |
Text MD5: | 278479dee51a424e20e3280aedb02d58 |
Author: | stefan |
Category: | javax / talking robots |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-08-04 02:25:06 |
Source code size: | 1292 bytes / 57 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 624 / 1346 |
Referenced in: | [show references] |