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).

1  
!752
2  
3  
!include #1003283 // class C
4  
5  
sclass OccTree {
6  
  int count;
7  
  new HashMap<C, OccTree> followUp;
8  
  
9  
  void add(L<C> script) {
10  
    OccTree node = this;
11  
    ++count;
12  
    for (C c : script)
13  
      (node = node.followUp(c)).count++;
14  
  }
15  
  
16  
  OccTree followUp(C c) {
17  
    OccTree node = followUp.get(c);
18  
    if (node == null)
19  
      followUp.put(c, node = new OccTree);
20  
    ret node;
21  
  }
22  
  
23  
  C randomChoice() {
24  
    MultiSet<C> ms = new MultiSet(false);
25  
    for (C c : keys(followUp))
26  
      ms.add(c, followUp.get(c).count);
27  
    ret msOneOf(ms);
28  
  }
29  
}
30  
31  
p {
32  
  L<C> script = poemToC("#1003260");
33  
  new OccTree tree;
34  
  tree.add(script);
35  
  tree.add(poemToC("Usr: hello"));
36  
  tree.add(poemToC("Usr: hallo"));
37  
  //printStructure(tree);
38  
  print(asciiHeading("Random"));
39  
  print(unparsePoem(poemFromC(randomScript(tree))));
40  
}
41  
42  
static L<C> randomScript(OccTree tree) {
43  
  C c;
44  
  new L<C> script;
45  
  while ((c = tree.randomChoice()) != null) {
46  
    script.add(c);
47  
    tree = tree.followUp(c);
48  
  }
49  
  ret script;
50  
}

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: 525 / 597
Referenced in: [show references]