中文语音识别的方法

中文语音识别的方法:

先把该语音识别的类源代码贴在下面,然后再做说明:

public class SpRecognition
{
private static SpRecognition _Instance = null ;
private SpeechLib.ISpeechRecoGrammar isrg ;
private SpeechLib.SpSharedRecoContextClass ssrContex =null;
private System.Windows.Forms.Control cDisplay ;
private SpRecognition()
{
ssrContex = new SpSharedRecoContextClass() ;
isrg = ssrContex.CreateGrammar(1) ;
SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandler recHandle = new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition) ;
ssrContex.Recognition += recHandle ;
}

public void BeginRec(Control tbResult)
{
isrg.DictationSetState(SpeechRuleState.SGDSActive) ;
cDisplay = tbResult ;
}
public static SpRecognition instance()
{
if (_Instance == null)
_Instance = new SpRecognition() ;
return _Instance ;
}

public void CloseRec()
{
isrg.DictationSetState(SpeechRuleState.SGDSInactive) ;
}
private void ContexRecognition(int iIndex,object obj,SpeechLib.SpeechRecognitionType type,SpeechLib.ISpeechRecoResult result)
{
cDisplay.Text += result.PhraseInfo.GetText(0,-1,true) ;
}
}


我们定义了ssrContex 和isrg为语音识别的上下文和语法,通过设置isrg的DictationSetState方法,我们可以开始或结束识别,在上面的程序中是BeginRec和CloseRec方法。cDisplay 是我们用来输出识别结果的地方,为了能够在大部分控件上都可以显示结果,我用了一个Control 类来定义它。当然,每次语音识别后都会触发ISpeechRecoContextEvents_RecognitionEventHandler 事件,我们定义了一个这样的方法ContexRecognition来响应事件,并且在这个方法里输出识别结果。


相关文档
最新文档