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

60
LINES

< > BotCompany Repo | #1001494 // Answer Questions Bot (developing)

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

Libraryless. Click here for Pure Java version (1683L/11K/39K).

// Answer Questions Bot knows how to answer questions to any other bot.

!747

m {
  static new AList<Question> questions;
  
  !include #1001495 // AList
  
  static class Question {
    S botID;
    S questionPattern;
    new Code code;
  }
  
  static class Code {
    S snippetID;
    boolean blessed; // is it known to be safe to run?
  }
  
  p {
    readLocally("questions");
    makeAndroid3("Questions Bot.");
  }
  
  static synchronized S answer(S s, L<S> history) {
    new Matches m;
    
    if (match3("how can question * to bot * be answered?", s, m)) {
      S botID = formatSnippetID(m.unq(0));
      S text = m.unq(1);
      L<Question> l = findQuestion(botID, text);
      if (l.isEmpty())
        ret "I don't know.";
      Code code = l.get(0).code;
      ret format3("code: *, (" + (code.blessed ? "blessed" : "non-blessed") + ")", code.snippetID);
    }
    
    if (match3("add: bot *, question *, code snippet: *, blessed: *", s, m)) {
      new Question q;
      q.botID = m.fsi(0);
      q.questionPattern = m.unq(1);
      q.code.snippetID = m.fsi(2);
      q.code.blessed = m.bool(3);
    }
    
    ret standardQuery(s, "questions");
  }
  
  static L<Question> findQuestion(S botID, S text) {
    new L<Question> l;
    
    // It is thread-safe to iterate over an AList :)
    for (Question q : questions)
      if (sameSnippetID(q.botID, botID)
        && match3(q.questionPattern, text))
        l.add(q);
    ret l;
  }
}

Author comment

Began life as a copy of #1001493

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001494
Snippet name: Answer Questions Bot (developing)
Eternal ID of this version: #1001494/1
Text MD5: bd79681b7c9277f813be585ae39adfa2
Transpilation MD5: 0b080757b4dd646d47c6899e7e000f17
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-20 23:52:48
Source code size: 1516 bytes / 60 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 675 / 702
Referenced in: [show references]