0

嗨,我正在尝试解析语音所说的时间:

        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

但是我在 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#

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

有任何想法吗?

4

1 回答 1

1

我很确定您正在重新绑定 SemanticResultValues。 文档特别说

在使用 SemanticResultValue 设置 Value 后,无论是使用默认根键还是任何特定 SemanticResultKey 标记,都不得修改该值,否则在识别操作期间将发生异常。

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

于 2014-07-15T00:56:35.477 回答