最近在做SAPI方面的工作,比較詳細的中文資料不多,遇到各種問題,本來想著做完了專案總結一下,今天看到這篇文章,對於SAPI載入識別語法方面的描述十分詳細,先轉過來做個備份,謝謝原文博主:djyangmaowei,原文地址:http://blog.csdn.net/djyangmaowei/article/details/5384942
應用程式可以利用SpSharedRecoContext介面建立不同的與語音識別引擎的連線。每一個連線都可以使用各自的事件並且使用不同的語音識別語法(grammars)。每一個基於SAPI語音識別的應用程式必須具有至少一個SpSharedRecoContext介面。
第一種方法: 自己定義Grammar
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Diagnostics;
using SpeechLib;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
private SpeechLib.ISpeechGrammarRule menuRule = null;
private SpeechLib.SpSharedRecoContext objRecoContext;
private ISpeechRecoGrammar grammar;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// 得到一個RecoContext例項.
objRecoContext = new SpeechLib.SpSharedRecoContext();
// 指派一個事件給Hypothesis Event(中間層暫定的識別,即,初級的,臨時的識別).
objRecoContext.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(Hypo_Event);
// 指派一個事件給語音識別.
objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(Reco_Event);
//建立grammer例項.
grammar = objRecoContext.CreateGrammar(0);
label1.Text = "Speak Out one of the following./r/n1. 人民 2. 馬克思 3. 孫中山 4. 恩格斯/r/n5. 楊茂巍 6. 王芳 7. 世界 8. 成都";
//啟用選單命令.
menuRule = grammar.Rules.Add("MenuCommands", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);
object PropValue = "";
menuRule.InitialState.AddWordTransition(null, "人民", " ", SpeechGrammarWordType.SGLexical, "人民", 1, ref PropValue, 1.0F);
menuRule.InitialState.AddWordTransition(null, "馬克思", " ", SpeechGrammarWordType.SGLexical, "馬克思", 2, ref PropValue, 1.0F);
menuRule.InitialState.AddWordTransition(null, "孫中山", " ", SpeechGrammarWordType.SGLexical, "孫中山", 3, ref PropValue, 1.0F);
menuRule.InitialState.AddWordTransition(null, "恩格斯", " ", SpeechGrammarWordType.SGLexical, "恩格斯", 4, ref PropValue, 1.0F);
menuRule.InitialState.AddWordTransition(null, "楊茂巍", " ", SpeechGrammarWordType.SGLexical, "楊茂巍", 5, ref PropValue, 1.0F);
menuRule.InitialState.AddWordTransition(null, "王芳 ", " ", SpeechGrammarWordType.SGLexical, "王芳 ", 6, ref PropValue, 1.0F);
menuRule.InitialState.AddWordTransition(null, "世界", " ", SpeechGrammarWordType.SGLexical, "世界", 7, ref PropValue, 1.0F);
menuRule.InitialState.AddWordTransition(null, "成都", " ", SpeechGrammarWordType.SGLexical, "成都", 8, ref PropValue, 1.0F);
grammar.Rules.Commit();
grammar.CmdSetRuleState("MenuCommands", SpeechRuleState.SGDSActive);
}
private void Reco_Event(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
{
textBox1.Text = Result.PhraseInfo.GetText(0, -1, true);
}
private void Hypo_Event(int StreamNumber, object StreamPosition, ISpeechRecoResult Result)
{
textBox2.Text = Result.PhraseInfo.GetText(0, -1, true);
}
}
}