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

82
LINES

< > BotCompany Repo | #1003405 // Tree-Walking DialogBot + LooseBot v2 (dev.)

JavaX fragment (include)

// expects a "good tree" now

abstract sclass DialogBot {
  sbool debug, repeat = true;
  OccTree<E> tree, node;
  
  *(OccTree<E> *tree) { node = tree; }
  
  void take(E e) {
    selectMatching(e);
  }
  
  E getSingleOutput() {
    E e = nextBotOutput();
    if (e != null)
      select(node, e);
    ret e;
  }
  
  void rewind() {
    node = node.parent;
  }
  
  E findMatch(OccTree<E> node, E in) {
    for (E e : keys(node.followUp))
      if (matchE(e, in))
        ret e;
    null;
  }
  
  bool selectMatching(E in) {
    E e = findMatch(node, in);
    bool rep = false;
    if (repeat && e == null && node != tree) {
      e = findMatch(tree, in);
      rep = true;
    }
    if (e != null) {
      if (debug) {
        if (rep)
          print("Repeat!");
        print("Selecting " + struct(e) + " matching " + struct(in));
      }
      select(rep ? tree : node, e);
      true;
    }
    if (debug)
      print("No match for " + structure(in) + " in " + structure(node.followUpKeys()));
    false;
  }
  
  abstract bool matchE(E a, E b);
  
  E nextBotOutput() {
    new L<E> l;
    Collection<E> follows = keys(node.followUp);
    for (E e : follows)
      if (e.a() || isCommand(e))
        l.add(e);
    if (empty(l)) null; // fail("no good bot output: " + struct(node.followUpKeys()));
    if (l(l) > 1) fail("unclear bot output: " + struct(follows));
    ret first(l);
  }
  
  void select(OccTree<E> n, E e) {
    node = n.followUp(e);
    if (node == null) fail("can't select " + structure(e));
  }
  
  bool isCommand(E e) {
    ret e.state() && matchStart("bot", e.state);
  }
}

sclass LooseBot extends DialogBot {
  *(OccTree<E> node) { super(node); }
  
  bool matchE(E a, E b) {
    // standard NL matching
    ret eq(a.type(), b.type()) && match(a.text(), b.text());
  }  
}

Author comment

Began life as a copy of #1003370

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1003405
Snippet name: Tree-Walking DialogBot + LooseBot v2 (dev.)
Eternal ID of this version: #1003405/1
Text MD5: c4759547c661c93d3bbc6794d5d1266a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-06-30 20:31:19
Source code size: 1888 bytes / 82 lines
Pitched / IR pitched: No / No
Views / Downloads: 598 / 831
Referenced in: #1003401 - Dialog Optimizer (dev.)
#1003410 - Tree-Walking DialogBot + LooseBot v3 (dev.)