简体   繁体   English

C#语音同时识别多个单词? (认识一个句子)

[英]C# Speech Recognizing Multiple Words together? (Recognize a sentence)

I'm building an application that recognizes multiple words from a user;我正在构建一个可以识别来自用户的多个单词的应用程序; thus putting together a sentence using the words recognized.从而使用识别的单词组合一个句子。

Here's what I have as of now:这是我现在所拥有的:

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

HOWEVER, this code will only recognize one word at a time.但是,此代码一次只能识别一个单词。 Even if I edit my code to do this:即使我编辑我的代码来做到这一点:

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

The application cannot continuously recognize words when I utter the words "What is a car" continuously without having breaks when I speak them out.当我连续说出“什么是汽车”这个词时,应用程序无法连续识别单词,而当我说出它们时没有中断。

What changes can I make in order for the program to recognize an entire sentence built using those words defined, without having to have speech breaks when uttering the sentence?为了让程序识别使用这些定义的单词构建的整个句子,我可以进行哪些更改,而不必在说出句子时出现语音中断

Output required:需要输出:

I utter the sentence: What is a car我说出一句话:什么是汽车

Application displays: What is a car应用展示:什么是汽车

PERFECT Example: Google Speech Recognition Google develops a sentence using the words available in their word library完美示例:谷歌语音识别谷歌使用其词库中可用的单词开发一个句子

Thank you kindly :)非常感谢你 :)

It recognizes one word because you incorrectly constructed the grammar.它可以识别一个单词,因为您错误地构建了语法。 Since you constructed the grammar consisting of choice of one of the words "what", "is", "a", "car" it exactly recognizes one of the words.由于您构建了由选择“what”、“is”、“a”、“car”其中一个词组成的语法,因此它可以准确识别其中一个词。

You probably want to read introduction into grammars and related documentation.您可能想阅读语法和相关文档的介绍。

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

If you want to construct the grammar describing the phrase you can just use GrammarBuilder like this:如果你想构建描述短语的语法,你可以像这样使用 GrammarBuilder:

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

This grammar will recognize a phrase.该语法将识别一个短语。

To understand how Choices work you can read documentation on Choices:要了解 Choices 的工作原理,您可以阅读有关 Choices 的文档:

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

This answer may be a little late, but I have not found any other place with an actual answer to this problem.这个答案可能有点晚了,但我还没有找到任何其他地方对这个问题有实际的答案。 So to save others the the time and frustration, here is how I did it.因此,为了节省其他人的时间和挫折感,我就是这样做的。

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



        }
}
}

The key here is this line这里的关键是这条线

new GrammarBuilder(builder, 0, 4)新的 GrammarBuilder(builder, 0, 4)

This tells the speech recognizer to recognize, up to 4 repetitions of elements form builder , and a minimum of zero.这告诉语音识别器识别builder中最多 4 次重复的元素,并且最少为 0。

So it will now recognize所以它现在会识别

"what is a car"

And if you want more than 4 repetitions, just change new GrammarBuilder(builder, 0, 4)如果您想要超过 4 次重复,只需更改new GrammarBuilder(builder, 0, 4)

to

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

See this for more info GrammarBuilder(builder, minRepeat, maxRepeat)有关更多信息,请参阅此GrammarBuilder(builder, minRepeat, maxRepeat)

您应该使用richTextBox而不是textbox然后附加e.result.text ,例如:

richtextbox.append(e.result.text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM