zoukankan      html  css  js  c++  java
  • Sapi 添加语法的文章(转载)

    最近在做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>
    如要源代码 请留下邮箱 
  • 相关阅读:
    性能测试四十五:性能测试策略
    性能测试四十四:性能优化思路
    性能测试四十三:数据库监控的指标
    性能测试四十二:sql案例之联合索引最左前缀
    性能测试四十一:sql案例之慢sql配置、执行计划和索引
    性能测试四十:Mysql存储过程造数据
    性能测试三十九:Jprofiler分析CPU过高和响应时间长的问题
    delphiIDE 把 window 桌面改慢后的 还原方法
    TStringList 善用 value['names'] 即使value 是带=号的值都没有关系呵呵 ,我靠 强,以后就用这个了,key=value首选
    TStringList,快速解析 查找测试。。。很有用,再也不用 FOR 循环了
  • 原文地址:https://www.cnblogs.com/yuanyuan/p/3436811.html
Copyright © 2011-2022 走看看