最近在做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);

}

}

}

第二种方法:
不定义Grammar 利用SAPI自动识别输入语音 准确率不高
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) ;
         }
 
     }
第三种方法:
外界读入xml grammar (c://1.xml)
 private SpeechLib.SpSharedRecoContext ssrc;
        private ISpeechRecoGrammar srg;
        private void button1_Click(object sender, EventArgs e)
        {
            ssrc = new SpSharedRecoContext();
            srg = ssrc.CreateGrammar(1);
            srg.CmdLoadFromFile("c://1.xml", SpeechLoadOption.SLODynamic);//读入规则
           // ssrc.EventInterests = SpeechRecoEvents.SREAllEvents;//在"语音事件"中有说明
            ssrc.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(ssrc_Recognition);//添加识别事件     
            ssrc.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(Hypo_Event);
            srg.CmdSetRuleState(srg.Rules.Item(0).Name, SpeechRuleState.SGDSActive);//激活规则       
        }
        private void button2_Click(object sender, EventArgs e)
        {
          
        }
        private void button3_Click(object sender, EventArgs e)
        {
           
        }
        void ssrc_Recognition(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);
        }
 
1.xml
<?xml version="1.0" encoding="gb2312" ?> 
<GRAMMAR LANGID="804"> 
<RULE NAME="命令" TOPLEVEL="ACTIVE"> 
<L> 
<P>口令结束</P> 
<P>放大</P> 
<P>Home</P> 
<p>End</p> 
<P>上一页</P> 
<P>下一页</P> 
<P>向上</P> 
<P>向下</P> 
<P>左窗口</P> 
<P>右窗口</P> 
<P>关闭</P> 
<P>撤消</P> 
<P>刷新</P> 
</L> 
</RULE> 
</GRAMMAR>
如要源代码 请留下邮箱 

Sapi 添加语法的文章(转载)的更多相关文章

  1. 【前端】向blog或网站中添加语法高亮显示代码方法总结

    向blog或网站中添加语法高亮显示的代码方法总结 文章目录 预备知识 目标 第一类方法:嵌入 第二类方法:外部引用 第三类方法:忽略HTML和PHP 最近在写代码时遇到一个问题,就是如何让代码像在ID ...

  2. 文章转载利用border、transparent实现微风

    微风效果预览 微风源码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...

  3. 文章转载至CSDN社区罗升阳的安卓之旅,原文地址:

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6720261 前面我们在分析Activity启动 ...

  4. thymeleaf 添加语法提示

    thymeleaf 添加语法提示: xmlns:th="http://www.thymeleaf.org"

  5. wordpress添加post_type自定义文章类型

    wordpress很强大,能当博客也能进行二次开发出很完善的内容管理系统满足企业运营需求,比如可以添加products产品模型.汽车模型等,如何实现呢?添加post_type自定义文章类型就可以了 p ...

  6. 用 highlight.js 为文章中的代码添加语法高亮

    来源:http://www.ghostchina.com/adding-syntax-highlighting-to-ghost-using-highlight-js/ --------------- ...

  7. Perl的基本语法<总结> (转载)

    前言:这篇文章是花了我很多时间.费了我很多心血才完成的,虽然连我自己都觉得无法达到尽善尽美的境界,但希望能帮助大家入门,稍微了解到Perl 到底是个什么样的东西,Perl到底有那些强大的功能,那么这篇 ...

  8. Eclipse 如何添加 更换字体(转载)

    1. 打开eclipse-->Window-->Preferences-->General-->appearance-->Colors and Fonts, 点开后选择B ...

  9. Android性能优化文章转载

    今天看到几篇比较好的文章就转了!(链接如下) 转载注明出处:Sunzxyong Android性能优化之Bitmap的内存优化 Android性能优化之常见的内存泄漏 Android最佳实践之Syst ...

随机推荐

  1. VS.PHP详细破解教程,用Visual Studio编写PHP代码插件PhpTools

    一.准备文件:(下载地址:http://download.csdn.net/detail/wulang1988/9662363) Default.aspx是解决在线破解文件:PhptoolCracke ...

  2. Apache shiro

    Shiro 是一个 Apache Incubator 项目,旨在简化身份验证和授权. 学习博客:http://jinnianshilongnian.iteye.com/blog/2018398

  3. java生成解析xml的另外两种方法JAXB

     JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反 ...

  4. iOS开发备忘录:实现多StoryBoard之间跳转

    iOS项目中可以将同一业务流程的页面归置到一个StoryBoard中,项目中必然会包含多个StroryBoard,可以利用跳转,实现项目的不同业务流程页面间的跳转切换. 实现思路: 1,项目(Proj ...

  5. 配置ini指定eclipse启动JDK版

    eclipse mars1 需要JDK 1.7+ 解决方案: 改eclipse.ini配置文件 -startupplugins/org.eclipse.equinox.launcher_1.3.100 ...

  6. mac 自动配置java版本

    首先输入命令:vi .bash_profile ,添加如下内容: # Mac默认 JDK (Mac默认自带了一个jdk6版本) export JAVA_6_HOME=`/usr/libexec/jav ...

  7. CSS基础(一):开篇

    背景 HTML是一种超文本标记语言,用来定义文档的结构和内容,例如标题.段落和列表等等,而文档内容如何渲染.如何展示,这就需要样式来修饰了.CSS正是可以与HTML很好地结合.如果将HTML比作水,那 ...

  8. PyCharm4注册码--软件安装

    软件连接:http://www.jetbrains.com/pycharm/ PyCharm4注册码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  9. ODAC(V9.5.15) 学习笔记(十五)数据离线模式

    数据离线模式(Disconnected Mode)是指数据库只有在需要的时候才连接,数据的处理放在客户端内存缓冲区中完成.这样做最大的好处是减少了网络资源依赖,对数据库服务器的资源开销和压力也减少.如 ...

  10. AndroidTouchGalleryLibrary 优化

    AndroidTouchGalleryLibrary 是一个非常好用的库, 但是使用的时候,需要小心处理,容易引发OutOfMemoryError,同时使用UrlTouchImageView的时候, ...