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

时间:2017-08-07 20:19:45

标签: c# speech-recognition

我正在使用Windows.Speech API 我想要做的是让系统识别短语的一部分,而不是寻找整个事情。 例如,如果我用以下字符加载字符串:"你好吗",它要求用户准确说出,你好吗?最终,我希望Windows.Speech能够认识到这样的事情:"嘿,你今天过得怎么样?"。

以下是我目前的情况:

//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做我需要的东西,我就不会重写。

1 个答案:

答案 0 :(得分:0)

我要回答我自己的问题。这里的评论很有帮助,但他们并没有为部分做出伎俩。我实际上发现,如果你添加.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();
        }