Skip to content
Advertisement

How to perform regular expressions on a Speech Recognition transcript?

Summary of my application:

  1. Input: Have the user speak to the computer (ask the computer "What is your name?") ✅
  2. Perform a regular expression on the ‘transcript’ result (from webkitSpeechRecognition) ❌
  3. Output: console.log("My name is Harry Johnson"). ❌

I’m trying to perform a regular expression on a speech recognition transcript, as follows:

var speechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; 
var recognition = new webkitSpeechRecognition();

var transcript;

recognition.addEventListener('result', e => {
     transcript = Array.from(e.results)
        .map(result => result[0])
        .map(result => result.transcript)
        .join('');
    console.log(transcript);
});

recognition.addEventListener('end',recognition.start);


window.addEventListener('DOMContentLoaded', function() {
    document.getElementById("speak_button").addEventListener('click', function() {
        recognition.start();
    });
});

The above works fine and I am able to console.log the transcript/speech from the user. The problem arises from the below code and I think it has something to do with the fact that the transcript has to be a string? I have tried converting the transcript to a string using String(transcript) and transcript.toString(), but I’m still not achieving my result: which is for the computer to console.log “My name is Harry Johnson”.

function speakBackToMe() {

/*regular expression that will match words in the transcript */
    var person_name_regex = /what is your name|(?=.*byourb)(?=.*bfullb)(?=.*bnameb)|(?=.*btellb)(?=.*bmeb)(?=.*byourb)(?=.*bnameb)|(?=.*bcanb)(?=.*btellb)(?=.*bmeb)(?=.*byourb)(?=.*bnameb)|(?=.*btellb)(?=.*bmeb)(?=.*byourb)(?=.*bfullb)(?=.*bnameb)|(?=.*bletb)(?=.*bknowb)(?=.*bfullb)(?=.*bnameb)|(?=.*bgrabb)(?=.*byourb)(?=.*bnameb)|(?=.*bwhatb)(?=.*byourb)(?=.*bnameb)|(?=.*bshareb)(?=.*bmeb)(?=.*bfullb)(?=.*bnameb)|(?=.*bwhatb)(?=.*byourb)(?=.*bfirstb)(?=.*bandb)(?=.*blastb)(?=.*bnameb)/ig;
   
/*if regular expression matches all words, then function will be performed*/
    if (person_name_regex.test(transcript)) {
        console.log("My name is Harry Johnson");
        
}

}

speakBackToMe();

The regular expression is correct, the main problem is with the transcript variable. If anyone has any advice on why the console.log("My name is Harry Johnson") function is not being performed, it would be greatly appreciated if you could share that with me! 🙂

Thanks!

Advertisement

Answer

for me I just put the speakBackToMe(); line below console.log(transcript); within the result event listener block and it worked

recognition.addEventListener('result', e => {
     transcript = Array.from(e.results)
        .map(result => result[0])
        .map(result => result.transcript)
        .join('');
    console.log(transcript);
    speakBackToMe();
});
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement