SAPI settings for speech recognition grammar

Source: Internet
Author: User
Tags gettext sapi

Applications can use the Spsharedrecocontext interface to create different connections to the speech recognition engine. Each connection can use its own event and use a different speech recognition grammar (grammars). Every application that is based on SAPI speech recognition must have at least one Spsharedrecocontext interface.

The first method: Define your own 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)

{

Get a Recocontext instance.

Objrecocontext = new Speechlib.spsharedrecocontext ();

Assign an event to the hypothesis event (intermediate level tentative recognition, i.e., primary, temporary identification).

Objrecocontext.hypothesis + = new _ispeechrecocontextevents_hypothesiseventhandler (hypo_event);

Assigns an event to speech recognition.

Objrecocontext.recognition + = new _ispeechrecocontextevents_recognitioneventhandler (Reco_Event);

Creates an grammer instance.

Grammar = Objrecocontext.creategrammar (0);

Label1. Text = "Speak out one of the following./r/n1. The People 2. Marx 3. Sun Yat-sen 4. Engels/r/n5. Yang Mao 6. Wang Fang 7. World 8. Chengdu ";

Activates the menu command.

Menurule = grammar. Rules.add ("Menucommands", Speechruleattributes.sratoplevel | Speechruleattributes.sradynamic, 1);

Object propvalue = "";

MenuRule.InitialState.AddWordTransition (NULL, "People", "" ", Speechgrammarwordtype.sglexical," people ", 1, ref propvalue, 1.0F) ;

MenuRule.InitialState.AddWordTransition (NULL, "Marx", "" ", Speechgrammarwordtype.sglexical," Marx ", 2, ref propvalue, 1.0F);

MenuRule.InitialState.AddWordTransition (NULL, "Sun Yat-sen", "", Speechgrammarwordtype.sglexical, "Sun Yat-sen", 3, ref propvalue, 1.0F);

MenuRule.InitialState.AddWordTransition (NULL, "Engels", "" ", Speechgrammarwordtype.sglexical," Engels ", 4, ref propvalue, 1.0F);

MenuRule.InitialState.AddWordTransition (null, "Yang Mao", "" ", Speechgrammarwordtype.sglexical," Yang Mao ", 5, ref propvalue, 1.0F);

MenuRule.InitialState.AddWordTransition (null, "Wang Fang", "", Speechgrammarwordtype.sglexical, "Wang Fang", 6, ref propvalue, 1.0F) ;

MenuRule.InitialState.AddWordTransition (NULL, "World", "" ", Speechgrammarwordtype.sglexical," World ", 7, ref propvalue, 1.0F) ;

MenuRule.InitialState.AddWordTransition (NULL, "Chengdu", "" ", Speechgrammarwordtype.sglexical," Chengdu ", 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);

}

}

The second method: Do not define grammar use SAPI to automatically recognize input speech accuracy is not high public class Sprecognition      {       &NBSP ;  private static sprecognition _instance = null;           private Speechlib.ispeechrecogrammar ISRG;           private speechlib.spsharedrecocontextclass Ssrcontex =null;           private System.Windows.Forms.Control CDisplay  ;           private sprecognition ()          {     &N Bsp        ssrcontex = new Spsharedrecocontextclass ();              &NBSP;ISRG = Ssrcontex.creategrammar (1);                         speechlib._ispeechrecocontexteve Nts_recognitioneventhandler Rechandle =                    New _ISPEECHR EcocontextevEnts_recognitioneventhandler (contexrecognition);               ssrcontex.recognition + = Rechandle;         }          public void Beginrec (Control tbresult)          {                         &NBSP;ISR G.dictationsetstate (speechrulestate.sgdsactive);               cdisplay = Tbresult;         }          public static Sprecognition instance ()          {              if (_instance = = null)      &N Bsp             _instance = new Sprecognition ();               return _instance;         }          public void Closerec ()          {             &NBSP;ISRG. Dictationsetstate (speechrulestate.sgdsinactive);         }           private void contexrecognition (int iindex,obje CT obj,speechlib.speechrecognitiontype type,speechlib.ispeechrecoresult result)          {& nbsp;             cdisplay.text + = result. Phraseinfo.gettext (0,-1,true);         }       } third method: External read XML grammar (c://1.xml)   Private Speechl Ib. Spsharedrecocontext SSRC;         private Ispeechrecogrammar SRG;         private void Button1_Click (object sender, EventArgs e)         {            &NBSP;SSRC = new Spsharedrecocontext ();            &NBSP;SRG = ssrc. Creategrammar (1);  &nbSp         &NBSP;SRG. Cmdloadfromfile ("C://1.xml", speechloadoption.slodynamic);//read-in rules           //SSRC. Eventinterests = speechrecoevents.sreallevents;//is described in "voice events"            &NBSP;SSRC. Recognition + = new _ispeechrecocontextevents_recognitioneventhandler (ssrc_recognition);//Add Recognition Events                 &NBSP;SSRC. Hypothesis + = new _ispeechrecocontextevents_hypothesiseventhandler (hypo_event);            &NBSP;SRG. Cmdsetrulestate (SRG. Rules.item (0). Name, speechrulestate.sgdsactive);//Activation rules                }    & nbsp    private void button2_click (object sender, EventArgs e)         {                   }         private void button3_click (objec T sender, EventArgs e)  &nBsp      {                    }    &nbsp ;    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 Strea Mposition, Ispeechrecoresult Result)         {            tex Tbox2.text = Result.PhraseInfo.GetText (0,-1, true);         } 1.xml <?xml version= "1.0" encoding= "gb2312"?>  <grammar langid= "804" & gt;  <rule name= "command" toplevel= "ACTIVE" >  <L>  <P> end of password </P>  <P> zoom in </P>  <P>Home</P>  <p>end</p>  <P> prev </P>  <P> next </P>  <P> up </P>  <P> down </P>  <P> left window </P>  <P> right window </P>  <P> close </P>  <P> undo </P>  <P> Refresh </P>  </L>  </RULE>  </GRAMMAR> If you want to source code please leave your email  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.