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

50
LINES

< > BotCompany Repo | #1003277 // Predict (prints a random script from occurrence tree)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (2018L/13K/46K).

!752

!include #1003283 // class C

sclass OccTree {
  int count;
  new HashMap<C, OccTree> followUp;
  
  void add(L<C> script) {
    OccTree node = this;
    ++count;
    for (C c : script)
      (node = node.followUp(c)).count++;
  }
  
  OccTree followUp(C c) {
    OccTree node = followUp.get(c);
    if (node == null)
      followUp.put(c, node = new OccTree);
    ret node;
  }
  
  C randomChoice() {
    MultiSet<C> ms = new MultiSet(false);
    for (C c : keys(followUp))
      ms.add(c, followUp.get(c).count);
    ret msOneOf(ms);
  }
}

p {
  L<C> script = poemToC("#1003260");
  new OccTree tree;
  tree.add(script);
  tree.add(poemToC("Usr: hello"));
  tree.add(poemToC("Usr: hallo"));
  //printStructure(tree);
  print(asciiHeading("Random"));
  print(unparsePoem(poemFromC(randomScript(tree))));
}

static L<C> randomScript(OccTree tree) {
  C c;
  new L<C> script;
  while ((c = tree.randomChoice()) != null) {
    script.add(c);
    tree = tree.followUp(c);
  }
  ret script;
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003277
Snippet name: Predict (prints a random script from occurrence tree)
Eternal ID of this version: #1003277/1
Text MD5: 04e0b3daf117b6b4d5d0bb378222af60
Transpilation MD5: 86dcbb4954573f1e0a78f9637eae15a7
Author: stefan
Category: javax / talking robots
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-06-15 00:46:30
Source code size: 1047 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 520 / 588
Referenced in: [show references]