如何获得SAPI语音识别结果的替代方案

时间:2013-11-21 15:10:20

标签: sapi

在windows7平台上我可以使用System.Speech.Recognition.SpeechRecognitionEngine将语音转换为text.By SpeechRecognitionEngine当SpeechRecognized事件触发时我可以得到一些替代词,我可以向用户显示这些词以供选择。

void engine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    if (this.SpeechRecognized != null)
    {
       this.SpeechRecognized(this, new RecognizedResultEventArgs
            {
                Text = e.Result.Text,
                Alternates = new ReadOnlyCollection<string>(e.Result.Alternates.Select(p => p.Text).ToList())
            });
    }
}

顺便说一句,当我初始化SpeechRecognitionEngine实例时,我想加载一些特定的单词,而不是使用“DictationGrammar”。

我的程序有时需要在xp平台上运行。所以我想通过使用sapi5.1在xp操作系统上运行一个特定的vertion。

我已经阅读了sapi 5.1文档的一部分,然后我明白了:在sapi5.1中,我可以使用“命令和控制”的方式来做到这一点。但是当我使用“命令和控制”时,不能使用“Result.Alternates()”方法。那么,我怎样才能达到SpeechRecognitionEngine的相同效果?

我尝试了以下代码并且有一个com Eception:

public void RecoContext_Recognition(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
    {
        ISpeechPhraseProperty oItem;

        oItem = Result.PhraseInfo.Properties.Item(0);

        if ((System.Decimal)Result.PhraseInfo.GrammarId == grammarId)
        {
            if (this.SpeechRecognized != null)
            {
                RecognizedResultEventArgs e = new RecognizedResultEventArgs();
                e.Text = oItem.Name;

                // The following code throws an exception
                ISpeechPhraseAlternates alternates = Result.Alternates(10);
                List<string> s = new List<string>();
                foreach (ISpeechPhraseAlternate item in alternates)
                {
                    s.Add(item.RecoResult.PhraseInfo.Properties.Item(0).Name);
                }
                e.Alternates = new ReadOnlyCollection<string>(s);

                this.SpeechRecognized(this, e);
            }
        }
    }

有没有办法通过COM使用sapi来获取替代项?谢谢。

1 个答案:

答案 0 :(得分:0)

在SAPI(任何版本)中,命令和控制语法都没有替代品。只有听写语法才有替代。

相关问题