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

95
LINES

< > BotCompany Repo | #1003314 // GMail Bot with stop/move buttons (dev.)

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

Libraryless. Click here for Pure Java version (3125L/22K/72K).

!759

static S scriptID = "#1003305";

!include #1003380 // Chat

static JTextArea thoughtArea;
static Chat chat;

static new L<E> script;
static int idx;

// learned examples - >0 = move, <0 = stop
static PersistentMap<O, Int> examples;

p {
  examples = new PersistentMap("examples");
  printStructure(examples);
  script = parsePoem(scriptID);
  
  chat = new Chat;
  thoughtArea = new JTextArea;
  
  JPanel buttons = hgrid(
    jbutton("Stop", "learnStop"),
    jbutton("Move", "learnMove"));
  
  showSideBySide(programTitle(), chat.swing(),
    withTitle("Thoughts", centerAndSouth(thoughtArea, buttons)));
    
  chat.requestFocus();
  chat.onEnter_pre = runnable {
    if (eq(probableNextMove(), "move")) {
      ++idx;
      predict;
    }
  };
  
  onUpdate(chat.chatField, "predict");
  
  //swingEverySecond(thoughtArea, runnable { predict; });
  predict;
}

svoid predict {
  new StringBuilder buf;
  try {
    buf.append(unparsePoem(subList(script, idx)) + "\n\n");
    S move = probableNextMove();
    buf.append("Probable next move: " + move + "\n\n");
  } catch Throwable e {
    buf.append("\n\n" + getStackTrace(e));
  }
  setText(thoughtArea, buf);
}

sS userInput { ret chat.chatField.getText(); }

sS probableNextMove {
  S input = userInput();
  
  // stop at end
  if (idx >= l(script)) ret "stop";
  
  // move if correct input
  E e = get(script, idx);
  if (e.q != null && match(e.q, input)) ret "move";
  
  // ask learning module
  S cmd = query(makeState());
  if (nempty(cmd)) ret cmd;
  
  // anything
  ret "stop";
}

sL makeState {
  ret litlist(idx, scriptID, userInput());
}

svoid learnStop { learn(makeState(), -1); }
svoid learnMove { learn(makeState(),  1); }

svoid learn(O state, int value) {
  int count = toInt(examples.get(state)) + value;
  print("Learner: Updating " + structure(state) + " to " + count);
  examples.put(state, count);
  predict;
}

sS query(O state) {
  int count = toInt(examples.get(state));
  print("Learner: Query " + structure(state) + " = " + count);
  ret count > 1 ? "move" : count < 0 ? "stop" : null;
}

Author comment

Began life as a copy of #1003306

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: #1003314
Snippet name: GMail Bot with stop/move buttons (dev.)
Eternal ID of this version: #1003314/1
Text MD5: 7cbcd2b602eeace652e0f1194d37dc4f
Transpilation MD5: 5f605f1672106c7ef97650c8005a168c
Author: stefan
Category: javax / talking robots
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-06-28 21:23:59
Source code size: 2171 bytes / 95 lines
Pitched / IR pitched: No / No
Views / Downloads: 574 / 634
Referenced in: [show references]