Here’s the one component that handles all of the logic you’ll need.

“initializeRecognizer()” on row 21 does almost exactly what it says. It creates a new instance of a “SpeechCommandRecognizer”, loads all the known commands in to an array then starts listening using the microphone and compares sounds your microphones picks up against the array of known commands.

“this.recognizer.listen()” on row 31 takes an “RecognizerCallback” as a parameter which is the function that will run each time the recognizer hears something (i.e. a word is spoken). It also takes a “StreamingRecognitionConfig” as a parameter, which is a optional parameter where you can specify something called a “probabilityThreshold”. The “probabilityThreshold” determines when the callback function will be invoked, default value is 0.

Inside my callback function I map the result into an array. I sort the array based on the score from each word. In my example I’m only interested in the word with the highest score which is why i only push the first item inside my sorted array in to my predictions array.