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

82
LINES

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

JavaX fragment (include)

1  
// expects a "good tree" now
2  
3  
abstract sclass DialogBot {
4  
  sbool debug, repeat = true;
5  
  OccTree2 tree, node;
6  
  
7  
  *(OccTree2 *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(OccTree2 node, E in) {
25  
    for (E e : node.followUpKeys())
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 = node.followUpKeys();
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(OccTree2 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  
  *(OccTree2 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 #1003405

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: #1003410
Snippet name: Tree-Walking DialogBot + LooseBot v3 (dev.)
Eternal ID of this version: #1003410/1
Text MD5: 8013207cee28c1f382222dea9b8a4535
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-06-30 21:11:45
Source code size: 1878 bytes / 82 lines
Pitched / IR pitched: No / No
Views / Downloads: 634 / 605
Referenced in: [show references]