Speech Recognition does not recognize special characters

4

I've tried using result.replace ("at", "@") of javascript to change the word "at" to "@" as it is in the code below but did not work. I want to do this with period, comma, underline and etc ... therefore, speech recognition writes in extenso the special characters. Does anyone have any idea how I can do this?

HTML

<!DOCTYPE html>

<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
    </head>
    <body>
        <button id="btn-gravar-audio">Gravar</button><br/><br/>
        <script type="text/javascript" src="js/teste.js"></script>
        <textarea id="textarea" cols="60" rows="5"></textarea>
    </body>
</html>

JAVASCRIPT

window.addEventListener('DOMContentLoaded', function () {
var btn_gravacao = document.querySelector('#btn-gravar-audio');

var transcricao_audio = '';
var esta_gravando = false;

if (window.SpeechRecognition || window.webkitSpeechRecognition) {

    var speech_api = window.SpeechRecognition || window.webkitSpeechRecognition;
    var receber_audio = new speech_api();

    receber_audio.continuous = true;
    receber_audio.interimResults = true;
    receber_audio.lang = "pt-BR";

    receber_audio.onstart = function () {
        esta_gravando = true;
        btn_gravacao.innerHTML = 'Gravando! Parar gravação';
    };

    receber_audio.onend = function () {
        esta_gravando = false;
        btn_gravacao.innerHTML = 'Iniciar Gravação';
    };
    receber_audio.onresult = function (event) {
                var interim_transcript = '';

               for(var i = event.resultIndex; i < event.results.length; i++){
                   if(event.results[i].isFinal){
                       transcricao_audio += event.results[i][0].transcript; 
                   }else{
                       interim_transcript += event.results[i][0].transcript; 
                   }

                   var resultado = transcricao_audio || interim_transcript;

                   console.log(resultado);
               }



               document.getElementById("textarea").innerHTML = resultado;

    resultado.replace("arroba", "@");

    };

    btn_gravacao.addEventListener('click', function (e) {
        if (esta_gravando) {
            receber_audio.stop();
            return;
        }
        receber_audio.start();
    }, false);

} else {
    console.log("navegador não apresenta suporte a web speech api");
}
}, false);
    
asked by anonymous 05.01.2018 / 14:35

1 answer

5

It has 2 errors, first is order:

document.getElementById("textarea").innerHTML = resultado;

resultado.replace("arroba", "@");

You are doing replace after, it has to be before, something else you need to set the value again in the variable, like this:

resultado = resultado.replace("arroba", "@");

document.getElementById("textarea").innerHTML = resultado;

Only if you use with string so replace("arroba", "@") it will only search the first arroba, ideial is to use

05.01.2018 / 16:15