abstract sclass Adapter { void learn(S in, S out) {} S get(S in) { ret in; } O remember() { null; } void rewind(O o) {} } abstract sclass DialogBot { sbool debug, repeat = true; OccTree2 tree; new L<OccTree2> nodes; new L<L> rewindStack; new L adapterRewind; Adapter adapter; *(OccTree2 *tree) { nodes.add(tree); } *(OccTree2 tree, Adapter adapter) { this(tree); this.adapter = adapter; } void take(E in) { remember(); if (repeat) setAdd(nodes, tree); new L<OccTree2> l; for (OccTree2 node : nodes) for (OccTree2 n : node.next) if (matchE(adapt(n.e), in)) l.add(n); if (adapter != null) for (OccTree2 n : l) adapter.learn(adapt(n.e).text(), in.text()); nodes = l; } E adapt(E e) { if (adapter != null) ret new E(adapter.get(e.text()), e.type()); // TODO: security when rewriting states ret e; } void remember() { if (rewindStack != null) { rewindStack.add(cloneList(nodes)); if (adapter != null) adapterRewind.add(adapter.remember()); } } E getSingleOutput() { remember(); new L<OccTree2> l; for (OccTree2 node : nodes) for (OccTree2 n : node.next) if (n.e.a() || isCommand(n.e)) l.add(n); nodes = l; ret nempty(nodes) ? adapt(first(nodes).e) : null; } void rewind() { nodes = popLast(rewindStack); if (adapter != null) adapter.rewind(popLast(adapterRewind)); } abstract bool matchE(E a, E b); bool isCommand(E e) { ret e.state() && matchStart("bot", e.state); } void noRewind() { rewindStack = null; } } sclass LooseBot extends DialogBot { *(OccTree2 node) { super(node); } *(OccTree2 tree, Adapter adapter) { super(tree, adapter); } bool matchE(E a, E b) { // standard NL matching ret eq(a.type(), b.type()) && match(a.text(), b.text()); } } sclass WordAdapter extends Adapter { new Map<S, S> wordMap; void learn(S in, S out) { L<S> t1 = nlTok2(dropPunctuation2(in)); L<S> t2 = nlTok2(dropPunctuation2(out)); if (l(t1) != l(t2)) ret; for (int i = 1; i < l(t1); i += 2) { S w1 = t1.get(i), w2 = t2.get(i); if (!eqic(w1, w2)) // just overwrite - be flexible! wordMap.put(w1.toLowerCase(), w2.toLowerCase()); } } S get(S s) { L<S> tok = nlTok2(s); for (int i = 1; i < l(tok); i += 2) { S w = wordMap.get(tok.get(i).toLowerCase()); if (nempty(w)) tok.set(i, w); } ret join(tok); } O remember() { ret cloneMap(wordMap); } void rewind(O o) { wordMap = (Map) o; } }
Began life as a copy of #1003410
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1003412 |
Snippet name: | Tree-Walking DialogBot + LooseBot v4 (parallel walk) |
Eternal ID of this version: | #1003412/1 |
Text MD5: | a26eeb5b0f9ec4656209db032357234c |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-07-01 03:24:36 |
Source code size: | 2798 bytes / 119 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 579 / 935 |
Referenced in: | [show references] |