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

72
LINES

< > BotCompany Repo | #1002567 // Advanced Again Bot (LIVE)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1915L/12K/41K).

1  
!752
2  
3  
// TODO: When in a dialog, this should be prioritized by dispatcher
4  
5  
static new Map<S, Status> statusMap; // key = dialog id
6  
7  
p {
8  
  load("statusMap");
9  
}
10  
11  
static abstract class Status {
12  
  abstract S answer(S s);
13  
}
14  
15  
static class AgainThis extends Status {
16  
  S q;
17  
  *() {}
18  
  *(S *q) {}
19  
  
20  
  S answer(S s) {
21  
    status(null); // always forget question after one line
22  
    
23  
    if (isYes(s)) exceptionToUser {
24  
      ret q + " >> " + askSelf(q);
25  
    } else if (isNo(s))
26  
      ret "OK";
27  
    null;
28  
  }
29  
}
30  
31  
static void status(Status s) {
32  
  if (s == null)
33  
    statusMap.remove(getDialogID());
34  
  else
35  
    statusMap.put(getDialogID(), s);
36  
  save("statusMap");
37  
}
38  
39  
answer {
40  
  if (matchStart("again", s, m)) {
41  
    S query = m.rest().trim();
42  
    if (!empty(query)) {
43  
      L<Map> dialog = getDialog();
44  
      Map match = findMatch(dialog, query);
45  
      if (match == null)
46  
        ret "No match in history for " + quote(query);
47  
        
48  
      S q = getString(match, "question");
49  
      status(new AgainThis(q));
50  
      ret "Again this? >> " + q;
51  
    }
52  
  }
53  
  
54  
  Status status = statusMap.get(getDialogID());
55  
  if (status != null)
56  
    ret status.answer(s);
57  
}
58  
59  
static Map findMatch(L<Map> dialog, S query) {
60  
  // search for prefix
61  
  
62  
  for (int i = l(dialog)-1; i >= 0; i--)
63  
    if (matchStart(query, getString(dialog.get(i), "question")))
64  
      ret dialog.get(i);
65  
      
66  
  // search for inner match
67  
  for (int i = l(dialog)-1; i >= 0; i--)
68  
    if (find3(query, getString(dialog.get(i), "question")))
69  
      ret dialog.get(i);
70  
      
71  
  null;
72  
}

Author comment

Began life as a copy of #1002561

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002567
Snippet name: Advanced Again Bot (LIVE)
Eternal ID of this version: #1002567/1
Text MD5: b6e4ecbde72613f55fa6d0d746ca7e08
Transpilation MD5: 59408ec0b5dc80e7d69c36b40426edb3
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-03-03 03:09:13
Source code size: 1594 bytes / 72 lines
Pitched / IR pitched: No / No
Views / Downloads: 767 / 1227
Referenced in: [show references]