JamesBrill / react-speech-recognition

💬Speech recognition for your React app
https://webspeechrecognition.com/
MIT License
657 stars 119 forks source link

Access to finalTranscript from within command callback #84

Closed timesarrow closed 3 years ago

timesarrow commented 3 years ago

It would be pretty useful to be able to get to the final transcript before the command callback is executed. Right now you can access the {command} as a param within the callback but that only gives the matched command. So for example, I say "my favorite foods are pizza and beer" and the command is set up as "my favorite foods are and ". I could log it that way but I'd rather have the full text of what was spoken. So...I am trying to log the final transcript first, then the response second, like this: log[0] = "my favorite foods are pizza and beer" log[1] = "Yes, pizza and beer are my favorites too"

But if I log the response from the callback and then have a useEffect() that logs the final transcript, it becomes: log[0] = "Yes, pizza and beer are my favorites too" log[1] = "my favorite foods are pizza and beer"

JamesBrill commented 3 years ago

Hi @timesarrow In theory, you shouldn't need access to the final transcript - you already have what you need inside the callback. For example, let's say you have the command for 'My favourite foods are * and *'. When this is matched, the callback will receive the two words that matched the splats. You already know what the command template is, so you just need to insert the two matched words to recreate the final transcript in your logs. For example, this command would do the job:

    {
      command: 'My favorite foods are * and *',
      callback: (a, b) => {
        console.log(`My favorite foods are ${a} and ${b}`)
        console.log(`Yes, ${a} and ${b} are my favorites too`)
      }
    }

Hope that helps!

timesarrow commented 3 years ago

That works, although I was hoping to be able to use the actual spoken phrase without having to splice it together. Not a huge deal tho. Thx for the info.