为什么微软语音识别系统与语法匹配,即使没有说过该语法的单词?

时间:2017-04-04 09:39:08

标签: c# speech-recognition grammar .net-4.6 system.speech.recognition

我正在使用c#和System.Speech.Recognition来加载我定义的几个简单语法。当我说出与语法匹配的短语时,引擎会正确识别语法,信心大约为0.95。

但是当我发出甚至不在语法中的单词时(即使是来自差异语言或乱码),引擎会随机地将匹配返回到语法,其中随机文本从未发音,仍然具有0.92的高可信度。

我是否需要在SpeechRecognitionEngine对象或每个语法对象中设置以避免此问题?

1 个答案:

答案 0 :(得分:2)

我认为我找到了一个适合我的解决方案,但如果存在,找到更优雅的解决方案仍然会很好:

我定义了一个听写语法和一个"占位符"。然后我加载我的语法并立即禁用它们。

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);
    }
}

然后在speechRecognized事件中,我提出了如果识别占位符则启用命令的逻辑。如果识别出命令,则重新启用听写并禁用所有命令:

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}
}