[JavaScript30] ๐ŸŽ™ 20. Native Speech Recognition

์กฐ์ค€ํ˜•ยท2021๋…„ 7์›” 14์ผ
0

JavaScript30

๋ชฉ๋ก ๋ณด๊ธฐ
20/30

๐ŸŽ™ 20. Native Speech Recognition

์Œ์„ฑ์ธ์‹์„ ํ†ตํ•ด ํ™”๋ฉด์— ์ถœ๋ ฅ ํ•ด๋ณด๊ธฐ.

์ดˆ๊ธฐ์ฝ”๋“œ

<!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>

์ดˆ๊ธฐํ™”๋ฉด

๐ŸŒ ์ƒˆ๋กœ ์•Œ๊ฒŒ๋œ ๊ฒƒ

๐Ÿ‘‰ SpeechRecognition

Web Speech API์˜ ์Œ์„ฑ ์ธ์‹ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์ธ์‹ ์„œ๋น„์Šค๋ฅผ ์œ„ํ•œ ์ปจํŠธ๋กค๋Ÿฌ ์ธํ„ฐํŽ˜์ด์Šค์ด๋ฉฐ, ์ด๊ฒƒ์€ ๋˜ํ•œ ์ธ์‹ ์„œ๋น„์Šค์—์„œ ๋ณด๋‚ด๋Š” ์Œ์„ฑ ์ธ์‹ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค.

์ฃผ์˜! ๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ์„ ์ž˜ ํ™•์ธํ•˜๊ธฐ.

์ฐธ๊ณ  :

https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition

๐Ÿ‘‰ SpeechRecognition.interimResults

์Œ์„ฑ ์ธ์‹ ์ธํ„ฐํŽ˜์ด์Šค์˜ ์ž„์‹œ ๊ฒฐ๊ณผ ์†์„ฑ์€ ์ค‘๊ฐ„ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋Š”์ง€(์ฐธ) ์—ฌ๋ถ€๋ฅผ ์ œ์–ดํ•ฉ๋‹ˆ๋‹ค(๊ฑฐ์ง“).
์ค‘๊ฐ„ ๊ฒฐ๊ณผ๋Š” ์•„์ง ์ตœ์ข…์ด ์•„๋‹Œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค(์˜ˆ: ์Œ์„ฑ ์ธ์‹ ๊ฒฐ๊ณผ.isFinal ์†์„ฑ์ด false์ž…๋‹ˆ๋‹ค).

intervalResults์˜ ๊ธฐ๋ณธ๊ฐ’์€ false์ž…๋‹ˆ๋‹ค.

์ฐธ๊ณ  :

https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition/interimResults

๐ŸŒ ๊ณผ์ •

๐Ÿ‘‰ 1. ์Œ์„์ธ์‹์„ ์œ„ํ•œ ์„ค์ •๊ณผ ๋ณ€์ˆ˜ ์ƒ์„ฑ

window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;

const recognition = new SpeechRecognition();
recognition.interimResults = true;

window.SpeechRecognition์„ ๋ธŒ๋ผ์šฐ์ €์—๋”ฐ๋ผ webkit์†์„ฑ์„ ์ ์šฉํ•œ๋‹ค.

interResult๋Š” ์ค‘๊ฐ„๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์ œ์–ดํ•จ.

๐Ÿ‘‰ 2. ์Œ์„ฑ์„ ๋ธŒ๋ผ์šฐ์ €์— ํ‘œ์‹œ

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์— ํ•œ ๋ฌธ์žฅ์œผ๋กœ ๋‚ด์šฉ์„ ๋‹ด์•„ ๋ง์ด ๋๋‚˜๋ฉด ์ƒˆ ๋ฌธ์žฅ์ด ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๊ฒŒ ๋œ๋‹ค.

๐Ÿ‘‰ 3. ์ด๋ฒคํŠธ ์‹คํ–‰

recognition.addEventListener('end', recognition.start);
recognition.start();
profile
๊นƒํ—ˆ๋ธŒ : github.com/JuneHyung

0๊ฐœ์˜ ๋Œ“๊ธ€