1 | sbool translateUsingWordTree_debug; |
2 | sbool translateUsingWordTree_printReplacements; |
3 | |
4 | sS translateUsingWordTree(S input, Map<S, O> tree) { |
5 | ret empty(input) ? input : join(translateUsingWordTree(javaTok(input), tree)); |
6 | } |
7 | |
8 | static L<S> translateUsingWordTree(L<S> tok, Map<S, O> tree) { |
9 | for (int i = 1; i < l(tok); ) |
10 | i = translateUsingWordTree_scanFrom(tok, i, tree); |
11 | ret tok; |
12 | } |
13 | |
14 | // returns index of first unreplaced token |
15 | // every replacement ends up as a single token plus some "" |
16 | static int translateUsingWordTree_scanFrom(L<S> tok, int i, Map<S, O> tree) { |
17 | int j = i; |
18 | S best = null; // best replacement found |
19 | int bestIndex = 0; // till where to replace |
20 | |
21 | while true { |
22 | if (j > i) { // no default in root! |
23 | S def = cast tree.get("<default>"); |
24 | if (def != null) { |
25 | best = def; bestIndex = j; |
26 | //print("best solution: " + sfu(bestSolution)); |
27 | } |
28 | } |
29 | |
30 | if (j >= l(tok)) break; |
31 | |
32 | O t = tree.get(tok.get(j)); |
33 | if (translateUsingWordTree_debug) |
34 | print(i + "-" + j + " / " + joinSubList(tok, i, j+1) + " => " + sfu(t)); |
35 | if (t == null) break; |
36 | if (t instanceof S) { |
37 | int k = j+2; |
38 | if (translateUsingWordTree_printReplacements) |
39 | print("translateUsingWordTree: replacing " + sfu(subList(tok, i, k-1) + " with " + quote(t)); |
40 | replaceTokens(tok, i, k-1, (S) t); |
41 | ret k; |
42 | } |
43 | tree = (Map) t; |
44 | j += 2; |
45 | } |
46 | |
47 | if (best != null) { |
48 | int k = bestIndex; |
49 | replaceTokens(tok, i, k-1, best); |
50 | ret k; |
51 | } |
52 | ret i+2; |
53 | } |
54 | |
55 | |
56 | |
57 | sS translateUsingWordTree(Map<S, O> tree, S input) { |
58 | ret translateUsingWordTree(input, tree); |
59 | } |
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1014205 |
Snippet name: | translateUsingWordTree - uses javaTok (doesn't ignore punctuation) |
Eternal ID of this version: | #1014205/17 |
Text MD5: | 0b7dd3f2c8664bf124212a5615a90c2b |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-03-24 01:36:55 |
Source code size: | 1690 bytes / 59 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 418 / 460 |
Version history: | 16 change(s) |
Referenced in: | [show references] |