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

158
LINES

< > BotCompany Repo | #1002462 // Dynamic Turing Questions Bot

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

Libraryless. Click here for Pure Java version (1987L/12K/42K).

!752

!include #1002461 // Source

static class TuringQuestion {
  S id;
  S question;
  S answer;
  Source source;
  int level; // difficulty level, 1 or higher (0 means 1)
}

// validate or invalidate a question
static class Val {
  S id; // turing question id
  boolean value;
  Source source;
  
  *() {}
  *(S *id, boolean *value, Source *source) {}
}

static new L<TuringQuestion> questions;
static long nextID = 1;
static new L<Val> vals;

p {
  load("questions");
  load("nextID");
  load("vals");
}

synchronized answer {
  if (match("list turing questions", s) || match("list tq", s)) {
    S url = "bots.tinybrain.de/1002408?id=" + parseSnippetID("#1002463");
    ret n(l(questions), "question") + ", results here: " + url + "\n" + simpleList();
  }
    
  if (match("full list turing questions", s) || match("full list tq", s))
    ret n(l(questions), "question") + "\n" + structureLines(questions);
  if (match("count turing questions", s) || match("count tq", s))
    ret str(l(questions));
    
  if "show tq * full" {
    S id = "tq" + m.unq(0);
    ret structure(findQuestion(id));
  }
  
  if (match("*", s, m)) {
    S x = m.unq(0);
    if (x.startsWith("tq"))
      ret structure(findQuestion(x));
  }
  
  // add last dialog as turing question
  if "add tq" {
    O dialog = last(getDialog());
    if (dialog == null) ret "No dialog to add";
    
    new TuringQuestion tq;
    tq.question = getString(dialog, "question");
    tq.answer = removeAtName(getString(dialog, "answer"));
    tq.source = new Source(s);
    ret addTQ(tq);
  }
    
  // add last question as turing question with custom answer
  if "add tq *" {
    O dialog = last(getDialog());
    if (dialog == null) ret "No dialog to add";
    
    new TuringQuestion tq;
    tq.question = getString(dialog, "question");
    tq.answer = m.unq(0);
    tq.source = new Source(s);
    ret addTQ(tq);
  }
    
  if (match("add tq * *", s, m) || match("add turing question * *", s, m)) {
    new TuringQuestion tq;
    tq.question = m.unq(0);
    tq.answer = m.unq(1);
    tq.source = new Source(s);
    ret addTQ(tq);
  }
  
  if (match("add tq level * * *", s, m) || match("add turing question level * * *", s, m)) {
    new TuringQuestion tq;
    tq.level = m.psi(0);
    tq.question = m.unq(1);
    tq.answer = m.unq(2);
    tq.source = new Source(s);
    ret addTQ(tq);
  }
  
  if (match("run tq", s) || match("run turing questions", s))
    ret askSelf(format("run turing test *", "#1002463"));
    
  if "run tq *" {
    S id = "tq" + m.unq(0);
    TuringQuestion tq = findQuestion(id);
    if (tq == null) ret "TQ not found";
    ret checkTQ(tq) ? id + " solved" : id +" not solved";
  }
    
  if "show tq *" {
    S id = "tq" + m.unq(0);
    TuringQuestion tq = findQuestion(id);
    if (tq == null) ret "TQ not found";
    ret tq.question + " => " + tq.answer;
  }
  
  if "invalidate tq *" {
    S id = "tq" + m.unq(0);
    TuringQuestion tq = findQuestion(id);
    if (tq == null) ret "TQ not found";
    vals.add(new Val(id, false, new Source(s)));
    save("vals");
    ret "OK, your input is recorded.";
  }
}

static synchronized TuringQuestion findQuestion(S id) {
  ret findByField(questions, "id", id);
}

static synchronized L<TuringQuestion> getLiveList() {
  ret cloneList(questions);
}

static synchronized S simpleList() {
  Map valsByID = indexByField(vals, "id");
  new L<S> l;
  for (TuringQuestion tq : questions) {
    boolean inv = valsByID.containsKey(tq.id);
    l.add(tq.question + " => " + tq.answer + (inv ? " (invalidated)" : ""));
  }
  ret fromLines(l);
}

static S html() {
  ret h3("Turing Questions")
    + sourceCodeToHTML(simpleList());
}

static S addTQ(TuringQuestion tq) {
  tq.id = "tq" + nextID++;
  save("nextID");
  questions.add(tq);
  save("questions");
  boolean solved = checkTQ(tq);
  ret "OK, added as question " + tq.id + ". "
    + (solved ? "Also, solved." : "Not solved yet.");
}

static boolean checkTQ(TuringQuestion tq) {
  ret turingMatch(tq.answer, askSelf(tq.question));
}

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: #1002462
Snippet name: Dynamic Turing Questions Bot
Eternal ID of this version: #1002462/1
Text MD5: e3e6e6c451a46919522e96e1e986b711
Transpilation MD5: a8f465feecabe48e5821c498605143fb
Author: stefan
Category: eleu
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-03 00:05:14
Source code size: 4175 bytes / 158 lines
Pitched / IR pitched: No / No
Views / Downloads: 574 / 1481
Referenced in: [show references]