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

168
LINES

< > BotCompany Repo | #1003485 // Dialog Optimizer 3 (with makeGrabber)

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

Libraryless. Click here for Pure Java version (3095L/22K/70K).

1  
!759
2  
3  
static S theme =
4  
  "Let's examine your input";
5  
  
6  
static O preprocessor = func(S in) {
7  
  S s = findLastQuoted(in);
8  
  ret s == null ? null : parsePoemLine(s).type();
9  
};
10  
11  
//include #1003412 // LooseBot v4
12  
!include #1003413 // LooseBot v5
13  
!include #1003407 // OccTree2
14  
15  
static OccTree2 tree, botTree;
16  
static O makeBot;
17  
sbool walkTree_debug;
18  
sbool useAdapter = true, useGrabber = true;
19  
20  
p {
21  
  L<Dialog> dialogs = loadDialogs(theme);
22  
  tree = goodDialogs2occTree2(dialogs);
23  
  botTree = goodDialogs2occTree2(dialogs);
24  
  printOccTree2(botTree);
25  
  print("Tree size: " + tree.size() + ", bot tree size: " + botTree.size());
26  
  makeBot = func {
27  
    LooseBot bot = new LooseBot(botTree, func { new WordAdapter });
28  
    bot.debug = true;
29  
    if (useGrabber) pcall {
30  
      bot.grabber = makeGrabber(botTree, preprocessor);
31  
      if (bot.grabber != null)
32  
        print("Grabber made!");
33  
    }
34  
    ret bot;
35  
  };
36  
  //walkTree_debug = true;
37  
  try {
38  
    walkTree();
39  
  } catch e {
40  
    print("Engine not working, abort.");
41  
    printStackTrace(e);
42  
    ret;
43  
  }
44  
  print("Tree walked! " + inputFed + " -> " + outputGot);
45  
  
46  
  int origSize = botTree.size()-1;
47  
  print("Engine works! Trying to shrink the tree.");
48  
  
49  
  while (canShrinkTree()) {
50  
    print("Tree shrunk, trying it again.");
51  
  }
52  
  
53  
  int newSize = botTree.size()-1;
54  
  printOccTree2(botTree);
55  
  if (newSize < origSize) {
56  
    long percent = newSize == 0 ? 9999 : round(origSize*100.0/newSize-100);
57  
    print("Done shrinking tree (" + origSize + " -> " + newSize + " entries - " + percent + "% shrinkage!)");
58  
  } else
59  
    print("Tree unshrinkable.");
60  
}
61  
62  
static int inputFed, outputGot;
63  
64  
sbool canShrinkTree() {
65  
  L<OccTree2> leaves = botTree.allLeaves();
66  
  print("Leaves found: " + l(leaves));
67  
  
68  
  bool anyChange = false;
69  
  for (OccTree2 leaf : leaves) {
70  
    print("Trying to remove leaf: " + leaf.e + " (level " + leaf.level() + ")");
71  
    Reattach reattach = removeLeaf(leaf);
72  
    //printOccTree2(botTree);
73  
    if (canWalkTree()) {
74  
      print("Leaf removed successfully!");
75  
      anyChange = true;
76  
    } else
77  
      reattach.reattach();
78  
  }
79  
  
80  
  ret anyChange;
81  
}
82  
83  
sclass Reattach {
84  
  OccTree2 node, leaf;
85  
  int idx;
86  
87  
  void reattach() {
88  
    leaf.parent = node;
89  
    node.next.add(idx, leaf);
90  
  }
91  
}
92  
93  
static Reattach removeLeaf(OccTree2 leaf) {
94  
  new Reattach r;
95  
  r.leaf = leaf;
96  
  r.node = leaf.parent;
97  
  r.idx = leaf.parent.next.indexOf(leaf);
98  
  leaf.parent.next.remove(r.idx);
99  
  leaf.parent = null;
100  
  ret r;
101  
}
102  
103  
sbool canWalkTree() {
104  
  ret canWalkTree(tree, call(makeBot));
105  
}
106  
107  
sbool canWalkTree(OccTree2 tree, O bot) {
108  
  try { walkTree(tree, bot); true; } catch e {
109  
    if (walkTree_debug)
110  
      printStackTrace(e);
111  
    ret false;
112  
  }
113  
}
114  
115  
svoid walkTree() {
116  
  walkTree(tree, call(makeBot));
117  
}
118  
119  
svoid walkTree(OccTree2 tree, O bot) {
120  
  if (tree.isLeaf())
121  
    assertNull(getOutput(bot));
122  
  else
123  
    for (E e : tree.followUpKeys()) {
124  
      if (!contains(ll("good", "bad"), e.state)) {
125  
        bool input = isInput(e);
126  
        if (walkTree_debug)
127  
          print((input ? "FEED " : "CHECK ") + structure(e));
128  
        if (input) {
129  
          assertNull(getOutput(bot));
130  
          feed(bot, e);
131  
        } else
132  
          checkOutput(e, getOutput(bot));
133  
        walkTree(tree.followUp(e), bot);
134  
        if (walkTree_debug)
135  
          print("Rewind " + structure(e));
136  
        rewind(bot);
137  
      }
138  
    }
139  
}
140  
141  
svoid rewind(O bot) {
142  
  call(bot, "rewind"); // rewind one step
143  
}
144  
145  
svoid feed(O bot, E e) {
146  
  call(bot, "take", e);
147  
  ++inputFed;
148  
}
149  
150  
static E getOutput(O bot) {
151  
  E e = cast call(bot, "getSingleOutput");
152  
  if (e != null) ++outputGot;
153  
  ret e;
154  
}
155  
156  
svoid checkOutput(E e, E output) {
157  
  //assertEquals(e, output);
158  
  if (neq(e, output)) {
159  
    print("Bot fail: " + e + " vs " + output);
160  
    fail();
161  
  }
162  
}
163  
164  
static bool isInput(E e) {
165  
  ret e.q()
166  
    //|| (e.state() && !matchStart("bot", e.state));
167  
    ;
168  
}

Author comment

Began life as a copy of #1003408

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: #1003485
Snippet name: Dialog Optimizer 3 (with makeGrabber)
Eternal ID of this version: #1003485/1
Text MD5: e8da84a86e7171b8158bf07b38d799b8
Transpilation MD5: 6eeed13568bade7a36f4fa04bc14cbfc
Author: stefan
Category: javax / talking robots
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-07-05 21:21:19
Source code size: 4001 bytes / 168 lines
Pitched / IR pitched: No / No
Views / Downloads: 559 / 610
Referenced in: [show references]