Speech SDK 5.1--No.4:用xml描述语法规则初探

前端之家收集整理的这篇文章主要介绍了Speech SDK 5.1--No.4:用xml描述语法规则初探前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 

最悲哀的莫过于,我不知道microsoft speech sdk 5.1的原理,却要依葫芦画瓢做一个系统,没时间先去弄懂microsoft speech sdk 5.1了。本文需要读者有一定的microsoft speech sdk 5.1基础。我也不知道xml规则,但是看了下面的函数和xml文档,我大致知道怎么回事。希望能给其他人一些帮助。

两个重要的函数

void speechControl::RecoEvent()
{
	USES_CONVERSION;
    CSpEvent event;

    // Process all of the recognition events
    while (event.GetFrom(m_cpRecoCtxt) == S_OK)
    {
        switch (event.eEventId)
        {
            case SPEI_RECOGNITION:
                // There may be multiple recognition results,so get all of them
                {
					CSpDynamicString dstrText;
					if (SUCCEEDED(event.RecoResult()->GetText(SP_GETWHOLEPHRASE,SP_GETWHOLEPHRASE,TRUE,&dstrText,NULL)))
                    {						
	//					MessageBox(m_hWnd,(char*)dstrText.Copy(),"recognized",0);

						m_cpVoice->Speak( dstrText,SPF_ASYNC,NULL);
					}
					ExecuteCommand(event.RecoResult());
                }
                break;
        }
    }
}
void speechControl::ExecuteCommand(ISpPhrase *pPhrase)
{
	HWND hWnd=m_hWnd;
    
	SPPHRASE *pElements;
    
	// Get the phrase elements,one of which is the rule id we specified in
    // the grammar.  Switch on it to figure out which command was recognized.
    if (SUCCEEDED(pPhrase->GetPhrase(&pElements)))
    {        
        switch ( pElements->Rule.ulId )
        {
			case VID_Hello:
			case VID_Stops:
			case VID_Out:
            case VID_Navigation:
            {
                switch( pElements->pProperties->vValue.ulVal )
                {
					case VID_Feifei:
						m_cpCmdGrammar->SetRuleIdState(VID_Navigation,SPRS_ACTIVE);
						m_cpCmdGrammar->SetRuleIdState(VID_Stops,SPRS_ACTIVE);
						m_cpCmdGrammar->SetRuleIdState(VID_Out,SPRS_ACTIVE);
						MessageBox(hWnd,"OK,madam,you can control me!",NULL,0);
//						m_cpVoice->Speak(L"OK,NULL);
						break;
					case VID_Out:
						m_cpCmdGrammar->SetRuleIdState(VID_Navigation,SPRS_INACTIVE);
						m_cpCmdGrammar->SetRuleIdState(VID_Stops,SPRS_INACTIVE);
						m_cpCmdGrammar->SetRuleIdState(VID_Out,SPRS_INACTIVE);
						break;
                    case VID_Ahead:
                   //     PostMessage( hWnd,WM_GOTOCOUNTER,NULL );                        
						MessageBox(hWnd,"turn/go ahead","ahead",0);
						break;
					case VID_Left:
						MessageBox(hWnd,"turn/go left","left",0);
						break;
					case VID_Back:
						MessageBox(hWnd,"turn/go back","back",0);
						break;
					case VID_Right:
						MessageBox(hWnd,"turn/go right","right",0);
						break;                
					case VID_Stop:
						MessageBox(hWnd,"turn/go stop","stop",0);
						break;					
				}
			break;
            }
			case VID_Place:
			{
                switch( pElements->pProperties->vValue.ulVal )
                {
                    case VID_Ahead:
                   //     PostMessage( hWnd,0);
						break;
                
					case VID_Stop:
						MessageBox(hWnd,0);
						break;
                }
			}
            break;
        }
        // Free the pElements memory which was allocated for us
        ::CoTaskMemFree(pElements);
    }
}


对应的xml文件:

<?xml version="1.0" encoding="GB2312"?>
<GRAMMAR LANGID="804">
	<DEFINE>
		<ID NAME="VID_Hello" VAL="100"/>
        <ID NAME="VID_Ahead" VAL="1"/>
        <ID NAME="VID_Back" VAL="2"/>
        <ID NAME="VID_Left" VAL="3"/>
        <ID NAME="VID_Right" VAL="4"/>
        <ID NAME="VID_Stop" VAL="5"/>
		<ID NAME="VID_Stops" VAL="6"/>
		<ID NAME="VID_Tiantian" VAL="101"/>
		<ID NAME="VID_Haha" VAL="102"/>
		<ID NAME="VID_Feifei" VAL="103"/>
		<ID NAME="VID_Out" VAL="200"/>
        <ID NAME="VID_Place" VAL="253"/>
        <ID NAME="VID_Navigation" VAL="254"/>
    </DEFINE>
	<RULE ID="VID_Hello" TOPLEVEL="ACTIVE">
	<O>你好</O>
	<P>
		<L PROPID="VID_Hello" >
			<P  VAL="VID_Tiantian">天天</P>
			<P  VAL="VID_Haha">哈哈</P>
			<P  VAL="VID_Feifei">飞飞</P>
		</L>
    </P>
    </RULE>
	<RULE ID="VID_Out" TOPLEVEL="INACTIVE">
	<P>
		<L PROPID="VID_Out" >
			<P  VAL="VID_Out">好了</P>
			<P  VAL="VID_Out">拜拜</P>
			<P  VAL="VID_Out">再见</P>
			<P  VAL="VID_Out">结束</P>
		</L>
    </P>
    </RULE>
    <RULE ID="VID_Navigation" TOPLEVEL="INACTIVE">
	<O>请</O>
	<P>
		<L>
			<P>朝</P>
			<P>向</P>
		</L>
    </P>
	<L>
        <RULEREF REFID="VID_Place" />
		<RULEREF REFID="VID_Stops" />
	</L>
    </RULE>
    <RULE ID="VID_Place"  >
        <L PROPID="VID_Place" >
		<P>
				<P PROPID="VID_Place" VAL="VID_Ahead">前</P>
				<O>
				<L>
						<P>走</P>
						<p>进</P>
						<p>跑</P>
				</L>
				</O>
		</P>            
        <P VAL="VID_Back">后退</P>
 		<P>
				<P PROPID="VID_Place" VAL="VID_Left">左</P>
				<O>
				<L>
						<P>转</P>
						<p>拐</P>
						<p>跑</P>
				</L>
				</O>
		</P>
 		<P>
				<P PROPID="VID_Place" VAL="VID_Right">右</P>
				<O>
				<L>
						<P>转</P>
						<p>拐</P>
						<p>跑</P>
				</L>
				</O>
		</P>
	    <P VAL="VID_Stop">
			<L>
				<P>停下来</P>
				<p>停止</P>
			</L>
		</P>
        </L>
    </RULE>
    <RULE ID="VID_Stops" TOPLEVEL="INACTIVE">
		<P PROPID="VID_Stops" VAL="VID_Stop">停</P>
		<L>
				<P>下来</P>
				<p>止前进</P>
				<p>吧</P>
        </L>
    </RULE>
</GRAMMAR>
原文链接:https://www.f2er.com/xml/300559.html

猜你在找的XML相关文章