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

1  
!759
2  
3  
static S scriptID = "#1003305";
4  
5  
!include #1003380 // Chat
6  
7  
static JTextArea thoughtArea;
8  
static Chat chat;
9  
10  
static new L<E> script;
11  
static int idx;
12  
13  
// learned examples - >0 = move, <0 = stop
14  
static PersistentMap<O, Int> examples;
15  
16  
p {
17  
  examples = new PersistentMap("examples");
18  
  printStructure(examples);
19  
  script = parsePoem(scriptID);
20  
  
21  
  chat = new Chat;
22  
  thoughtArea = new JTextArea;
23  
  
24  
  JPanel buttons = hgrid(
25  
    jbutton("Stop", "learnStop"),
26  
    jbutton("Move", "learnMove"));
27  
  
28  
  showSideBySide(programTitle(), chat.swing(),
29  
    withTitle("Thoughts", centerAndSouth(thoughtArea, buttons)));
30  
    
31  
  chat.requestFocus();
32  
  chat.onEnter_pre = runnable {
33  
    if (eq(probableNextMove(), "move")) {
34  
      ++idx;
35  
      predict;
36  
    }
37  
  };
38  
  
39  
  onUpdate(chat.chatField, "predict");
40  
  
41  
  //swingEverySecond(thoughtArea, runnable { predict; });
42  
  predict;
43  
}
44  
45  
svoid predict {
46  
  new StringBuilder buf;
47  
  try {
48  
    buf.append(unparsePoem(subList(script, idx)) + "\n\n");
49  
    S move = probableNextMove();
50  
    buf.append("Probable next move: " + move + "\n\n");
51  
  } catch Throwable e {
52  
    buf.append("\n\n" + getStackTrace(e));
53  
  }
54  
  setText(thoughtArea, buf);
55  
}
56  
57  
sS userInput { ret chat.chatField.getText(); }
58  
59  
sS probableNextMove {
60  
  S input = userInput();
61  
  
62  
  // stop at end
63  
  if (idx >= l(script)) ret "stop";
64  
  
65  
  // move if correct input
66  
  E e = get(script, idx);
67  
  if (e.q != null && match(e.q, input)) ret "move";
68  
  
69  
  // ask learning module
70  
  S cmd = query(makeState());
71  
  if (nempty(cmd)) ret cmd;
72  
  
73  
  // anything
74  
  ret "stop";
75  
}
76  
77  
sL makeState {
78  
  ret litlist(idx, scriptID, userInput());
79  
}
80  
81  
svoid learnStop { learn(makeState(), -1); }
82  
svoid learnMove { learn(makeState(),  1); }
83  
84  
svoid learn(O state, int value) {
85  
  int count = toInt(examples.get(state)) + value;
86  
  print("Learner: Updating " + structure(state) + " to " + count);
87  
  examples.put(state, count);
88  
  predict;
89  
}
90  
91  
sS query(O state) {
92  
  int count = toInt(examples.get(state));
93  
  print("Learner: Query " + structure(state) + " = " + count);
94  
  ret count > 1 ? "move" : count < 0 ? "stop" : null;
95  
}

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