4
I tried to use result.replace("arroba", "@") javascript to change the word "arroba" to "@" as it is in the code below, but it didn’t work. I want to do this with point, comma, underline, underline and etc... because, the Speech recognition spells out 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);
Closes
receber_audio.onresult = function (event) {
with}
.– user7393973
Utilizes Regexp with the flag
g
to replace all occurrences instead of only the first. Example:"um dois um".replace(/um/g, "1").replace(/dois/g, "2");
.– user7393973
Thanks for the help!
– Luizinho