Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试创建一些简单的System.Speech.Recognition语法时崩溃_C#_.net_Speech Recognition_Sapi_Microsoft Speech Api - Fatal编程技术网

C# 尝试创建一些简单的System.Speech.Recognition语法时崩溃

C# 尝试创建一些简单的System.Speech.Recognition语法时崩溃,c#,.net,speech-recognition,sapi,microsoft-speech-api,C#,.net,Speech Recognition,Sapi,Microsoft Speech Api,嗨,我在分析演讲中说的时间: var ret = new GrammarBuilder(); var hours = new Choices(); for (int c = 1; c < 24; c++) hours.Add(new SemanticResultValue(c.ToString(), c)); var minutes = new Choices(); for (int

嗨,我在分析演讲中说的时间:

        var ret = new GrammarBuilder();

        var hours = new Choices();
        for (int c = 1; c < 24; c++)
            hours.Add(new SemanticResultValue(c.ToString(), c));

        var minutes = new Choices();
        for (int c = 1; c < 60; c = c + 1)
            minutes.Add(new SemanticResultValue(c.ToString(), c));

        var topLevelChoices = new Choices();

        //eight fourty five = 8:45
        //four twenty = 4:20
        var methodOne = new GrammarBuilder();
        methodOne.Append(new SemanticResultKey("hour", hours));
        methodOne.Append(new SemanticResultKey("minute", minutes));
        topLevelChoices.Add(methodOne);

        //quarter to five = 4:45
        //half past ten = 10:30
        var methodTwo = new GrammarBuilder();
        methodTwo.Append(new SemanticResultKey("minute", new Choices(
            new SemanticResultValue("twenty past", 20),
            new SemanticResultValue("twenty to", -20),
            new SemanticResultValue("quarter past", 15),
            new SemanticResultValue("quarter to", -15),
            new SemanticResultValue("half past", 30)
            )));
        methodTwo.Append(new SemanticResultKey("hour", hours));
        topLevelChoices.Add(methodTwo);

        //ten o'clock
        //one o'clock
        var methodThree = new GrammarBuilder();
        methodThree.Append(new SemanticResultKey("hour", hours));
        methodThree.Append("o'clock");
        topLevelChoices.Add(methodThree);

        ret.Append(topLevelChoices);

        var g = new Grammar(ret); //crash occurs here
var ret=newgrammarbuilder();
var hours=新选项();
对于(int c=1;c<24;c++)
Add(新的SemanticResultValue(c.ToString(),c));
var minutes=新选项();
对于(int c=1;c<60;c=c+1)
Add(新的SemanticResultValue(c.ToString(),c));
var topLevelChoices=新选项();
//845=8:45
//四点二十=四点二十
var methodOne=new GrammarBuilder();
Append(新的SemanticResultKey(“hour”,hours));
Append(新的SemanticResultKey(“分钟”,分钟));
topLevelChoices.Add(methodOne);
//五点差一刻=4:45
//十点半=十点半
var methodTwo=新的GrammarBuilder();
方法二:追加(新语义结果键(“分钟”),新选项(
新SemanticResultValue(“二十过去”,20),
新SemanticResultValue(“二十到”、-20),
新SemanticResultValue(“过去一个季度”,15),
新SemanticResultValue(“四分之一到”,-15),
新SemanticResultValue(“过半”,30)
)));
方法2.Append(新的语义查询键(“hour”,hours));
添加(方法二);
//十点钟
//一点钟
var methodThree=新的GrammarBuilder();
方法三:追加(新语义查询键(“小时”,小时));
方法三:附加(“点”);
添加(方法三);
ret.Append(顶级选项);
var g=新语法(ret)//这里发生车祸
但我在mscorlib.dll中遇到类型为“System.ArgumentOutOfRangeException”的异常(其他信息:索引超出范围。必须为非负且小于集合的大小)。请参见上面的堆栈:

mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException() + 0x4e bytes 
mscorlib.dll!System.Collections.Generic.List<System.__Canon>.this[int].set(int index, System.__Canon value) + 0x3b bytes    
System.Speech.dll!System.Speech.Internal.GrammarBuilding.BuilderElements.Optimize(System.Collections.ObjectModel.Collection<System.Speech.Internal.GrammarBuilding.RuleElement> newRules) + 0x38e bytes 
System.Speech.dll!System.Speech.Recognition.GrammarBuilder.InternalGrammarBuilder.CreateElement(System.Speech.Internal.SrgsParser.IElementFactory elementFactory, System.Speech.Internal.SrgsParser.IElement parent, System.Speech.Internal.SrgsParser.IRule rule, System.Speech.Internal.GrammarBuilding.IdentifierCollection ruleIds) + 0x83 bytes    
System.Speech.dll!System.Speech.Recognition.GrammarBuilder.CreateGrammar(System.Speech.Internal.SrgsParser.IElementFactory elementFactory) + 0x6e bytes 
System.Speech.dll!System.Speech.Recognition.GrammarBuilder.Compile(System.IO.Stream stream) + 0x7e bytes    
System.Speech.dll!System.Speech.Recognition.Grammar.LoadCfg(bool isImportedGrammar, bool stgInit) + 0x174 bytes 
System.Speech.dll!System.Speech.Recognition.Grammar.LoadAndCompileCfgData(bool isImportedGrammar, bool stgInit) + 0x32 bytes    
> SapiGrammarCrash.exe!SapiGrammarCrash.Form1.Form1_Load(object sender, System.EventArgs e) Line 71 + 0x29 bytes    C#
mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException()+0x4e字节
mscorlib.dll!System.Collections.Generic.List.this[int].set(int索引,System.\u佳能值)+0x3b字节
System.Speech.dll!System.Speech.Internal.GrammarBuilding.BuilderElements.Optimize(System.Collections.ObjectModel.Collection newRules)+0x38e字节
System.Speech.dll!System.Speech.Recognition.GrammarBuilder.InternalGrammarBuilder.CreateElement(System.Speech.Internal.SrgsParser.IElementFactory元素工厂,System.Speech.Internal.SrgsParser.IEElement父级,System.Speech.Internal.SrgsParser.IRule规则,System.Speech.Internal.GrammarBuildering.IdentifierCollection规则ID)+0x83字节
System.Speech.dll!System.Speech.Recognition.GrammarBuilder.CreateGrammar(System.Speech.Internal.SrgsParser.IElementFactory elementFactory)+0x6e字节
System.Speech.dll!System.Speech.Recognition.GrammarBuilder.Compile(System.IO.Stream)+0x7e字节
System.Speech.dll!System.Speech.Recognition.Grammar.LoadCfg(bool isImportedGrammar,bool stgInit)+0x174字节
System.Speech.dll!System.Speech.Recognition.Grammar.LoadAndCompileCfgData(bool isImportedGrammar,bool stgInit)+0x32字节
>SapiGrammarCrash.exe!SAPIGRAMARCRASH.Form1.Form1_加载(对象发送方,System.EventArgs e)行71+0x29字节C#
我试过.NET3.0和4.5-结果相同。还尝试了2台机器-均为x64


有什么想法吗?

我很确定您正在重新绑定SemanticResultValues。具体说

SemanticResultValue用于设置值后,是否 使用默认根键或任何特定的 SemanticResultKey,该值不能被修改或作为异常 将在识别操作期间发生


解决方法是为methodThree创建一组单独的SemanticResultValue。

我怀疑您正在对语义结果键/值对进行多重绑定。试着简化你的语法(注释掉各个小节),直到它运行为止,看看它能告诉你什么;事实上,它在没有methodOne+methodTwo或methodTwo的情况下运行。但我需要全部3:)还尝试了全部的minOccur=1和maxoccure=1。还尝试为每个方法的语义添加唯一的后缀。还有其他提示吗?如果不重用methodThree中的“hours”对象,而是创建一组新的SemanticResultValue,会发生什么?造成问题的不是键,而是值。特别是说“在使用SemanticResultValue设置值后,无论是使用默认根键还是任何特定SemanticResultKey对其进行标记,都不得修改该值,否则在识别操作期间会发生异常。”就是这样!我在重复使用同一个实例。一旦我创建了一个小时数组,问题就消失了。非常感谢。请复制并粘贴到答案上,我会打勾。