์์ฑ์ธ์์ ํตํด ํ๋ฉด์ ์ถ๋ ฅ ํด๋ณด๊ธฐ.
์ด๊ธฐ์ฝ๋
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Speech Detection</title>
</head>
<body>
<div class="words" contenteditable>
</div>
<script>
window.SpeechRecognition = window.SpeechRecongnition || window.webkitSpeechRecognition;
</script>
</body>
<style>
html{
font-size: 10px;
}
body{
background: #ffc600;
font-family: 'helvetica neue';
font-weight: 200;
font-size: 20px;
}
.words{
max-width: 500px;
margin:50px auto;
background: #fff;
border-radius: 5px;
box-shadow: 10px 10px 0 rgba(0,0,0,0.1);
padding: 1rem 2rem 1rem 5rem;
background: -webkit-gradient(linear, 0 0, 0 100%, from(#d9eaf3), color-stop(4%, #fff)) 0 4px;
background-size: 100% 3rem;
position: relative;
line-height: 3rem;
}
p {
margin: 0 0 3rem;
}
.words:before {
content: '';
position: absolute;
width: 4px;
top: 0;
left: 30px;
bottom: 0;
border: 1px solid;
border-color: transparent #efe4e4;
}
</style>
</html>
์ด๊ธฐํ๋ฉด
Web Speech API์ ์์ฑ ์ธ์ ์ธํฐํ์ด์ค๋ ์ธ์ ์๋น์ค๋ฅผ ์ํ ์ปจํธ๋กค๋ฌ ์ธํฐํ์ด์ค์ด๋ฉฐ, ์ด๊ฒ์ ๋ํ ์ธ์ ์๋น์ค์์ ๋ณด๋ด๋ ์์ฑ ์ธ์ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ๋ค.
์ฃผ์! ๋ธ๋ผ์ฐ์ ํธํ์ฑ์ ์ ํ์ธํ๊ธฐ.
์ฐธ๊ณ :
https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition
์์ฑ ์ธ์ ์ธํฐํ์ด์ค์ ์์ ๊ฒฐ๊ณผ ์์ฑ์ ์ค๊ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํด์ผ ํ๋์ง(์ฐธ) ์ฌ๋ถ๋ฅผ ์ ์ดํฉ๋๋ค(๊ฑฐ์ง).
์ค๊ฐ ๊ฒฐ๊ณผ๋ ์์ง ์ต์ข
์ด ์๋ ๊ฒฐ๊ณผ์
๋๋ค(์: ์์ฑ ์ธ์ ๊ฒฐ๊ณผ.isFinal ์์ฑ์ด false์
๋๋ค).
intervalResults์ ๊ธฐ๋ณธ๊ฐ์ false์ ๋๋ค.
์ฐธ๊ณ :
https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition/interimResults
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.interimResults = true;
window.SpeechRecognition์ ๋ธ๋ผ์ฐ์ ์๋ฐ๋ผ webkit์์ฑ์ ์ ์ฉํ๋ค.
interResult๋ ์ค๊ฐ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋์ง ์ฌ๋ถ๋ฅผ ์ ์ดํจ.
let p = document.createElement('p');
const words = document.querySelector('.words');
words.appendChild(p);
recognition.addEventListener('result', e =>{
console.log(e.results);
const transcript = Array.from(e.results)
.map(result => result[0])
.map(result => result.transcript)
.join('')
p.textContent = transcript;
/*Finished์ ์๋ ๋ถ๋ถ*/
const poopScript = transcript.replace(/poop|poo|shit|dump/gi, '๐ฉ');
p.textContent = poopScript;
if(transcript.includes('์ ๋์ฝ')){
console.log('์ ๋์ฝ22');
}
if(e.results[0].isFinal){
p = document.createElement('p');
words.appendChild(p);
}
});
์์ฑ์ธ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์ด๋ก ๋ณํํด ํ ์คํธ์ ๋ด๊ณ , poop | poo๋ฑ ๋น์์ด๊ฐ ์ค๋ฉด ๐ฉ๋ก ํ์ํจ.
์ ๋์ฝ์ด๋ ๋ง์ด ์ค๋ฉด ์ฝ์์ ์ ๋์ฝ22๊ฐ ์ฐํ๊ฒ ํจ.
๋ง์ด ๋๋๋ฉด p์ ํ ๋ฌธ์ฅ์ผ๋ก ๋ด์ฉ์ ๋ด์ ๋ง์ด ๋๋๋ฉด ์ ๋ฌธ์ฅ์ด ๋ค์ ์์ํ๊ฒ ๋๋ค.
recognition.addEventListener('end', recognition.start);
recognition.start();