与C#语音库

时间:2016-08-06 22:23:34

标签: c# speech-recognition

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

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))
            {
        }

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

示例对话:

  • 我:搜索股票代码
  • 电脑:符号是什么?
  • 我:AAPL
  • 电脑:Apple正在交易....

1 个答案:

答案 0 :(得分:3)

不,只是那个,然后根据收到的文字改变你的行为。在之前的一些代码中:

    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;
    }