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

!759

static S theme =
  "Let's examine your input";
  
static O preprocessor = func(S in) {
  S s = findLastQuoted(in);
  ret s == null ? null : parsePoemLine(s).type();
};

//include #1003412 // LooseBot v4
!include #1003413 // LooseBot v5
!include #1003407 // OccTree2

static OccTree2 tree, botTree;
static O makeBot;
sbool walkTree_debug;
sbool useAdapter = true, useGrabber = true;

p {
  L<Dialog> dialogs = loadDialogs(theme);
  tree = goodDialogs2occTree2(dialogs);
  botTree = goodDialogs2occTree2(dialogs);
  printOccTree2(botTree);
  print("Tree size: " + tree.size() + ", bot tree size: " + botTree.size());
  makeBot = func {
    LooseBot bot = new LooseBot(botTree, func { new WordAdapter });
    bot.debug = true;
    if (useGrabber) pcall {
      bot.grabber = makeGrabber(botTree, preprocessor);
      if (bot.grabber != null)
        print("Grabber made!");
    }
    ret bot;
  };
  //walkTree_debug = true;
  try {
    walkTree();
  } catch e {
    print("Engine not working, abort.");
    printStackTrace(e);
    ret;
  }
  print("Tree walked! " + inputFed + " -> " + outputGot);
  
  int origSize = botTree.size()-1;
  print("Engine works! Trying to shrink the tree.");
  
  while (canShrinkTree()) {
    print("Tree shrunk, trying it again.");
  }
  
  int newSize = botTree.size()-1;
  printOccTree2(botTree);
  if (newSize < origSize) {
    long percent = newSize == 0 ? 9999 : round(origSize*100.0/newSize-100);
    print("Done shrinking tree (" + origSize + " -> " + newSize + " entries - " + percent + "% shrinkage!)");
  } else
    print("Tree unshrinkable.");
}

static int inputFed, outputGot;

sbool canShrinkTree() {
  L<OccTree2> leaves = botTree.allLeaves();
  print("Leaves found: " + l(leaves));
  
  bool anyChange = false;
  for (OccTree2 leaf : leaves) {
    print("Trying to remove leaf: " + leaf.e + " (level " + leaf.level() + ")");
    Reattach reattach = removeLeaf(leaf);
    //printOccTree2(botTree);
    if (canWalkTree()) {
      print("Leaf removed successfully!");
      anyChange = true;
    } else
      reattach.reattach();
  }
  
  ret anyChange;
}

sclass Reattach {
  OccTree2 node, leaf;
  int idx;

  void reattach() {
    leaf.parent = node;
    node.next.add(idx, leaf);
  }
}

static Reattach removeLeaf(OccTree2 leaf) {
  new Reattach r;
  r.leaf = leaf;
  r.node = leaf.parent;
  r.idx = leaf.parent.next.indexOf(leaf);
  leaf.parent.next.remove(r.idx);
  leaf.parent = null;
  ret r;
}

sbool canWalkTree() {
  ret canWalkTree(tree, call(makeBot));
}

sbool canWalkTree(OccTree2 tree, O bot) {
  try { walkTree(tree, bot); true; } catch e {
    if (walkTree_debug)
      printStackTrace(e);
    ret false;
  }
}

svoid walkTree() {
  walkTree(tree, call(makeBot));
}

svoid walkTree(OccTree2 tree, O bot) {
  if (tree.isLeaf())
    assertNull(getOutput(bot));
  else
    for (E e : tree.followUpKeys()) {
      if (!contains(ll("good", "bad"), e.state)) {
        bool input = isInput(e);
        if (walkTree_debug)
          print((input ? "FEED " : "CHECK ") + structure(e));
        if (input) {
          assertNull(getOutput(bot));
          feed(bot, e);
        } else
          checkOutput(e, getOutput(bot));
        walkTree(tree.followUp(e), bot);
        if (walkTree_debug)
          print("Rewind " + structure(e));
        rewind(bot);
      }
    }
}

svoid rewind(O bot) {
  call(bot, "rewind"); // rewind one step
}

svoid feed(O bot, E e) {
  call(bot, "take", e);
  ++inputFed;
}

static E getOutput(O bot) {
  E e = cast call(bot, "getSingleOutput");
  if (e != null) ++outputGot;
  ret e;
}

svoid checkOutput(E e, E output) {
  //assertEquals(e, output);
  if (neq(e, output)) {
    print("Bot fail: " + e + " vs " + output);
    fail();
  }
}

static bool isInput(E e) {
  ret e.q()
    //|| (e.state() && !matchStart("bot", e.state));
    ;
}

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: 560 / 610
Referenced in: #1003500 - Dialog Optimizer / What's the date