/
index.html
360 lines (296 loc) · 16.4 KB
/
index.html
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
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<script src="jquery-3.3.1.min.js"></script>
<script src="p5.min.js"></script>
<title>word play</title>
<style>
body {background-color: rgb(5, 5, 5);}
button:focus { outline: 0; }
</style>
</head>
<body>
<div class="speech_output"></div>
<div class="anagram_output"></div>
<div class="to_delete"></div>
<button id="btn" style="color: white; background: black; outline: none;">Listen</button>
<div id="sketch-holder"></div>
<script>
//NOTES: There is a bug where anagramica returns a scrambled version of the inputted word and the inputted word in else where in the returned JSON other than [0].
// Bug with invariants, this POS type retuns items such as Kansas (noun) deeper in the response.
// Good invariant to include is "um"
// det_pronoun example "each"
// current position of setting random variables happens each time a response is recieved
//the word dicionary breaks anagram API
//HUGE BUG, ANY WORD OVER 10 LETTERS WILL NOT GET RESPONSE FROM ANAGRAM API
var windowWidth = window.innerWidth;
var windowHeight = window.innerHeight;
var verb = new Array();
var adjective = new Array();
var noun = new Array();
var conjunction = new Array();
var determiner = new Array();
var preposition = new Array();
var adverb = new Array();
var invariant = new Array();
var interjection = new Array();
var det_pronoun = new Array();
var pronoun = new Array();
var participle = new Array();
var numeral = new Array();
var thisResponse = [];
var holdSentences = [];
var allWordsFormed = [];
var numberSentences = 5;
var randSentence; //generate random number based on the amount of sentence structures formed
var word, currentText;
var someResult;
var testWord;
var randNoun1, randNoun2, randVerb1, randVerb2, randDet, randAdverb;
var proxy = 'https://cors-anywhere.herokuapp.com/';
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent;
var recognition = new SpeechRecognition();
recognition.lang = 'en-US';
var clickCount = 0;
document.getElementById("btn").addEventListener("click", function() {
clickCount+=1;
randSentence = Math.floor(Math.random() * Math.floor(numberSentences + 1));
console.log(clickCount);
});
document.getElementById("btn").addEventListener("click", function(){
recognition.start();
console.log('Im listening...');
//TESTING
// if (clickCount == 1) {
// word = "daughter";
// } else if (clickCount == 2) {
// word = "people";
// } else if (clickCount == 3) {
// word = "shrink";
// } else if (clickCount == 4) {
// word = "turtle";
// }
});
recognition.onresult = function(event) {
var last = event.results.length - 1;
var word = event.results[last][0].transcript;
console.log(word);
var myURL = 'http://www.anagramica.com/all/:' + word; //TESTING
$.ajax({
url: proxy + myURL,
fail: function(data) {
console.log('this didnt work' + data);
},
complete:function(data){
console.log(data.responseJSON.all);
for (var i = data.responseJSON.all.length - 1; i >= 0; i--) {
if ((data.responseJSON.all[i].length == 1) && (data.responseJSON.all[i] !== "a") || (data.responseJSON.all[i] == "kc")) {
console.log(data.responseJSON.all[i]);
someResult = data;
someResult.responseJSON.all.splice(i,1);
}
}
for (var p = 0; p < someResult.responseJSON.all.length; p++) {
allWordsFormed.push(someResult.responseJSON.all[p]);
}
var APIkey = "dict.1.1.20181124T041325Z.e581716fc8d92efb.c64dc11cd7db2b6a9ae026ee97a33f55e62077c5";
$.each(someResult.responseJSON.all, function(index, val){
var defURL = "https://dictionary.yandex.net/api/v1/dicservice.json/lookup?key=" + APIkey + "&lang=en-en&text=" + val;
thisResponse.push(
$.ajax({
url: defURL,
success: function(data) {
console.log(data);
}
})
);
});
$.when.apply($, thisResponse).then(function(){
for (var i = 0; i < thisResponse.length; i++) {
//console.log(thisResponse[i].responseJSON.def[0].pos);
if (thisResponse[i].responseJSON.def.length !== 0) {
if (thisResponse[i].responseJSON.def[0].pos == 'noun') {
noun.push(thisResponse[i].responseJSON.def[0].text);
} else if (thisResponse[i].responseJSON.def[0].pos == 'verb') {
verb.push(thisResponse[i].responseJSON.def[0].text);
} else if (thisResponse[i].responseJSON.def[0].pos == 'adjective') {
adjective.push(thisResponse[i].responseJSON.def[0].text);
} else if (thisResponse[i].responseJSON.def[0].pos == 'conjunction') {
conjunction.push(thisResponse[i].responseJSON.def[0].text);
} else if (thisResponse[i].responseJSON.def[0].pos == 'noun') {
noun.push(thisResponse[i].responseJSON.def[0].text);
} else if (thisResponse[i].responseJSON.def[0].pos == 'determiner') {
determiner.push(thisResponse[i].responseJSON.def[0].text);
} else if (thisResponse[i].responseJSON.def[0].pos == 'preposition') {
preposition.push(thisResponse[i].responseJSON.def[0].text);
} else if (thisResponse[i].responseJSON.def[0].pos == 'adverb') {
adverb.push(thisResponse[i].responseJSON.def[0].text);
} else if (thisResponse[i].responseJSON.def[0].pos == 'invariant') {
invariant.push(thisResponse[i].responseJSON.def[0].text);
} else if (thisResponse[i].responseJSON.def[0].pos == 'interjection') {
interjection.push(thisResponse[i].responseJSON.def[0].text);
} else if (thisResponse[i].responseJSON.def[0].pos == 'determiner / pronoun') {
det_pronoun.push(thisResponse[i].responseJSON.def[0].text);
} else if (thisResponse[i].responseJSON.def[0].pos == 'pronoun') {
pronoun.push(thisResponse[i].responseJSON.def[0].text);
} else if (thisResponse[i].responseJSON.def[0].pos == 'participle') {
participle.push(thisResponse[i].responseJSON.def[0].text);
} else if (thisResponse[i].responseJSON.def[0].pos == 'numeral') {
numeral.push(thisResponse[i].responseJSON.def[0].text);
} else if (thisResponse[i].responseJSON.def[0].pos == 'undefined'){}
else {
console.log('idk what pos that is: ' + thisResponse[i].responseJSON.def[0].pos);
}
}
}
//create random numbers and add sentences to an array here
randVerb1 = Math.floor(Math.random() * Math.floor(verb.length));
randVerb2 = Math.floor(Math.random() * Math.floor(verb.length));
randNoun1 = Math.floor(Math.random() * Math.floor(noun.length));
randNoun2 = Math.floor(Math.random() * Math.floor(noun.length));
randDet = Math.floor(Math.random() * Math.floor(determiner.length));
randAdverb = Math.floor(Math.random() * Math.floor(adverb.length));
if (clickCount%2 == 0) {
if ((noun.length !== 0) && (determiner.length !== 0) && (verb.length !== 0)) {
holdSentences.push(sentenceStruc6(verb[randVerb1], determiner[randDet], noun[randNoun2], verb[randVerb2]));
} else if ((adverb.length !== 0) && (verb.length !== 0) && (noun.length !== 0)) {
holdSentences.push(sentenceStruc3(adverb[randAdverb], verb[randVerb1], noun[randNoun1], verb[randVerb2]));
}
} else {
if ((adverb.length !== 0) && (verb.length !== 0) && (noun.length !== 0)) {
holdSentences.push(sentenceStruc3(adverb[randAdverb], verb[randVerb1], noun[randNoun1], verb[randVerb2]));
} else if ((noun.length !== 0) && (determiner.length !== 0) && (verb.length !== 0)) {
holdSentences.push(sentenceStruc6(verb[randVerb1], determiner[randDet], noun[randNoun2], verb[randVerb2]));
}
}
thisResponse = [];
}).fail(function() {
console.log('fail');
})
}
});
}
recognition.onspeechend = function() {
recognition.stop();
}
recognition.onnomatch = function(event) {
output.textContent = 'Im not sure what you said.';
}
recognition.onerror = function(event) {
output.textContent = 'There has been an error.';
}
function setup() {
var canvas = createCanvas(windowWidth,windowHeight);
canvas.parent('sketch-holder');
}
function draw() {
background(5);
var posX = 10;
var posY = 20;
if (allWordsFormed) {
for (var j = allWordsFormed.length; j > 0; j--) {
textSize(16);
fill(60);
text(allWordsFormed[j], posX, posY);
posY = posY + 15;
if (posY > height) {
posX = posX + 65;
posY = 20;
}
}
var xPos = 10;
var yPos = 45;
//could color certain words based on if they come from the input here by checking if the
if (holdSentences) {
for (var q = 0; q < holdSentences.length; q++) {
fill(255, 252, 247);
textFont('Helvetica');
textSize(50);
text(holdSentences[q], xPos, yPos);
if (yPos >= windowHeight) {
yPos = -10;
xPos = xPos + 500;
}
//xPos = holdSentences[j].length*30 + 20; //holdSentence[i].length will give length of the sentence which you can use to scale the position of the next sentence so they do not overlap.
xPos = xPos;
yPos = yPos + 55;
}
}
}
fill(51);
}
function sentenceStruc1(noun1, verb, noun2) {
return noun1 + " " + verb + " " + noun2;
}
function sentenceStruc2(determiner, noun, verb) {
return determiner + " " + noun + " " + verb;
}
function sentenceStruc3(adverb, verb1, noun, verb2) {
return adverb + " " + verb1 + " " + noun + " " + verb2;
}
function sentenceStruc4(adverb, noun) {
return adverb + " " + noun;
}
function sentenceStruc5(determiner, noun) {
return determiner + " " + noun;
}
function sentenceStruc6(verb1, determiner, noun, verb2) {
return verb1 + " " + determiner + " " + noun + " " + verb2;
}
//look into using $.each here
// for (var j = 1; j < someResult.responseJSON.all.length; j++) {
// var APIkey = "dict.1.1.20181124T041325Z.e581716fc8d92efb.c64dc11cd7db2b6a9ae026ee97a33f55e62077c5";
// var defURL = "https://dictionary.yandex.net/api/v1/dicservice.json/lookup?key=" + APIkey + "&lang=en-en&text=" + someResult.responseJSON.all[j];
//
// $.ajax({
// url: defURL,
// complete:function(newData){
// if (newData.responseJSON.def.length == 1) {
// console.log(newData.responseJSON.def[0].pos);
//
// if (newData.responseJSON.def[0].pos == 'verb') {
// verb.push(newData.responseJSON.def[0].text);
// randVerb1 = Math.floor(Math.random() * Math.floor(verb.length));
// randVerb2 = Math.floor(Math.random() * Math.floor(verb.length));
// } else if (newData.responseJSON.def[0].pos == 'adjective') {
// adjective.push(newData.responseJSON.def[0].text);
// } else if (newData.responseJSON.def[0].pos == 'conjunction') {
// conjunction.push(newData.responseJSON.def[0].text);
// } else if (newData.responseJSON.def[0].pos == 'noun') {
// noun.push(newData.responseJSON.def[0].text);
// randNoun1 = Math.floor(Math.random() * Math.floor(noun.length));
// randNoun2 = Math.floor(Math.random() * Math.floor(noun.length));
// } else if (newData.responseJSON.def[0].pos == 'determiner') {
// determiner.push(newData.responseJSON.def[0].text);
// randDet = Math.floor(Math.random() * Math.floor(determiner.length));
// } else if (newData.responseJSON.def[0].pos == 'preposition') {
// preposition.push(newData.responseJSON.def[0].text);
// } else if (newData.responseJSON.def[0].pos == 'adverb') {
// adverb.push(newData.responseJSON.def[0].text);
// randAdverb = Math.floor(Math.random() * Math.floor(adverb.length));
// } else if (newData.responseJSON.def[0].pos == 'invariant') {
// invariant.push(newData.responseJSON.def[0].text);
// } else if (newData.responseJSON.def[0].pos == 'interjection') {
// interjection.push(newData.responseJSON.def[0].text);
// } else if (newData.responseJSON.def[0].pos == 'determiner / pronoun') {
// det_pronoun.push(newData.responseJSON.def[0].text);
// } else if (newData.responseJSON.def[0].pos == 'pronoun') {
// pronoun.push(newData.responseJSON.def[0].text);
// } else if (newData.responseJSON.def[0].pos == 'participle') {
// participle.push(newData.responseJSON.def[0].text);
// } else if (newData.responseJSON.def[0].pos == 'numeral') {
// numeral.push(newData.responseJSON.def[0].text);
// } else {
// console.log('idk what pos that is');
// }
// }
//
// }
// });
// }
</script>
</body>
</html>