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

51
LINES

< > BotCompany Repo | #1014764 // translateUsingWordTreeC - works on code token parses

JavaX fragment (include)

1  
sbool translateUsingWordTreeC_debug;
2  
sbool translateUsingWordTreeC_printReplacements;
3  
4  
static L<S> translateUsingWordTreeC(L<S> tok, Map<S, O> tree) {  
5  
  for (int i = 0; i < l(tok); )
6  
    i = translateUsingWordTreeC_scanFrom(tok, i, tree);
7  
  ret tok;
8  
}
9  
      
10  
// returns index of first unreplaced token
11  
// every replacement ends up as a single token plus some ""
12  
static int translateUsingWordTreeC_scanFrom(L<S> tok, int i, Map<S, O> tree) {
13  
  int j = i;
14  
  S best = null; // best replacement found
15  
  int bestIndex = 0; // till where to replace
16  
  
17  
  while true {
18  
    if (j > i) { // no default in root!
19  
      S def = cast tree.get("<default>");
20  
      if (def != null) {
21  
        best = def; bestIndex = j;
22  
        //print("best solution: " + sfu(bestSolution));
23  
      }
24  
    }
25  
   
26  
    if (j >= l(tok)) break;
27  
    
28  
    O t = tree.get(tok.get(j));
29  
    if (translateUsingWordTreeC_debug)
30  
      print(i + "-" + j + " / " + joinWithSpace(subList(tok, i, j+1)) + " => " + sfu(t));
31  
    if (t == null) break;
32  
    if (t instanceof S) {
33  
      int k = j+1;
34  
      if (translateUsingWordTreeC_printReplacements)
35  
        print("translateUsingWordTreeC: replacing " + sfu(subList(tok, i, k-1) + " with " + quote(t));
36  
      replaceTokens(tok, i, k, (S) t);
37  
      ret k;
38  
    }
39  
    tree = (Map) t;
40  
    j++;
41  
  }
42  
  
43  
  if (best != null) {
44  
    int k = bestIndex;
45  
    if (translateUsingWordTreeC_printReplacements)
46  
      print("translateUsingWordTreeC: replacing " + sfu(subList(tok, i, k) + " with " + quote(best));
47  
    replaceTokens(tok, i, k, best);
48  
    ret k;
49  
  }
50  
  ret i+1;
51  
}

Author comment

Began life as a copy of #1014205

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: #1014764
Snippet name: translateUsingWordTreeC - works on code token parses
Eternal ID of this version: #1014764/3
Text MD5: 5db3fdf44151702d424f4561b7000bcc
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-01 20:56:41
Source code size: 1605 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 304 / 340
Version history: 2 change(s)
Referenced in: [show references]