C#语法和语法;GrammarBuilder'';此语法中未定义规则引用
我试图重复使用一些单词,但是当我尝试重复使用一个选项列表时,我得到了这个错误。除非我没有。 “”规则引用未在此语法中定义 我有两个项目:C#语法和语法;GrammarBuilder'';此语法中未定义规则引用,c#,voice-recognition,speech,C#,Voice Recognition,Speech,我试图重复使用一些单词,但是当我尝试重复使用一个选项列表时,我得到了这个错误。除非我没有。 “”规则引用未在此语法中定义 我有两个项目: List<string> words = new List<string>(); foreach (string line in File.ReadLines("wordsEnTest.txt")) { if (line.Length > 0) { words.Add(line); }
List<string> words = new List<string>();
foreach (string line in File.ReadLines("wordsEnTest.txt"))
{
if (line.Length > 0)
{
words.Add(line);
}
else { }
}
Choices omniChoices = new Choices(words.ToArray());
GrammarBuilder omniBuild = new GrammarBuilder(omniChoices);
for (int i = 1; i < 3; i++)
{
omniBuild.Append(omniChoices, 0, 1);
}
Grammar grammar = new Grammar(omniBuild);
engine.LoadGrammar(grammar);
我不知道你在做什么,但我建议你用“语音识别”而不是“语法”来标记你的问题。(语法标签指的是另一种语法。)我还注意到,你的“for”语句从1开始,而不是从0开始,这是用C#编写语句的典型方式-但也许这就是你想要的?我尝试过,但显然我需要通过发布更多信息来“解锁”这些标签(编辑:nvm,找到语音识别)。在这种情况下,1或0是任意的。我数了第一个加法,所以我从1开始。好吧,对不起,我帮不上更多的忙,我在这方面没有经验,只是一般的C。但为了100%确定,对于i=1和i=2,您的“for”语句将只执行两次。您得到的异常文本是什么?你的wordsEnTest.txt的内容是什么?您可以共享它以提供更多信息。不要粘贴它,因为它可能会丢失一些字节。总的来说,此问题可能是由API的输入不正确引起的。例如,您将仅空白字符串传递给choices.wordsEnTest.txt:a aah aahed aahing aardvark aardvark aardwalf ab abaci aback abacus等
for (int i = 1; i < 100; i++)
{
omniBuild.Append(omniChoices, 0, 1);
}
for (int i = 1; i < 3; i++)
{
omniBuild.Append(omniChoices, 0, 1);
}
System.FormatException was unhandled
HResult=-2146233033
Message='' rule reference not defined in this grammar.
Source=System.Speech
StackTrace:
at System.Speech.Internal.SrgsCompiler.GrammarElement.System.Speech.Internal.SrgsParser.IElement.PostParse(IElement parent)
at System.Speech.Recognition.GrammarBuilder.InternalGrammarBuilder.CreateElement(IElementFactory elementFactory, IElement parent, IRule rule, IdentifierCollection ruleIds)
at System.Speech.Recognition.GrammarBuilder.CreateGrammar(IElementFactory elementFactory)
at System.Speech.Recognition.GrammarBuilder.Compile(Stream stream)
at System.Speech.Recognition.Grammar.LoadCfg(Boolean isImportedGrammar, Boolean stgInit)
at System.Speech.Recognition.Grammar.LoadAndCompileCfgData(Boolean isImportedGrammar, Boolean stgInit)
at System.Speech.Recognition.Grammar.InitialGrammarLoad(String ruleName, Object[] parameters, Boolean isImportedGrammar)
at System.Speech.Recognition.Grammar..ctor(GrammarBuilder builder)
at VAI.Program.LoadWords() in C:\Users\Corbin\Desktop\VAI\VAI\Program.cs:line 71
at VAI.Program.Main(String[] args) in C:\Users\Corbin\Desktop\VAI\VAI\Program.cs:line 34
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: