c# 语音识别部分短语 - windows.speech

c# speech recognition part of phrase - windows.speech

我正在使用 Windows.Speech API 我想做的是让系统识别短语的一部分,而不是寻找整个词组。 例如,如果我用 "How are you" 加载字符串,它需要用户准确地说出你好吗。最终,我希望 Windows.Speech 也能识别出这样的东西:"Hey How are you today".

这是我目前拥有的:

//This is used for Building the recognizer engine. 
Choices commands = new Choices();
commands.Add(new string[] { Question5, 
Question1,Question3,Question2,Question4});
GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(commands);
Grammar grammar = new Grammar(gBuilder);

//Loading the Grammar engine
recEngine.LoadGrammarAsync(grammar);
recEngine.SetInputToDefaultAudioDevice();
recEngine.SpeechRecognized += recEngine_SpeechRecognized;

然后显示:

void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    if (e.Result.Text == Question1)
    {
        richTextBox1.Text += "\nYou have Asked Question 1";
        chkBoxQ1.Checked = true;
        agentScore = agentScore + 1;
        stopCussing = Response1;
        Form2 responseForm = new Form2();
        responseForm.Show();
        Question1Answer = "Question Asked";
        txtBoxAnswer1.Enabled = true;
    }
    else if (e.Result.Text == Question2)
    {
        richTextBox1.Text += "\nYou have now asked Question 2";
        chkQuestion2.Checked = true;
        stopCussing = Response2;
        Form2 responseForm = new Form2();
        responseForm.Show();
        agentScore = agentScore + 1;
        Question2Answer = "Question Asked";
        txtAnswer2.Enabled = true;
    }
    else if (e.Result.Text == Question3)
    {
        richTextBox1.Text += "\nYou have now asked Question 3";
        chkQuestion3.Checked = true;
        stopCussing = Response3;
        Form2 responseForm = new Form2();
        responseForm.Show();
        agentScore = agentScore + 1;
        Question3Answer = "Question Asked";
        txtAnswer3.Enabled = true;
    }
    else if (e.Result.Text == Question4)
    {
        richTextBox1.Text += "\nYou have now asked Question 4";
        chkQuestion4.Checked = true;
        stopCussing = Response4;
        Form2 responseForm = new Form2();
        responseForm.Show();
        agentScore = agentScore + 1;
        Question4Answer = "Question Asked";
        txtAnswer4.Enabled = true;
    }
    else if (e.Result.Text == Question5)
    {
        richTextBox1.Text += "\nYou have now asked Question 5";
        chkQuestion5.Checked = true;
        stopCussing = Response5;
        Form2 responseForm = new Form2();
        responseForm.Show();
        agentScore = agentScore + 1;
        Question5Answer = "Question Asked";
        txtAnswer5.Enabled = true;
    } 

这里的任何见解都很棒! 仅供参考,我考虑过使用认知服务——但如果我能让 windows.speech 做我需要的,我宁愿不重写。

我要回答我自己的问题。这里的评论很有帮助,但它们并没有解决部分问题。我实际上发现,如果您添加 .contains,它将接受近似值 - 唯一需要注意的是,所说的陈述必须以同一个词开头。示例代码如下:

void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {

        if (e.Result.Text.Contains( Question1)) //This did the trick here
        {
            richTextBox1.Text += "\nYou have Asked Question 1";
            questionCode = "Question1";
            chkBoxQ1.Checked = true;
            agentScore = agentScore + 1;
            stopCussing = Response1;
            //Form2 responseForm = new Form2();
            //responseForm.Show();
            FurtherResponse further = new FurtherResponse(questionCode, programcode);
            further.Show();
            Question1Answer = "Question Asked";
            txtBoxAnswer1.Enabled = true;
        }
        else if (e.Result.Text.Contains(Question2))
        {
            richTextBox1.Text += "\nYou have now asked Question 2"; 
            chkQuestion2.Checked = true;
            stopCussing = Response2;
            //Form2 responseForm = new Form2();
            //responseForm.Show();
            agentScore = agentScore + 1;
            Question2Answer = "Question Asked";
            txtAnswer2.Enabled = true;
            questionCode = "Question2";
            FurtherResponse further = new FurtherResponse(questionCode, programcode);
            further.Show();
        }