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

59
LINES

< > BotCompany Repo | #1017474 - groupUsingWordTree - uses curly brackets; values of word tree don't matter [dev.]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (4150L/27K).

sbool groupUsingWordTree_debug;
sbool groupUsingWordTree_printReplacements;

sS groupUsingWordTree(S input, Map<S, O> tree) {  
  ret join(groupUsingWordTree(javaTok(input), tree));
}
  
static L<S> groupUsingWordTree(L<S> tok, Map<S, O> tree) {  
  for (int i = 1; i < l(tok); )
    i = groupUsingWordTree_scanFrom(tok, i, tree);
  ret tok;
}
      
// returns index of first unreplaced token
// every replacement ends up as a single token plus some ""
static int groupUsingWordTree_scanFrom(L<S> tok, int i, Map<S, O> tree) {
  int j = i;
  S best = null; // best replacement found
  int bestIndex = 0; // till where to replace
  
  while true {
    if (j > i) { // no default in root!
      S def = cast tree.get("<default>");
      if (def != null) {
        best = join(subList(tok, i, j+1)); bestIndex = j;
        //print("best solution: " + sfu(bestSolution));
      }
    }
   
    if (j >= l(tok)) break;
    
    O t = tree.get(tok.get(j));
    if (groupUsingWordTree_debug)
      print(i + "-" + j + " / " + joinSubList(tok, i, j+1) + " => " + sfu(t));
    if (t == null) break;
    if (!t instanceof Map) {
      int k = j+2;
      
      // skip if already grouped
      if (!(eqGet(tok, i-2, "{") && eqGet(tok, k, "}"))) {
        LS subList = subList(tok, i, k-1);
        S replacement = curly(join(subList));
        if (groupUsingWordTree_printReplacements)
          print("groupUsingWordTree: replacing " + sfu(subList) + " with " + quote(replacement));
        replaceTokens(tok, i, k-1, replacement);
      }
      ret k;
    }
    tree = (Map) t;
    j += 2;
  }
  
  if (best != null) {
    int k = bestIndex;
    replaceTokens(tok, i, k-1, best);
    ret k;
  }
  ret i+2;
}

Author comment

Began life as a copy of #1014205

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1017474
Snippet name: groupUsingWordTree - uses curly brackets; values of word tree don't matter [dev.]
Eternal ID of this version: #1017474/3
Text MD5: 7e594f96dc3d5c7aee1b21adee14f7ad
Transpilation MD5: 88169bceb46042ed170340aa601432c8
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-09 11:04:08
Source code size: 1758 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 237 / 261
Version history: 2 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1017474 & http://1017474.tinybrain.de