与C#语音库的对话

本文关键字:对话 语音 | 更新日期: 2023-09-27 18:01:05

我使用语音识别库有以下代码:

var listen = new SpeechRecognitionEngine();
var reader = new Choices(File.ReadLines(@"C:'words.txt")
listen.LoadGrammar(new Grammar(new GrammarBuilder(reader)));
listen.SpeechRecognized += listen_SpeechRecognized;
listen.SpeechRecognitionRejected += listen_SpeechRecognitionRejected;
listen.SetInputToDefaultAudioDevice();
listen.RecognizeAsync(RecognizeMode.Multiple);

我有一个像这样的事件监听器。。。

static void listen_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            var talk = new SpeechSynthesizer();
            if (e.Result.Text == "Search Stock Symbol") 
            {
                talk.Speak("What symbol?");
                //Do I have to create another event listener?
                //a Listener .. symbol = a.Result.Text
                //talk.Speak(GetQuote(symbol))
            {
        }

我是否必须为"对话"的每一部分创建一个事件监听器?如果是这样的话,还有更好的方法吗?

对话示例:

  • Me:搜索股票符号
  • 电脑:什么符号
  • 我:AAPL
  • 电脑:苹果的交易价格是

与C#语音库的对话

没有,只是一个,然后根据收到的文本来改变您的操作。在某些代码之前:

    List<string> stockSymbols = new List<string>();
    stockSymbols.Add("AAPL");

然后

    string lastSpeechInput;
    static void listen_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        var talk = new SpeechSynthesizer();
        switch (e.Result.Text) {
            case "Search Stock Symbol":
                talk.Speak("What symbol?");
                break;
            default:
                break;
        }
        if (stockSymbols.Contains(e.Result.Text) && lastSpeechInput == "Search Stock Symbol") {
            talk.Speak(getStockPrice(e.Result.Text);
        }
        lastSpeechInput = e.Result.Text;
    }