-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadd-on.js
More file actions
517 lines (476 loc) · 17.1 KB
/
add-on.js
File metadata and controls
517 lines (476 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
"use strict"; // for added security
function heardAddOns(heard) {
// this should overwrite the function in brain.js
if (heardNumberGuessGame(heard)) return true;
if (heardTranslator(heard)) return true;
if (heardProgram(heard)) return true;
if (heardGetSnippet(heard)) return true;
return false;
}
let numberToGuess;
function heardNumberGuessGame(heard) {
if (heard.includes("number guessing game")) {
currentConversationType = "number guessing game";
numberToGuess = Math.floor(Math.random()*100 + 1);
say("I'm thinking of a number between 1 and 100. Guess my number.");
return true;
} else if (currentConversationType === "number guessing game") {
if (didHear(heard,['no',"lets stop playing","never mind"])) {
// "turn off" the game
currentConversationType = '';
say('Okay. What would you like to do instead?');
createSuggestionMessage(["What can you do?"]);
} else if (isNaN(heard)) {
say('Please give me a number.');
createSuggestionMessage(["Let's stop playing."]);
} else {
if (parseInt(heard) === numberToGuess) {
say("You got it! My number was " + numberToGuess + '. What would you like to do now?');
// "turn off" the game
currentConversationType = '';
} else if (parseInt(heard) < numberToGuess) {
say("It's higher than " + heard + '. Try another number.');
} else if (parseInt(heard) > numberToGuess) {
say("It's lower than " + heard + '. Try another number.');
}
}
return true;
}
// otherwise
return false;
}
let firstTranslation = true;
function heardTranslator(heard) {
if (heard.startsWith('translate ')) {
let english = heard.replace('translate ', '');
// allow for context
if (currentConversationTopic !== '' && english === "that") {
english = currentConversationTopic;
}
// allow for delay
if (firstTranslation) {
firstTranslation = false;
say("Please wait for the API to respond.");
}
currentConversationType = "translate";
currentConversationTopic = english;
let url = "https://coglang-translator.glitch.me/" + english;
$.getJSON(url, function(translations) {
if (translations.long) {
var long = translations.long;
var short = translations.short;
sayWithEsp(short);
} else {
say("Sorry, I couldn't find a translation for that.");
}
});
return true; // did hear, just need to wait for web API response
}
// otherwise
return false;
}
function spellLikeEsp(sentence) {
let converted = '';
let letter = '';
for (var i in sentence) {
letter = sentence[i];
if (letter === 'c') {
converted += 'sx';
} else if (letter === 'j') {
converted += 'jx';
} else if (letter === 'y') {
converted += 'j';
} else if (letter === 'h') {
converted += 'hx';
} else {
converted += letter;
}
}
return converted;
}
function sayWithEsp(sentence) {
if (sentence != '') {
if (!('webkitSpeechRecognition' in window)) {
// in case not using chrome
responsiveVoice.speak(spellLikeEsp(sentence), 'Esperanto Male');
} else {
if (typeof clicked == 'function') clicked(); // for CodePen demo
recognition.stop();
responsiveVoice.speak(spellLikeEsp(sentence), 'Esperanto Male', {onend: listenAgain});
}
updateMessageLog(sentence, 'LUI');
document.getElementById("input").focus(); // put cursor back
}
}
let pointer = 0;
let pointerPrev = 0;
let programString = "\n"; // so always responds to "new line"
let programStringPrev = "\n";
let variableList = {}; // so can notify user to initialize or create
function heardProgram(heard) {
if (didHear(heard,["let's program", 'lets program', 'program', 'lets code', "let's code", 'code'],'starts with')) {
currentConversationType = 'program';
$('#programming-area').css('visibility','visible');
say('What would you like to program in JavaScript?');
createSuggestionMessage(['I need code for quicksort.']);
return true;
} else if (heard.includes("your code")) {
currentConversationType = 'program';
showAddOnCode();
$('#programming-area').css('visibility','visible');
say("Here you go. Displaying add-on code.");
return true;
} else if (currentConversationType === 'program') {
if (didHear(heard,['stop',"let's stop","never mind"],'starts with')) {
// exit from programming
currentConversationType = '';
$('#programming-area').css('visibility','collapse');
$('#programming-area').css('display','none');
say('Okay. What would you like to do instead?');
createSuggestionMessage(["What can you do?"]);
} else if (didHear(heard,['clear everything','delete everything'])) {
// clear and
// exit from programming
currentConversationType = '';
programStringPrev = "";
pointerPrev = 0;
programString = "";
pointer = 0;
$('#programming-area').text('');
$('#programming-area').css('visibility','collapse');
$('#programming-area').css('display','none');
say('Okay. What would you like to do instead?');
createSuggestionMessage(["What can you do?"]);
} else {
// check if any change made in case need to do an action like heardGetSnippet
let tempCode = programString;
// ******* TODO: program different things *******
program(heard);
if (tempCode === programString) return false;
}
return true;
}
// otherwise
return false;
}
function showAddOnCode() {
prepForUndo();
// get add-on.js text
var client = new XMLHttpRequest();
client.open('GET', 'https://rawgit.com/hchiam/language-user-interface/master/add-on.js');
client.onreadystatechange = function() {
programString = client.responseText;
updateProgrammingAreaDisplay();
}
client.send();
}
function makeSaferForHTML(string) {
return string.replace(/[,\\\/#!?$%\^&\*;:{}<>=`"~()]/g,'');
}
function updateProgrammingAreaDisplay() {
// to avoid managing <br/> in strings and for easier use of a pointer
$('#programming-area').html(programString.replace(/\n/g, "<br/>"));
}
function programInsert(what, where) {
prepForUndo();
// for easier use of a pointer
let before = programString.substr(0,where);
let after = programString.substr(where);
programString = before + what + after;
updateProgrammingAreaDisplay();
}
function userEditedCode() {
programInterfaceToString();
}
function programInterfaceToString() {
prepForUndo();
// .text() removes new lines, so have to do this instead:
programString = $('#programming-area').html()
.replace("<br>", '\n')
.replace('<span id="programming-area" style="visibility: visible;" contenteditable="true">')
.replace("<br></span>");
}
// affect interface.html and
// say('...');
function program(heard) {
loop(heard);
comment(heard);
notify(heard); // -> alert(...)
variableCreate(heard); // create variable --> "let "
variableUse(heard); // variable ... ... --> "..._... "
// TODO: ifStatement(heard); // if --> "if(" (for composing statements)
// TODO: functionCreate(heard); // create function ... --> "function ...(*) {\n\n}" (* = pointer)
// TODO: functionUse(heard); // function ... --> "...(" (for composing statements)
newLine(heard);
specialCharacters(heard);
escapeNextBrace(heard);
runProgram(heard);
undo(heard);
useSnippet(heard);
hideProgrammingArea(heard);
}
function loop(heard) {
let matches = heard.match(RegExp("loop( backwards?)? through (.+)( backwards?)?"));
if (matches) {
currentConversationTopic = matches[0];
let what = matches[2];
let loop = "for (";
// check if looping backwards/forwards
if (matches[1] || matches[3]) {
// backwards
loop += "let i=" + makeSaferForHTML(what.replace(/ /g,'_')) + ".length-1; i>=0; i++) {\n\n}";
} else {
// forwards
loop += "let i=0; i<" + makeSaferForHTML(what.replace(/ /g,'_')) + ".length; i++) {\n\n}";
}
programInsert(loop,pointer);
pointer += loop.length - 2; // stay a line inside last brace
} else if (heard.includes('loop')) {
say("Please say something like: 'loop through object', or simply 'for'.");
} else if (heard === 'for') {
let forStart = "for (";
programInsert(forStart,pointer);
pointer += forStart.length;
}
}
function comment(heard) {
let matches = heard.match(RegExp("comment (.+)"));
if (matches) {
currentConversationTopic = 'comment';
let comment = "// " + makeSaferForHTML(matches[1]);
programInsert(comment,pointer);
pointer += comment.length;
} else if (heard === 'comment') {
let comment = "// ";
programInsert(comment,pointer);
pointer += comment.length;
}
}
function notify(heard) {
let matches = heard.match(RegExp("alert (.+)"));
if (matches) {
currentConversationTopic = matches[0];
let alert = "alert(" + makeSaferForHTML(matches[1]) + ");";
programInsert(alert,pointer);
pointer += alert.length;
} else if (heard === 'alert') {
let alert = "alert(";
programInsert(alert,pointer);
pointer += alert.length;
}
}
function variableCreate(heard) {
let matches = heard.match(RegExp("^(create variable|let) (.+)")); // ^ to disambiguate from variableUse(heard)
if (matches) {
let variable_Expression = handleMathOperators(makeSaferForHTML(matches[2].replace(/ /g,'_')));
currentConversationTopic = 'create variable ' + variable_Expression;
let letVariable = "let " + variable_Expression;
programInsert(letVariable,pointer);
pointer += letVariable.length;
// get and track variable name
let variableName = getJustVariableName(variable_Expression);
trackVariable(variableName);
}
}
function variableUse(heard) {
let matches = heard.match(RegExp("^variable (.+)")); // ^ to disambiguate from variableCreate(heard)
if (matches) {
let variable_Expression = handleMathOperators(makeSaferForHTML(matches[1].replace(/ /g,'_')));
currentConversationTopic = 'variable ' + variable_Expression;
// get just the variable name
let variableName = getJustVariableName(variable_Expression);
// check if did not create/initialize variable name already
if (!variableExistsAlready(variableName)) {
say("Create that variable first: say 'create variable ...'.");
} else {
programInsert(variable_Expression,pointer);
pointer += variable_Expression.length;
}
}
}
function handleMathOperators(str) {
// '_' because must handle after makeSaferForHTML()
let modified = str;
modified = modified.replace(/_greater_than_or_equal_to_/g,' >= ')
.replace(/_less_than_or_equal_to_/g,' <= ')
.replace(/_equals_/g,' = ')
.replace(/_equal_to_/g,' = ')
.replace(/_equal_/g,' = ')
.replace(/_greater_than_/g,' > ')
.replace(/_less_than_/g,' < ')
.replace(/_plus_plus_/g,'++ ')
.replace(/_plus_/g,' + ')
.replace(/_minus_minus_/g,'-- ')
.replace(/_minus_/g,' - ')
.replace(/_divided_by_/g,' / ')
.replace(/_multiplied_by_/g,' * ')
.replace(/_modulus_/g,' % ')
.replace(/_and_/g,' && ')
.replace(/_or_/g,' || ')
.replace(/_not_/g,' ~');
return modified;
}
function getJustVariableName(letVar) {
// to ensure put name (not expression) in variableList
// e.g.: "create variable a b c equals 1 plus 2" --> "a_b_c"" (not "a_b_c = 1 + 2")
let variableName = letVar.replace(/^variable /,'').replace(/^let /,'');
if (variableName.includes(' ')) variableName = variableName.substring(0,variableName.indexOf(' '));
return variableName;
}
function trackVariable(varName) {
variableList[varName] = true; // TODO: future: = line number, to check scope
}
function variableExistsAlready(varName) {
return varName in variableList;
}
function newLine(heard) {
if (heard === 'new line' || heard === 'carriage return') {
programInsert('\n',pointer);
pointer += 1;
}
}
function specialCharacters(heard) {
let s = '';
if (heard === 'semicolon') {
s = '; ';
} else if (heard === 'colon') {
s = ': ';
} else if (heard === 'dot' || heard === 'period') {
s = '.';
} else if (heard === 'dash') {
s = '-';
} else if (heard === 'underscore') {
s = '_';
} else if (heard === 'apostrophe') {
s = "'";
} else if (heard === 'double apostrophe') {
s = '""';
} else if (heard === 'bracket' || heard === 'opening bracket') {
s = '(';
} else if (heard === 'close bracket' || heard === 'closing bracket') {
s = ')';
} else if (heard === 'curly brace' || heard === 'curly bracket' || heard === 'opening brace' || heard === 'opening curly bracket') {
s = '{';
} else if (heard === 'close curly brace' || heard === 'closing curly brace' || heard === 'closing curly bracket' || heard === 'closing brace') {
s = '}';
} else if (heard === 'square bracket' || heard === 'opening square bracket') {
s = '[';
} else if (heard === 'close square bracket' || heard === 'closing square bracket') {
s = ']';
}
// check condition to avoid double-updating and affecting undo
if (s !== '') programInsert(s,pointer);
pointer += s.length;
}
function escapeNextBrace(heard) {
if (heard.startsWith('escape brace')) {
pointer = programString.substring(0,pointer).length + programString.substring(pointer).indexOf("}\n") + 2;
say("Escaped a brace.");
}
}
function runProgram(heard) {
if (heard === 'run program' || heard === 'run code') {
say("Running program.");
eval(programString);
}
}
function prepForUndo() {
// for easy undo:
programStringPrev = programString;
pointerPrev = pointer;
}
function undo(heard) {
if (heard === 'undo') {
// swap pointers
let temp = pointer;
pointer = pointerPrev;
pointerPrev = temp;
// (to avoid managing <br/> in strings and for easier use of a pointer)
$('#programming-area').html(programStringPrev.replace(/\n/g, "<br/>"));
// swap program strings
programStringPrev = programString;
programInterfaceToString();
}
}
let codeSnippet = '';
function heardGetSnippet(heard) {
let matches = heard.match(RegExp("i need (some |a )?code (snippet )?for (.+)"));
if (matches) {
let searchWords = matches[3];
currentConversationTopic = 'get snippet';
let url = "https://sourcefetch-server.glitch.me/fetch/?q=" + searchWords;
say("Searching.");
$.getJSON(url, function(response) {
if (response.code) {
codeSnippet = response.code;
// alert(response.code);
say("Here's what I found.");
// set up style for LUI speaking
let id = ' id="log-LUI"';
let idTimeStamp = ' id="log-time-stamp"';
// create message with buttons
let timeStamp = '<br><small' + idTimeStamp + '>' + ' - ' + getTime() + '</small>';
let buttonCopyCode = '<button onclick="saveCode()">Save Code</button>'
let sentenceToShow = codeSnippet;
let nextMessage = '<p' + id + '>' + buttonCopyCode + '<br>' + sentenceToShow + '<br>' + timeStamp + '</p>';
let logSoFar = document.getElementById('messageLog').innerHTML;
// show message with buttons
document.getElementById('messageLog').innerHTML = nextMessage + logSoFar;
if (currentConversationType === 'program') {
createSuggestionMessage(["Let's use that."]);
}
} else {
say("Sorry, I couldn't find an example code snippet for that.");
// be proactive and open a search results page
searchQuestion(searchWords + " in javascript");
}
});
return true; // did hear, just need to wait for web API response
}
// otherwise
return false;
}
function saveCode() {
try {
var filename = 'sample_code' + ".js";
var temporaryElem = document.createElement("a");
temporaryElem.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(codeSnippet));
temporaryElem.setAttribute("download", filename);
if (document.createEvent) {
var event = document.createEvent("MouseEvents");
event.initEvent("click", true, true);
temporaryElem.dispatchEvent(event);
}
else {
temporaryElem.click();
}
} catch(err) {
// if the previous code returns an error or isn't supported, try using this instead:
var content = fullOutputString;
window.open('data:text/txt;charset=utf-8,' + escape(content), 'newdoc');
}
}
function cleanupString(name) {
return name
.replace(' ','_')
.replace(/[.,;:'"\/\\<>?!]/g, '');
}
function useSnippet(heard) {
if (currentConversationTopic === 'get snippet') {
if (didHear(heard,["let's use that",'lets use that'])) {
pointer += codeSnippet.length + 2; // by default, continue after the code
programInsert('\n'+codeSnippet+'\n',pointer);
say('Done.');
}
}
}
function hideProgrammingArea(heard) {
if (didHear(heard,['hide screen','hide programming area'])) {
// exit from programming
currentConversationType = '';
$('#programming-area').css('visibility','collapse');
$('#programming-area').css('display','none');
say('Okay. What would you like to do instead?');
createSuggestionMessage(["What can you do?"]);
}
}