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)

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

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