using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeechLib;
using System.Windows.Forms;
namespace [email protected]
{
public class SpRecognition
{
public SpeechLib.ISpeechRecoGrammar isrg;
public SpeechLib.SpSharedRecoContextClass ssrContex = null;
public System.Windows.Forms.Control cDisplay;
public SpRecognition(string[] gh) //加载关键词
{
ssrContex = new SpSharedRecoContextClass();
isrg = ssrContex.CreateGrammar(0);
SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandler recHandle =
new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition);
ssrContex.Recognition += recHandle;
SpeechLib.ISpeechGrammarRule menuRule = isrg.Rules.Add("123", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);
object PropValue = "";
foreach (string ff in gh)
{
menuRule.InitialState.AddWordTransition(null, ff, " ", SpeechGrammarWordType.SGLexical, ff, 1, ref PropValue, 1.0F);
}
isrg.Rules.Commit();
isrg.CmdSetRuleState("123", SpeechRuleState.SGDSActive);
isrg.DictationLoad("", SpeechLoadOption.SLOStatic);
isrg.DictationSetState(SpeechRuleState.SGDSActive);
}
public void BeginRec(Control tbResult)
{
cDisplay = tbResult;
}
public void CloseRec()
{
ssrContex = null;
cDisplay = null;
}
private void ContexRecognition(int iIndex, object obj, SpeechLib.SpeechRecognitionType type, SpeechLib.ISpeechRecoResult result)
{
cDisplay.Text = result.PhraseInfo.GetText(0, -1, true);
}
}
}
类的调用
SpRecognition ff;
string[] dd={"尹成","华东","玉辉","};
ff = new SpRecognition(dd);
ff.BeginRec(richTextBox1);
ff.CloseRec();
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow