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)

1  
// expects a "good tree" now
2  
3  
abstract sclass DialogBot {
4  
  sbool debug, repeat = true;
5  
  OccTree<E> tree, node;
6  
  
7  
  *(OccTree<E> *tree) { node = tree; }
8  
  
9  
  void take(E e) {
10  
    selectMatching(e);
11  
  }
12  
  
13  
  E getSingleOutput() {
14  
    E e = nextBotOutput();
15  
    if (e != null)
16  
      select(node, e);
17  
    ret e;
18  
  }
19  
  
20  
  void rewind() {
21  
    node = node.parent;
22  
  }
23  
  
24  
  E findMatch(OccTree<E> node, E in) {
25  
    for (E e : keys(node.followUp))
26  
      if (matchE(e, in))
27  
        ret e;
28  
    null;
29  
  }
30  
  
31  
  bool selectMatching(E in) {
32  
    E e = findMatch(node, in);
33  
    bool rep = false;
34  
    if (repeat && e == null && node != tree) {
35  
      e = findMatch(tree, in);
36  
      rep = true;
37  
    }
38  
    if (e != null) {
39  
      if (debug) {
40  
        if (rep)
41  
          print("Repeat!");
42  
        print("Selecting " + struct(e) + " matching " + struct(in));
43  
      }
44  
      select(rep ? tree : node, e);
45  
      true;
46  
    }
47  
    if (debug)
48  
      print("No match for " + structure(in) + " in " + structure(node.followUpKeys()));
49  
    false;
50  
  }
51  
  
52  
  abstract bool matchE(E a, E b);
53  
  
54  
  E nextBotOutput() {
55  
    new L<E> l;
56  
    Collection<E> follows = keys(node.followUp);
57  
    for (E e : follows)
58  
      if (e.a() || isCommand(e))
59  
        l.add(e);
60  
    if (empty(l)) null; // fail("no good bot output: " + struct(node.followUpKeys()));
61  
    if (l(l) > 1) fail("unclear bot output: " + struct(follows));
62  
    ret first(l);
63  
  }
64  
  
65  
  void select(OccTree<E> n, E e) {
66  
    node = n.followUp(e);
67  
    if (node == null) fail("can't select " + structure(e));
68  
  }
69  
  
70  
  bool isCommand(E e) {
71  
    ret e.state() && matchStart("bot", e.state);
72  
  }
73  
}
74  
75  
sclass LooseBot extends DialogBot {
76  
  *(OccTree<E> node) { super(node); }
77  
  
78  
  bool matchE(E a, E b) {
79  
    // standard NL matching
80  
    ret eq(a.type(), b.type()) && match(a.text(), b.text());
81  
  }  
82  
}

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