首页 > 解决方案 > C# System.Speech.Recognition 没有命令?

问题描述

我认为这与C# system.speech.recognition alter words是同一个问题,但是答案不起作用。Microsoft API 需要使用命令。如果我不输入命令,它会显示一条消息,说明它是必需的。如果我添加一个命令,那就是唯一遇到的文本。我想写一些东西来决定我所说的每一个字。像 MS Agent 那样的事情在当天就做了。任何人都有一些方向,因为我失败了并且不想使用 Google Cloud API。我希望它在本地运行。

using System;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;

namespace S2TextDemo
{
    class Program
    {
        static SpeechSynthesizer ss = new SpeechSynthesizer();
        static SpeechRecognitionEngine sre;
        static bool speechOn = true;
        static private AutoResetEvent _quitEvent;

        static void Main(string[] args)
        {
            try
            {
                 _quitEvent = new AutoResetEvent(false);

                ss.SetOutputToDefaultAudioDevice();
                CultureInfo ci = new CultureInfo("en-us");
                sre = new SpeechRecognitionEngine(ci);
                sre.SetInputToDefaultAudioDevice();
                sre.SpeechRecognized += sre_SpeechRecognized;
                //sre.SpeechRecognized += SpeechRecognizedHandler;
                Choices ch_StartStopCommands = new Choices();

                ch_StartStopCommands.Add("quit");

                GrammarBuilder gb_StartStop = new GrammarBuilder();
                gb_StartStop.Append(ch_StartStopCommands);
                Grammar g_StartStop = new Grammar(gb_StartStop);
                sre.LoadGrammarAsync(g_StartStop);

                sre.RecognizeAsync(RecognizeMode.Multiple);
                Console.WriteLine("Listening...\n");
                ss.SpeakAsync("I'm now listening.");

                 _quitEvent.WaitOne();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        } // Main

        static void SpeechRecognizedHandler(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result == null) return;
            string txt = e.Result.Text;

            // Add event handler code here.

            // The following code illustrates some of the information available
            // in the recognition result.
            Console.WriteLine("Grammar({0}), {1}: {2}",
              e.Result.Grammar.Name, e.Result.Audio.Duration, e.Result.Text);

            // Display the semantic values in the recognition result.
            foreach (KeyValuePair<String, SemanticValue> child in e.Result.Semantics)
            {
                Console.WriteLine(" {0} key: {1}",
                  child.Key, child.Value.Value ?? "null");
            }
            Console.WriteLine();

            // Display information about the words in the recognition result.
            foreach (RecognizedWordUnit word in e.Result.Words)
            {
                RecognizedAudio audio = e.Result.GetAudioForWordRange(word, word);
                Console.WriteLine(" {0,-10} {1,-10} {2,-10} {3} ({4})",
                  word.Text, word.LexicalForm, word.Pronunciation,
                  audio.Duration, word.DisplayAttributes);
            }

            // Display the recognition alternates for the result.
            foreach (RecognizedPhrase phrase in e.Result.Alternates)
            {
                Console.WriteLine(" alt({0}) {1}", phrase.Confidence, phrase.Text);
            }
        }

        static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            double minConfidence = 0.90;
            string txt = e.Result.Text;
            float confidence = e.Result.Confidence;

            Console.WriteLine("\nRecognized: " + txt);
            if (confidence < minConfidence)
            {
                Console.WriteLine($"Failed confidence: {minConfidence} with {confidence}" );
                return;
            }

            if (txt.IndexOf("quit") >= 0)
            {
                if(speechOn)
                    ss.SpeakAsync("Shutting down.");
                else
                    Console.WriteLine("Shutting down.");

                Thread.Sleep(1000);
                _quitEvent.Set();
            }
        } // sre_SpeechRecognized
    } // Program
} // ns

标签: c#winformsspeech-to-text

解决方案


当你创建一个新的语法时,使用这个:

sre.LoadGrammarAsync(new DictationGrammar());

来自原始文档:
这里


推荐阅读