Created
December 4, 2020 07:58
-
-
Save AsedaDeveloper/d2c8daf1579903a39822af7fb11960db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// UI comp | |
const startBtn = document.createElement("button"); | |
startBtn.innerHTML = "Start listening"; | |
const result = document.createElement("div"); | |
const processing = document.createElement("p"); | |
document.write("<body><h1>My Siri</h1><p>Give it a try with 'hello', 'how are you', 'what's your name', 'what time is it', 'stop', ... </p></body>"); | |
document.body.append(startBtn); | |
document.body.append(result); | |
document.body.append(processing); | |
// speech to text | |
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; | |
let toggleBtn = null; | |
if (typeof SpeechRecognition === "defined") { | |
startBtn.remove(); | |
result.innerHTML = "<b>Browser suppoerts support Speech API"; | |
} else { | |
const recognition = new SpeechRecognition(); | |
recognition.continuous = true; | |
recognition.interimResults = true; | |
recognition.onresult = event => { | |
const last = event.results.length - 1; | |
const res = event.results[last]; | |
const text = res[0].transcript; | |
if (res.isFinal) { | |
processing.innerHTML = "processing ...."; | |
const response = process(text); | |
const p = document.createElement("p"); | |
p.innerHTML = `You said: ${text} </br>Siri said: ${response}`; | |
processing.innerHTML = ""; | |
result.appendChild(p); | |
// text to speech | |
speechSynthesis.speak(new SpeechSynthesisUtterance(response)); | |
} else { | |
processing.innerHTML = `listening: ${text}`; | |
} | |
} | |
let listening = false; | |
toggleBtn = () => { | |
if (listening) { | |
recognition.stop(); | |
startBtn.textContent = "Start listening"; | |
} else { | |
recognition.start(); | |
startBtn.textContent = "Stop listening"; | |
} | |
listening = !listening; | |
}; | |
startBtn.addEventListener("click", toggleBtn); | |
} | |
// processor | |
function process(rawText) { | |
let text = rawText.replace(/\s/g, ""); | |
text = text.toLowerCase(); | |
let response = null; | |
switch(text) { | |
case "hello": | |
response = "hi, how are you doing?"; break; | |
case "what'syourname": | |
response = "My name's Siri."; break; | |
case "howareyou": | |
response = "I'm good."; break; | |
case "whatdayisit": | |
response = "Monday, Tuesday."; break; | |
} | |
if (!response) { | |
window.open(`http://google.com/search?q=${rawText.replace("search", "")}`, "_blank"); | |
return `I found some information for ${rawText}`; | |
} | |
dont return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment