2
votes

I'm using c# and the System.Speech.Recognition to load a couple of simple grammars I defined. When I say phrases matching the grammars, the engine recognizes the grammar correctly with confidences around 0.95.

But when I pronounce words that are not even in the grammar (even from difference languages or gibberish), the engines randomly returns a match to a grammar with random text never pronounced and still with high confidence like 0.92.

Is there something I need to set in the SpeechRecognitionEngine object or in each Grammar object to avoid this problem?

1

1 Answers

2
votes

I think I found a solution that works for me but still it would be nice to find a more elegant one if exists:

I define a dictation grammar and a "placeholder". Then I load my grammars and disabled them immediately.

using System.Speech.Recognition;
...

private DictationGrammar dictationGrammar;
private Grammar placeholderGrammar;
private List<Grammar> commands;

public void Initialize()
{
    dictationGrammar = new DictationGrammar();
    recognizer.LoadGrammarAsync(dictationGrammar);

    var builder = new GrammarBuilder();
    builder.Append("MYPLACEHOLDER");        
    placeholderGrammar = new Grammar(builder);
    recognizer.LoadGrammarAsync(placeholderGrammar);

    commands = new List<Grammar>();

    foreach (var grammar in grammarManager.GetGrammars())
    {
        commands.Add(grammar);           
        grammar.Enabled = false;
        recognizer.LoadGrammarAsync(grammar);
    }
}

Then on the speechRecognized event I put the logic that if placeholder is recognized then enable the commands. If a command is recognized the re-enable the dictation and disable all commands:

private async void speechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    if (e.Result.Grammar == placeholderGrammar)
    {
        //go to command mode
        placeholderGrammar.Enabled = false;
        dictationGrammar.Enabled = false;

        foreach (var item in commands)
            item.Enabled = true;
    }
    else if (commands.Any(x => e.Result.Grammar == x))
    {
        Do_something_with_recognized_command("!!");

        //go back in normal mode
        placeholderGrammar.Enabled = true;
        dictationGrammar.Enabled = true;
    }else {//this is dictation.. nothing to do}
}