Home > Software engineering >  How to perform regular expressions on a Speech Recognition transcript?
How to perform regular expressions on a Speech Recognition transcript?

Time:04-16

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|(?=.*\byour\b)(?=.*\bfull\b)(?=.*\bname\b)|(?=.*\btell\b)(?=.*\bme\b)(?=.*\byour\b)(?=.*\bname\b)|(?=.*\bcan\b)(?=.*\btell\b)(?=.*\bme\b)(?=.*\byour\b)(?=.*\bname\b)|(?=.*\btell\b)(?=.*\bme\b)(?=.*\byour\b)(?=.*\bfull\b)(?=.*\bname\b)|(?=.*\blet\b)(?=.*\bknow\b)(?=.*\bfull\b)(?=.*\bname\b)|(?=.*\bgrab\b)(?=.*\byour\b)(?=.*\bname\b)|(?=.*\bwhat\b)(?=.*\byour\b)(?=.*\bname\b)|(?=.*\bshare\b)(?=.*\bme\b)(?=.*\bfull\b)(?=.*\bname\b)|(?=.*\bwhat\b)(?=.*\byour\b)(?=.*\bfirst\b)(?=.*\band\b)(?=.*\blast\b)(?=.*\bname\b)/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!

CodePudding user response:

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();
});
  • Related