Download Jar. Libraryless. Click here for Pure Java version (6893L/49K).
1 | !7 |
2 | |
3 | sclass Token { |
4 | S text; |
5 | CNC group; |
6 | int index; |
7 | |
8 | toString { ret text; } |
9 | } |
10 | |
11 | sclass CNC { |
12 | S text; // if known |
13 | new L<Token> tokens; |
14 | } |
15 | |
16 | p-exp { |
17 | S s = "There are 6 chess pieces. How many chess pieces do you know?"; |
18 | CNC in1 = makeCNC("There are 6 chess pieces. How many chess pieces do you know?"); |
19 | CNC out1 = makeCNC("6 I guess"); |
20 | CNC in2 = makeCNC("There are 16 chess pieces. How many chess pieces do you know?"); |
21 | |
22 | LPair<Token> l = zipCNC(in1, in2); |
23 | if (l == null) ret; |
24 | l = antiFilter(l, func(Pair<Token> p) -> Bool { eqic(p.a.text, p.b.text) }); |
25 | print("Remaining differences:"); |
26 | pnl(l); |
27 | SS daMap = pairsToMap(pairListMap(func(Token t) -> S { t.text }, l)); |
28 | printStruct(daMap); |
29 | S result = join(mapTokens_getOrKeep(collect(out1.tokens, 'text), daMap)); |
30 | assertEqualsVerbose("16 I guess", result); |
31 | } |
32 | |
33 | static CNC makeCNC(S s) { |
34 | CNC cnc = nu(CNC, text := s); |
35 | L<S> tok = javaTokWithAngleBrackets(s); |
36 | for i over tok: |
37 | cnc.tokens.add(nu(Token, text := tok.get(i), group := cnc, index := i); |
38 | ret cnc; |
39 | } |
40 | |
41 | // may return null (no match) |
42 | static LPair<Token> zipCNC(CNC cnc1, CNC cnc2) { |
43 | ret zipTwoListsToPairs_ifSameLength(cnc1.tokens, cnc2.tokens); |
44 | } |
45 | |
46 | static LPair<Token> dropSame(LPair<Token> l) { |
47 | ret antiFilter(l, func(Pair<Token> p) -> Bool { eqic(p.a.text, p.b.text) }); |
48 | } |
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1017527 |
Snippet name: | Intelligent Token Objects [OK] |
Eternal ID of this version: | #1017527/9 |
Text MD5: | 39fec17320383e523d62392635db1bfe |
Transpilation MD5: | 21b57e91d0a91128e38569c6b28e1df2 |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX source code (desktop) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-07-26 10:26:09 |
Source code size: | 1366 bytes / 48 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 434 / 914 |
Version history: | 8 change(s) |
Referenced in: | [show references] |