Click here to Skip to main content
15,888,031 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
i am writing an app where speech from a user is processed using the speech SDK and the text result is compared with a certain string and the output used to send a command to turn a light on or off. i am stuck at how to compare the output of the speech with the string so that if they are the same, i send the necessary command to the output. i am a novice at coding and everything i try just gives errors. please help.
C#
        void Main(string[] args)
        {
           
            // Create a SpeechRecognitionEngine object for the default recognizer in the en-US locale since we have chosen
            // american english as our language 
            using (SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))
            {

                // Create a grammar.
                Choices appliances = new Choices(new string[] { "fan", "lights" });
                Choices Commands = new Choices(new string[] { "on", "off" });

                GrammarBuilder gb = new GrammarBuilder();
                gb.Append("Please turn the");
                gb.Append(appliances);
                gb.Append(Commands);

                // Create a Grammar object and load it to the recognizer.
                Grammar g = new Grammar(gb);
                recognizer.LoadGrammarAsync(g);

                // Attach event handlers.
                recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
                recognizer.SpeechRecognitionRejected +=
                new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected);

                // Set the input to the recognizer i.e to the audio file that was stored
                recognizer.SetInputToWaveFile(@"D:\speechtestfiles.wav");


                // Start asynchronous, continuous recognition.
                recognizer.RecognizeAsync(RecognizeMode.Multiple);


                // Keep the console window open.
                Console.ReadLine();

            }

string _recognised1 = " Speech recognized: Please turn the lights on";
            string _recognised2 = " Speech recognized: Please turn the fan on";
            string _recognised3 = " Speech recognized: Please turn the lights off";
            string _recognised4 = " Speech recognized: Please turn the fan off";

string _detected2 = new string(Result.Text);
         
//sends the "turn the light on" command to the appliances
            if (String.Compare(_recognised1, _detected2)==0)
            {
               
                PortAccess.Output(888,1);
            }

}

// Handle the SpeechRecognitionRejected event.
        public static void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
        {
            foreach (RecognizedPhrase phrase in e.Result.Alternates)
            {
                Console.WriteLine("  Rejected phrase: " + phrase.Text);

            }
        }

        // Handle the SpeechRecognized event.
        public static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
          Console.WriteLine("  Speech recognized: " + e.Result.Text);

        }
Posted
Comments
Sergey Alexandrovich Kryukov 6-May-13 3:12am    
Help with what? This is not a question.
—SA
[no name] 6-May-13 4:06am    
Thanx Sergey, after I have obtained the result of the speech recognized, I would like to use that result to determine the command to send to the output, so I want to compare this text result with my strings such that if they match, I send the necessary command. My challenge is, I have failed to find a way to compare the result wit my strings...

1 solution

I have myself never used the Speech Recognition API, but if you get it to work (Just the recognition), meaning if you get the
Quote:
Console.WriteLine(" Speech recognized: " + e.Result.Text);
to execute, and the right said text does print on the screen, then you should put the code that analyses the result (e.Result.Text) right after that line, since recognizer_SpeechRecognized is the method that runs if the speech is successfully recognized.
For now, you should just try to get the recognizer to print the text on screen, once done, do a simple string comparison (e.Result.Text.ToLowerCase().Trim() == "Please turn the fan on") to see if it runs, once that works, you can make it a more sophisticated speech analysis method.
I hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900