简体   繁体   中英

Read a phrase in SpeechRecognitionEngine

I'm playing around with SpeechRecognizer to see if it will support features I want for an application I'm designing.

The following code works for individual words. But is it possible to analyze a phrase? My application wouldn't need to work with large phrases, just 3 or 4 word phrases. As an example, I'm trying to make the app see there are multiple "words" being spoken (even though it's technically not multiple words).

    static void Main(string[] args)
    {

        // Create an in-process speech recognizer for the en-US locale.  
        using ( SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine( new System.Globalization.CultureInfo("en-US")))
        {
            Choices c = new Choices();

            c.Add("one");
            c.Add("two");
            c.Add("three");
            c.Add("twenty");

            var gb = new GrammarBuilder(c);
            var g = new Grammar(gb);
            recognizer.LoadGrammar(g);

            // Add a handler for the speech recognized event.  
            recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);

            // Configure input to the speech recognizer.  
            recognizer.SetInputToDefaultAudioDevice();

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

            // Keep the console window open.  
            while (true)
            {
                Console.ReadLine();
            }
        }
    }

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

I can speak and it returns one, two, three and twenty. But if I say twenty one it comes back with just twenty.

Is there a way to extend how many "syllables" (I'm not even sure of the term used here) it will analyze?

The answer is yes/no, it doesn't appear that there is way to make "twenty one" logically so I think that will be a limitation, unless I'm pointed otherwise. However in reading more into MS's documentation, I found this:

    static Grammar CreateServicesGrammar(string grammarName)
    {

        // Create a grammar for finding services in different cities.  
        Choices services = new Choices(new string[] { "restaurants", "hotels", "gas stations" });
        Choices cities = new Choices(new string[] { "Seattle", "Boston", "Dallas" });

        GrammarBuilder findServices = new GrammarBuilder("Find");
        findServices.Append(services);
        findServices.Append("near");
        findServices.Append(cities);

        // Create a Grammar object from the GrammarBuilder. 
        Grammar servicesGrammar = new Grammar(findServices);
        servicesGrammar.Name = ("FindServices");
        return servicesGrammar;
    }

Which allows for creation of phrases and seems to work quite well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM