进行识别之前,至少必须加载一种语法 [英] at least one grammar must be loaded before doing a recognition

查看:157
本文介绍了进行识别之前,至少必须加载一种语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我尝试创建简单的AI程序,因此我定义了两个语法并加载了它们,但是在出现识别错误之前,我至少遇到了一个语法必须加载的错误. Visual Studio的错误是:

Hello i try to create simple AI program , so i define two grammar and load them and i get this error at least one grammar must be loaded before doing a recognition. the error from visual studio is :

System.Speech.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理
附加信息:在进行识别之前,必须至少加载一种语法.

An exception of type 'System.InvalidOperationException' occurred in System.Speech.dll but was not handled in user code
Additional information: At least one grammar must be loaded before doing a recognition.

这是代码 这是课程

class DefineGrammar
{
    /// <summary>
    /// Define Choices
    /// </summary>
    Choices greeting;
    Choices DateAndTime;


    /// <summary>
    /// Define the Grammar var
    /// </summary>
    Grammar greetingGrammar;
    Grammar DateAndTimeGrammar;

    SpeechRecognitionEngine rec = new SpeechRecognitionEngine();

    public void LoadGrammar()
    {
        // put the data inside the choice
        greeting = new Choices(new string[] { "hello", "how are you" });
        DateAndTime = new Choices(new string[] { "what time is it", "what is today" });

        // Define Grammar builder to put the choice inside it
        GrammarBuilder greetingGrammarBuillder = new GrammarBuilder(greeting);
        GrammarBuilder DateAndTimeGrammarBuilder = new GrammarBuilder(DateAndTime);

        //put the grammar builder inside the grammar
        greetingGrammar = new Grammar(greetingGrammarBuillder);
        greetingGrammar.Name = "GreetingGrammar";
        DateAndTimeGrammar = new Grammar(DateAndTimeGrammarBuilder);
        DateAndTimeGrammar.Name = "DateAndTimeGrammar";

        rec.LoadGrammar(greetingGrammar);
        rec.LoadGrammar(DateAndTimeGrammar);

    }
}

这是主页:

public partial class MainWindow : Window
{
    SpeechSynthesizer s = new SpeechSynthesizer();
    SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
    DefineGrammar gr = new DefineGrammar();

    public MainWindow()
    {
        rec.RequestRecognizerUpdate();
        gr.LoadGrammar();
        rec.SpeechRecognized += Rec_SpeechRecognized;
        rec.SetInputToDefaultAudioDevice();
        rec.RecognizeAsync(RecognizeMode.Multiple);
    }
}

推荐答案

在DefineGrammar类中,您有一个SpeechRecognitionEngine类型的成员字段,如果调用LoadGrammar(),则将语法加载到该成员字段.

In your DefineGrammar class, you have a member field of type SpeechRecognitionEngine to which you load the grammar if LoadGrammar() is invoked.

在您的主类中,您有一个不同的实例,您尝试在该实例上调用识别.

In your main class, you have a different instance of that type on which you try to invoke recognition.

现在,错误是您有两个单独的SpeechRecognitionEngine实例.

Now, the error is that you have two separate instances of SpeechRecognitionEngine.

解决此问题的一种方法可能是如下更改您的DefineGrammar:

One way to solve this could be to change your DefineGrammar as follows:

将其替换为public void LoadGrammar( SpeechRecognitionEngine rec ),并删除该类中的成员字段rec.

Instead of public void LoadGrammar() make it public void LoadGrammar( SpeechRecognitionEngine rec ) and remove the member field rec in that class.

还有其他可能性,但这可以完成工作.区别在于,现在您将语法加载到主类中使用的实例上,而不是其他实例上.

There are other possibilities but this will do the job. The difference is that now you load the Grammar to the instance that is used in your main class, not a different one.

这篇关于进行识别之前,至少必须加载一种语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆