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

209
LINES

< > BotCompany Repo | #1014135 // VisualKatze 5

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

Download Jar. Uses 4487K of libraries. Click here for Pure Java version (16782L/133K).

1  
!7
2  
3  
static JLabel l;
4  
static Map<S> consequentsMap = synchroCIMap(), precedentsMap = synchroCIMap();
5  
static Set<S> ignoreSet = asCISet(splitAtVerticalBar("english|englisch|german|deutsch|foreground|fairground|background|Vordergrund|Hintergrund"));
6  
sS lastUtterance, answer;
7  
static L<VF1<S>> receivers;
8  
static JTextField tfInput, tfSay, tfNote;
9  
static JButton btnSay, btnStartStop;
10  
static TailFile logTail;
11  
static JComponent catComponent, otherComponent;
12  
static SingleComponentPanel scp;
13  
sS currentList;
14  
sbool listening;
15  
static TrayIcon trayIcon; // the "start listening" icon
16  
17  
p-noconsole-autoupdate {
18  
  printAndConsoleSize(64K);
19  
  startBotInSeparateVM("Katze.", #1013936);
20  
  hideVMRunningBot("Katze.");
21  
  framesBot();
22  
  
23  
  catComponent = jscroll_verticalExtend_trackWidth(
24  
    jCenteredBackgroundImage(#1101166, setBackground(awtColor("e4e4e4"), setForeground(Color.black,
25  
    setFontSize(60, l = jcenteredlabel_noToolTip())))));
26  
  
27  
  scp = singleComponentPanel(catComponent);
28  
  final JFrame frame = frameIcon(#1101166, showMaximizedFrame(scp));
29  
  addToWindowWithMargin(frame, jcenteredline(
30  
    btnStartStop = jbutton("", f startOrStop),
31  
    btnSay = jbutton("Say", f sayIt)));
32  
    
33  
  awtCalcEveryAndNow(btnStartStop, 2000, r {
34  
    setListening(isYes(sendOpt("Chrome Speech.", "is on")));
35  
  });
36  
  
37  
  // bottom to top
38  
  addToWindowNorth(frame, withMargin(10, 1, withLabel("Add a note:", tfNote = jcenteredtextfield())));
39  
  addToWindowNorth(frame, withMargin(10, 1, withLabel("Make me say something:", tfSay = jcenteredtextfield())));
40  
  addToWindowNorth(frame, withMargin(10, 1, withLabel("Type here instead of speaking:", tfInput = jcenteredtextfield())));
41  
  
42  
  onEnter(tfInput, r-thread { katze_userTyped(getText(tfInput)) });
43  
  onEnter(tfSay, r-thread { speakInRecognizerLanguage(getTextTrim(tfSay)) });
44  
  onEnter(tfNote, r {
45  
    mechAppendQueue().add(r {
46  
      appendToMechList_noUniq("Today's Notes", "[" + localDateWithMilliseconds() + "] " + getTextTrim(tfNote)) })
47  
  });
48  
  focusOnFrameActivationAndNow(frame, btnSay);
49  
    
50  
  receivers = ll(
51  
    consequentsMap_receiver(consequentsMap), precedentsMap_receiver(precedentsMap));
52  
  time "Loading" { loading {
53  
    mapVoidMulti(receivers, notNulls(map prepLine(dropLast(linesOfFile(speechRecognitionLog())))));
54  
  } }
55  
  
56  
  logTail = tailFileLinewisePlusLastLine(speechRecognitionLog(), 100, vf1(f onUtteranceFromLog));
57  
  //makeAndCall_warmUp("mL_anInstanceOfA");
58  
}
59  
60  
sS prepLine(S s) {
61  
  s = unquoteAndDropAngleBracketStuff(s);
62  
  
63  
  // don't react on older messages
64  
  if (logTail != null && logTail.started())
65  
    printIndentUnlessEmpty(">> ", pcallAnswerMethod(s));
66  
    
67  
  ret nullIfInSet(ignoreSet, s);
68  
}
69  
70  
svoid onUtteranceFromLog(S utterance) {
71  
  if (empty(utterance = prepLine(trimLastLine(utterance)))) ret;
72  
  print(">>  " + utterance);
73  
  onUtterance(utterance);
74  
}
75  
76  
svoid onUtterance(S utterance) {
77  
  lastUtterance = utterance;
78  
  mapVoidMulti(receivers, ll(utterance));
79  
  answer = guessAnswer(utterance);
80  
  S both = utterance;
81  
  setTextAndEnabled(btnSay, empty(answer) ? pair("Say", false) : pair("Say " + quote(answer), true));
82  
  if (nempty(answer)) {
83  
    both += "\n\n" + answer;
84  
    logQuotedWithTime("guessLog", both);
85  
  }
86  
  
87  
  setFrameTitle(l, both.replaceAll("\n+", " > ");
88  
  setText(l, jlabel_textAsHTML_center(both));
89  
}
90  
91  
svoid say(S s) { speakInRecognizerLanguage(s); }
92  
93  
answer {
94  
  s = lookupOrKeep(mechMapCI("Mishearing corrections"), s);
95  
  
96  
  if (matchStartOneOf(s, m, "phone say"))
97  
    catSayOnPhone($1);
98  
  else if (matchStartOneOf(s, m, "sag", "say", "please say"))
99  
    thread { say($1); }
100  
  if "thanks"
101  
    say("sure");
102  
  if "higher"
103  
    infoBox("Get higher?");
104  
  if "font size *"
105  
    ret "OK" with revalidate(setFontSize(parseInt($1), l));
106  
  if "foreground|fairground|vordergrund"
107  
    ret "OK" with activateFrame(l);
108  
  if "background|hintergrund"
109  
    ret "OK" with minimizeFrame(l);
110  
  if "standard function|standard functions|steroid function|standard fashion" // Google keeps misunderstanding
111  
    ret "OK" with sfBot_doIt();
112  
  if "move mouse away"
113  
    ret "OK" with moveMouseToTopRightCorner();
114  
  if "mouse to center|mouse to centre|most two centre"
115  
    ret "OK" with moveMouseToScreenCenter();
116  
  if "split ..."
117  
    ok split();
118  
  if "unsplit ..."
119  
    ok scp.setComponent(catComponent);
120  
  if "show list of ..."
121  
    ok openList($1);
122  
  if "add to list"
123  
    addToList(lastUtterance);
124  
}
125  
126  
svoid sayIt {
127  
  fS a = answer;
128  
  if (empty(a)) ret;
129  
  print("Saying: " + a);
130  
  thread { speakInRecognizerLanguage(a); }
131  
  onUtterance(a);
132  
}
133  
134  
sS guessAnswer(S s) {
135  
  new Matches m;
136  
  if (find3plusRestsOneOf(s, m, "Ich heiße", "Ich bin", "Mein Name ist", "Man nennt mich", "Ich werde * genannt", "nennen mich", "nennt mich", "nenne mich"))
137  
    if (eqic($2, "Stefan"))
138  
      ret gudrun_later("Stefan ist ein Programmierer");
139  
    else
140  
      ret gudrun_later("Hallo " + $2);
141  
142  
  try answer mL_answerWithCodeFragments("Witty Answers", s);
143  
  try answer mL_answerWithCodeFragments(s);
144  
  try answer consequentsMap.get(s);
145  
  try answer precedentsMap.get(s);
146  
  null;
147  
}
148  
149  
svoid split(JComponent c) {
150  
  otherComponent = c;
151  
  split();
152  
}
153  
154  
svoid split {
155  
  if (otherComponent == null) otherComponent = jpanel();
156  
  scp.setComponent(setSplitPaneDelayed(0.5, jhsplit(catComponent, otherComponent)));
157  
  activateFrame(scp);
158  
}
159  
160  
static JTextArea openList(S name) {
161  
  currentList = name;
162  
  JTextArea ta;
163  
  split(jsection(name, ta = jtextarea(lines(mLOrCreate(name)))));
164  
  ret ta;
165  
}
166  
167  
svoid addToList(fS s) {
168  
  if (empty(s) || empty(currentList)) ret;
169  
  mechAppendQueue(r {
170  
    appendToMechList(currentList, s);
171  
    final JTextArea ta = openList(currentList);
172  
    if (ta != null) awt {
173  
      int h = ta.getHeight();
174  
      ta.scrollRectToVisible(new Rectangle(0, h-1, 1, 1));
175  
    }
176  
  });
177  
}
178  
179  
svoid startListening {
180  
  setListening_active(true);
181  
}
182  
183  
svoid startOrStop {
184  
  setListening_active(!listening);
185  
}
186  
187  
svoid setListening_active(bool b) {
188  
  setListening(b);
189  
  sendInNewThread("Chrome Speech.", b ? "start recognition" : "stop recognition");
190  
}
191  
192  
svoid setListening(final bool b) {
193  
  swing {
194  
    listening = b;
195  
    setText(btnStartStop, listening ? "Stop listening" : "Start listening");
196  
    if (b) {
197  
      removeTrayIcon(trayIcon);
198  
      trayIcon = null;
199  
    } else if (trayIcon == null)
200  
      trayIcon = installTrayIcon(#1101203, "Start listening", r { startListening() },
201  
        menuItem("Start Listening", f startListening),
202  
        menuItem("Exit", f stopKatze));
203  
  }
204  
}
205  
206  
svoid stopKatze {
207  
  killProgram(#1013936);
208  
  cleanKillVM();
209  
}

Author comment

Began life as a copy of #1014131

download  show line numbers  debug dex  old transpilations   

Travelled to 18 computer(s): anzfkgltbrzg, aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, nruopgnwybjz, oikenkqtkxpn, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, vpdwwinrgdga

No comments. add comment

Snippet ID: #1014135
Snippet name: VisualKatze 5
Eternal ID of this version: #1014135/121
Text MD5: c96728a4fc7765755e502a46a920f3de
Transpilation MD5: 0bed2db11fffb9d730e298b0fe93bcce
Author: stefan
Category: javax / gui
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-21 10:02:36
Source code size: 6667 bytes / 209 lines
Pitched / IR pitched: No / No
Views / Downloads: 700 / 62659
Version history: 120 change(s)
Referenced in: [show references]