C#语音识别多个单词? (承认一句话)

时间:2014-10-03 13:38:33

标签: c# speech-recognition speech sentence

我正在构建一个识别用户多个单词的应用程序;因此,使用已识别的单词组成一个句子。

这是我现在所拥有的:

namespace SentenceRecognitionFramework__v1_
{
    public partial class Form1 : Form
    {

        SpeechRecognitionEngine recog = new SpeechRecognitionEngine();
        SpeechSynthesizer sp = new SpeechSynthesizer();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnListen_Click(object sender, EventArgs e)
        {
            Choices sList = new Choices();
            sList.Add(new String[] { "what","is", "a", "car" });

            Grammar gr = new Grammar(new GrammarBuilder(sList));

            recog.RequestRecognizerUpdate();
            recog.LoadGrammar(gr);
            recog.SpeechRecognized += sRecognize_SpeechRecognized;
            recog.SetInputToDefaultAudioDevice();
            recog.RecognizeAsync(RecognizeMode.Multiple);
            recog.SpeechRecognitionRejected += sRecognize_SpeechRecognitionRejected;
        }

        private void sRecognize_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
        {
            sentenceBox.Text = "Sorry, I couldn't recognize";
        }

        private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            sentenceBox.Text = e.Result.Text.ToString();
        }
    }
}

但是,此代码一次只能识别一个单词。即使我编辑我的代码来执行此操作:

private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            sentenceBox.Text = sentenceBox.Text + " " + e.Result.Text.ToString();
        }

当我说出单词时,应用程序不能持续识别单词"什么是汽车"当我说出来的时候,一直没有休息。

我可以进行哪些更改,以便程序识别使用这些定义的单词构建的整个句子,而不必在说出句子时语音中断

需要输出:

  
    

我说出这句话:什么是汽车

         

应用程序显示:什么是汽车

  

完美示例: Google语音识别 Google使用其文字库中提供的字词开发句子

谢天谢地:)

2 个答案:

答案 0 :(得分:4)

它识别一个单词,因为你错误地构造了语法。由于你构造的语法包括选择其中一个单词“what”,“is”,“a”,“car”,它完全识别出其中一个单词。

您可能希望阅读有关语法和相关文档的介绍。

http://msdn.microsoft.com/en-us/library/hh378438(v=office.14).aspx

如果你想构建描述短语的语法,你可以像这样使用GrammarBuilder:

  Grammar gr = new Grammar(new GrammarBuilder("what is a car"));

这个语法会识别一个短语。

要了解Choices的工作原理,您可以阅读有关Choices的文档:

http://msdn.microsoft.com/en-us/library/microsoft.speech.recognition.choices(v=office.14).aspx

答案 1 :(得分:3)

这个答案可能有点晚了,但我还没有找到任何其他地方对这个问题有实际答案。所以为了节省他人的时间和挫折,我就是这样做的。

using System;
using System.Threading;
using System.Speech;
using System.Speech.Synthesis;
using System.Speech.Recognition;

namespace SpeachTest
{
public class GrammerTest
{
        static void Main()
        {
        Choices choiceList = new Choices();
        choiceList.Add(new string[]{"what", "is", "a", "car", "are", "you", "robot"} );

        GrammarBuilder builder = new GrammarBuilder();
        builder.Append(choiceList);
        Grammar grammar = new Grammar(new GrammarBuilder(builder, 0, 4) );   //Will recognize a minimum of 0 choices, and a maximum of 4 choices

            SpeechRecognizer speechReco = new SpeechRecognizer();
            speechReco.LoadGrammar(grammar);



        }
}
}

这里的关键是这一行

  

new GrammarBuilder(builder,0,4)

这告诉语音识别器识别,最多4个重复的元素形成builder,最小值为零。

所以它现在会识别

"what is a car"

如果您想重复4次以上,只需更改new GrammarBuilder(builder, 0, 4)

即可

new GrammarBuilder(builder, 0 "the number of repetitions you want")

有关详情GrammarBuilder(builder, minRepeat, maxRepeat)

,请参阅此处