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

91
LINES

< > BotCompany Repo | #1003370 // Tree-Walking DialogBot + LooseBot

JavaX fragment (include)

abstract sclass DialogBot {
  sbool debug;
  OccTree<E> node;
  
  *(OccTree<E> *node) {}
  
  L<E> handle(L<E> in) {
    int i = 0;
    while (i < l(in) && in.get(i).q != null) {
      if (!selectMatching(in.get(i))) break;
      ++i;
    }
    
    new L<E> out;
    E e;
    while ((e = nextBotOutput()) != null) {
      out.add(e);
      select(e);
    }
    ret out;
  }
  
  void take(E e) {
    selectMatching(e);
  }
  
  E getSingleOutput() {
    E e = nextBotOutput();
    if (e != null)
      select(e);
    ret e;
  }
  
  void rewind() {
    node = node.parent;
  }
  
  bool selectMatching(E in) {
    for (E e : keys(node.followUp))
      if (matchE(e, in)) {
        if (debug)
          print("Selecting " + struct(e) + " matching " + struct(in));
        select(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;
    L<E> follows = viableFollowUps();
    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(E e) {
    node = node.followUp(e);
    if (node == null) fail("can't select " + structure(e));
  }
  
  L<E> viableFollowUps() {
    new L<E> l;
    for (E e : keys(node.followUp))
      if (isViable(node.followUp(e)))
        l.add(e);
    ret l;
  }
  
  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
    if (a.q()) ret match(a.q, b.q);
    if (a.a()) ret match(a.a, b.a);
    ret match(a.state, b.state);
  }  
}

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: #1003370
Snippet name: Tree-Walking DialogBot + LooseBot
Eternal ID of this version: #1003370/1
Text MD5: 35df0010662c10445ada6fc1bd10453d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-06-29 01:13:41
Source code size: 1978 bytes / 91 lines
Pitched / IR pitched: No / No
Views / Downloads: 554 / 882
Referenced in: [show references]