Crash试图创建一些简单的System.Speech.Recognition语法

时间:2014-07-12 11:40:52

标签: 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 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

但是我得到了类型&System; .ArgumentOutOfRangeException&#39;发生在mscorlib.dll中(附加信息:索引超出范围。必须是非负数且小于集合的大小。)。见上层:

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#

我尝试过.NET 3.0和4.5 - 结果相同。还尝试了2台机器 - 都是x64。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我很确定你正在重新绑定SemanticResultValues。 The docs具体说

  

使用SemanticResultValue设置Value后,是否   标记有默认根密钥或任何特定的   SemanticResultKey,该值不得修改或异常   将在识别操作期间发生。

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