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).

!752

// TODO: When in a dialog, this should be prioritized by dispatcher

static new Map<S, Status> statusMap; // key = dialog id

p {
  load("statusMap");
}

static abstract class Status {
  abstract S answer(S s);
}

static class AgainThis extends Status {
  S q;
  *() {}
  *(S *q) {}
  
  S answer(S s) {
    status(null); // always forget question after one line
    
    if (isYes(s)) exceptionToUser {
      ret q + " >> " + askSelf(q);
    } else if (isNo(s))
      ret "OK";
    null;
  }
}

static void status(Status s) {
  if (s == null)
    statusMap.remove(getDialogID());
  else
    statusMap.put(getDialogID(), s);
  save("statusMap");
}

answer {
  if (matchStart("again", s, m)) {
    S query = m.rest().trim();
    if (!empty(query)) {
      L<Map> dialog = getDialog();
      Map match = findMatch(dialog, query);
      if (match == null)
        ret "No match in history for " + quote(query);
        
      S q = getString(match, "question");
      status(new AgainThis(q));
      ret "Again this? >> " + q;
    }
  }
  
  Status status = statusMap.get(getDialogID());
  if (status != null)
    ret status.answer(s);
}

static Map findMatch(L<Map> dialog, S query) {
  // search for prefix
  
  for (int i = l(dialog)-1; i >= 0; i--)
    if (matchStart(query, getString(dialog.get(i), "question")))
      ret dialog.get(i);
      
  // search for inner match
  for (int i = l(dialog)-1; i >= 0; i--)
    if (find3(query, getString(dialog.get(i), "question")))
      ret dialog.get(i);
      
  null;
}

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