0

I am using window.SpeechRecognition as a poor man's closed captioning for our church services.

occasionally the script misinterprets words as obscenities.

I did not see an option in window.SpeechRecognition to stop that from happening.

any thoughts?

below is the code in code pen. (this one works, it asks to allow microphone use) https://codepen.io/pollywog/pen/QWvaygg

if you try to run the snippet below it won't work because I guess the allow microphone is disabled on SO.

  window.SpeechRecognition = window.webkitSpeechRecognition || window.SpeechRecognition;
        let finalTranscript = '';
        let recognition = new window.SpeechRecognition();

        recognition.interimResults = true;
        recognition.maxAlternatives = 10;
        recognition.continuous = true;

        recognition.onresult = (event) => {
            let interimTranscript = '';
            for (let i = event.resultIndex, len = event.results.length; i < len; i++) {
                let transcript = event.results[i][0].transcript;
                if (event.results[i].isFinal) {
                    finalTranscript += transcript;
                } else {
                    interimTranscript += transcript;
                }
            }

            document.querySelector('div').innerHTML = finalTranscript + '<i style="color:#FFFF00;">' + interimTranscript + '</>';
            const myDiv = document.getElementById('container');
            myDiv.scrollTop = myDiv.scrollHeight;
        }
        recognition.start();
 <div style="height: 300px;
                     font-size: 100px;
                     color: #00ff90;
                     overflow:hidden;"
         id="container"/>
     
Bryan Dellinger
  • 4,724
  • 7
  • 33
  • 79

1 Answers1

1

At this point is where you should filter the string.

let transcript = event.results[i][0].transcript;

Here is one example of how to censor words using JavaScript:
https://codereview.stackexchange.com/questions/172154/censor-words-in-a-sentence

But you should be able to find something suitable to your needs.

Timothy Alexis Vass
  • 2,526
  • 2
  • 11
  • 30