问题
I want to build my grammar to accept multiple number. It has a bug when I repeat the number like saying 'twenty-one'. So I kept reducing my code to find the problem. I reached the following piece of code for the grammar builder:
string[] numberString = { "one" };
Choices numberChoices = new Choices();
for (int i = 0; i < numberString.Length; i++)
{
numberChoices.Add(new SemanticResultValue(numberString[i], numberString[i]));
}
gb[1].Append(new SemanticResultKey("op1", (GrammarBuilder)numberChoices), 1, 2);
Now when I pronounce "one one" it still gives me this exception
Which when I googled for it, it states that this is an exception outside my code, I am wondering is this a bug in Microsoft.Speech dll or I am missing something
Edit 1:
I played around with the code, and made the recognition to be Async as follow:
sre.RecognizeAsync(RecognizeMode.Multiple);
instead of
sre.Recognize();
now when I say 'twenty-one'for example it gets this exception: base = {"Duplicated semantic key 'op1' in rule 'root."}
I know the problem is with the grammar, but I did made it repeated for the 'op1'. What am I missing ??
回答1:
I ended by using the text recognized to parse it by myself in
void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
I parsed the string recognized:
e.Result
Instead of
recoResult.Semantics["op1"].Value.ToString())
as the .Semantics object throws the exception mentioned above.
I really want to know the solution, if anyone is experienced with it
来源:https://stackoverflow.com/questions/30531288/targetinvocationexception-when-using-semanticresultkey