Search code examples
c#microsoft-speech-platform

Microsoft Speech Platform speech to text


I'd like to write the speech a user says to text. Can I do this with the Microsoft Speech Platform? Perhaps I'm just misunderstanding how it's supposed to work and what its intended use case is.

I've got this console application now:

static void Main(string[] args)
        {
            Choices words = new Choices();
            words.Add(new string[] { "test", "hello" ,"blah"});
            GrammarBuilder gb = new GrammarBuilder();
            gb.Append(words);
            Grammar g = new Grammar(gb);

            SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
            sre.LoadGrammar(g);
            sre.SetInputToDefaultAudioDevice();

            //add listeners

            sre.Recognize();
            Console.ReadLine();
        }

And it only seems to output the words that I specify in Choices.

Would I have to add an entire dictionary of words if I wanted to match (most) of what a user will say?

Furthermore it stops right after it matches a single word. What if I wanted to capture entire sentences?

I'm looking for solutions for A) Capturing a wide array of words, and B) capturing more than one word at once.

Edit:

I found this: http://www.codeproject.com/Articles/483347/Speech-recognition-speech-to-text-text-to-speech-a#torecognizeallspeech


Solution

  • As seen in this page, the DictationGrammar class has a basic library of common words.

    To capture more than one word at once I did

     sre.RecognizeAsync(RecognizeMode.Multiple);
    

    So my code is now this:

        public static SpeechRecognitionEngine sre;
        static void Main(string[] args)
        {
            sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
            sre.LoadGrammar(new Grammar(new GrammarBuilder("exit")));
            sre.LoadGrammar(new DictationGrammar());
            sre.SetInputToDefaultAudioDevice();
    
            sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
    
            Console.ReadLine();
        }
    
    
        private static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result.Text == "exit")
            {
                sre.RecognizeAsyncStop();
            }
            Console.WriteLine("You said: " + e.Result.Text);
        }