-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
70 lines (59 loc) · 1.97 KB
/
script.js
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
const SpeechRecognition = webkitSpeechRecognition;
let recording = false;
const getSpeech = () => {
const recognition = new SpeechRecognition();
recognition.lang = 'en-US';
recognition.start();
recognition.continuous = false;
recognition.interimResults = true;
recording = true;
console.log('started rec');
recognition.onresult = event => {
const speechResult = event.results[0][0].transcript;
console.log('result: ' + speechResult);
console.log('confidence: ' + event.results[0][0].confidence);
// document.querySelector('#speech-div').textContent = speechResult;
let speechArray = speechResult.split(' ');
getEmoji(speechArray);
};
recognition.onend = () => {
// for "endless" mode, comment out the next line and uncomment getSpeech()
// recognition.stop();
getSpeech();
};
recognition.onerror = event => {
console.log('something went wrong: ' + event.error);
};
};
const getEmoji = speechArray => {
// let url = `https://unpkg.com/emoji.json/emoji.json`;
let url = 'emoji.json';
fetch(url)
.then(response => response.json())
.then(data => {
for (j=0; j<speechArray.length; j++){
let emojiWord=" ";
for (i=0; i<data.length; i++){
keyArray = data[i].keywords.split(', ');
console.log(keyArray);
for (k=0; k<keyArray.length; k++){
if (speechArray[j]==keyArray[k]){
emojiWord = data[i].char;
}
//yields about 1600 emojis
//if (data[i].keywords == "flag"){
// console.log(data[i].char);
// document.getElementById("emoji-div").innerHTML+=data[i].char;
}
}
document.getElementById("emoji-div").innerHTML+=emojiWord;
//render html here, will happen once per word
}
});
}
document.querySelector('#my-button').onclick = () => {
document.getElementById("emoji-div").innerHTML=' ';
document.getElementById('my-button').id = 'my-button-clicked';
console.log('clickity');
getSpeech();
};