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

108
LINES

< > BotCompany Repo | #1004778 // "?id music" (WORKS)

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

Transpiled version (2679L) is out of date.

!759

!include #1004810 // Concepts etc.

//include #1004817 // externalize stuff (repeat less code)

// What we think the user chose
sclass OptionChosen extends Concept {
  new Ref<UserAnswer> userAnswer;
  S choice;
}

extend Answer {
  new Ref<OptionChosen> inResponseTo;
}

p {
  loadConcepts();
  autoSaveConcepts();
  action();
}

static void processNewUserLine(UserLine ul) {
  S s = trim(ul.post.text);
  
  if (swic(s, "?id ")) { // assume "?id music"
    // action: ask user what he means by "music"
    
    S q = trim(s.substring(2));
    print("ul= " + structure(ul));
    Question question = findBackRefOfType(ul, Question.class);
    print("Question = " + struct(question));
    
    // did we ask this yet? if not: do it
    
    if (question == null) {
      S text = "Uh... do you want some MUSIC or the MUSIC PLAYER?";
      UserLine ulQ = postToChat(text);
      if (ulQ == null) { print("Couldn't post!"); ret; }
      question = new Question;
      question.userLine.set(ulQ);
      question = new Question(ul);
    }
  }
}

static void dbActions {
  // Scan through the answers we got from user and do something
  // with them.
  
  L<UserAnswer> userAnswers = conceptsOfType(UserAnswer.class);
  //printStructure("User Answers: ", userAnswers);
  for (UserAnswer ua : userAnswers) {
    // We only ask one question, so we know what this is about
    
    ELPost post = ua.userLine.get().post;
    //print("Processing: " + post.text);
    
    OptionChosen option = findBackRef(ua, OptionChosen.class);
    
    if (option == null) {
      // Interpret & save what user has said (-> "OptionChosen")
    
      option = new OptionChosen;
      option.userAnswer.set(ua);
      option.choice = getOption(trim(post.text));

      // VERIFY new link
      
      assertSame(option, findBackRef(ua, OptionChosen.class));
    }
    
    // Check if we answered already.
    
    Answer ourAnswer = findBackRef(option, Answer.class);
    
    if (ourAnswer != null) {
      //print("  We answered already.");
    } else {
      ourAnswer = new Answer;
      ourAnswer.inResponseTo.set(option);

      // VERIFY that we solved the condition above
      assertSame(ourAnswer, findBackRef(option, Answer.class));
      
      S text = makeAnswer(option);
      UserLine ulA = postToChat(text);
      if (ulA == null) { print("Couldn't post!"); ret; }
      ourAnswer.userLine.set(ulA);
    }
  }
}

// learnable! [interpret user input]
// returns "music", "player" or null (any unknown input)
static S getOption(S s) {
  if (containsIgnoreCase(s, "player")) ret "player";
  if (containsIgnoreCase(s, "music")) ret "music";
  null;
}

static S makeAnswer(OptionChosen option) {
  if (eq(option.choice, "player"))
    ret "Current Music Player Is: " + snippetWithTitle("#1003779");
  if (eq(option.choice, "music"))
    ret "Here's some polish music: " + snippetWithTitle("#1001093");
  ret "I don't understand";
}

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: #1004778
Snippet name: "?id music" (WORKS)
Eternal ID of this version: #1004778/1
Text MD5: b7ea29e164e94f174478df59b73423ec
Author: stefan
Category: javax / talking robots
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-09-02 14:03:35
Source code size: 3034 bytes / 108 lines
Pitched / IR pitched: No / No
Views / Downloads: 908 / 1284
Referenced in: #1003253 - New Homepage (LIVE)
#1004810 - Concepts / UserLine / Question / UserAnswer / Answer
#1004812 - User Belief Bot (dev.)