Jump to content
Sign in to follow this  
oMBRa

Word's Recognition

Recommended Posts

oMBRa

Hi, I was trying to do script for recognizing word's decided dynamically during run-time. I have tried doing it in various ways with no result.

it will work if you say "open one", but it won't for the word added during run-time.

Here is the script:

$oRecoContext = ObjCreate('SAPI.SpSharedRecoContext')
ObjEvent($oRecoContext, 'RecoContext_')
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")
ConsoleWrite('Error: ' & @error & @CRLF)

$oGrammar = $oRecoContext.CreateGrammar()
$oGrammar.CmdLoadFromFile(@DesktopDir & '\Dynamic Grammar.xml', 1)

$oTopRule = $oGrammar.Rules.FindRule('OPEN')
$oFileRule = $oGrammar.Rules.FindRule('FILE_NAME')
;~ $oRule = $oGrammar.Rules.Add('HI', 1, 1)
$oState = $oFileRule.InitialState()
;~ $state2 = $oRule.AddState()
$oState.AddwordTransition($oState, 'desk')
$oGrammar.CmdSetRuleState('OPEN', 1)
$oGrammar.Rules.Commit()

$oGrammar.CmdSetRuleState('OPEN', 1)
;$oTransitions = $oState.Transitions
;$oGrammar.CmdSetRuleState('OPEN', 1)
;$oTransition = $oTransitions.Item(0)
;$a = $oTransition.PropertyNAME()
;MsgBox(0, '', $a)


While 1
    Sleep(10)
WEnd


Func RecoContext_FalseRecognition($iStreamNumber, $vStreamPosition, $iResult)
    ConsoleWrite("(no recognition)" & @CRLF)
EndFunc   ;==>RecoContext_FalseRecognition

Func RecoContext_Recognition($iStreamNumber, $vStreamPosition, $iRecognitionType , $iResult)
    ConsoleWrite($iResult.PhraseInfo.GetText() &@CRLF)
EndFunc   ;==>RecoContext_Recognition

Func RecoContext_StartStream($iStreamNumber, $vStreamPosition)
    ConsoleWrite(Number($iStreamNumber) & @CRLF)
EndFunc   ;==>RecoContext_StartStream



Func MyErrFunc()

  Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"      & @CRLF  & @CRLF & _
             "err.description is: "    & @TAB & $oMyError.description    & @CRLF & _
             "err.windescription:"     & @TAB & $oMyError.windescription & @CRLF & _
             "err.number is: "         & @TAB & hex($oMyError.number,8)  & @CRLF & _
             "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
             "err.scriptline is: "     & @TAB & $oMyError.scriptline     & @CRLF & _
             "err.source is: "         & @TAB & $oMyError.source         & @CRLF & _
             "err.helpfile is: "       & @TAB & $oMyError.helpfile       & @CRLF & _
             "err.helpcontext is: "    & @TAB & $oMyError.helpcontext _
            )

    Local $err = $oMyError.number
    If $err = 0 Then $err = -1

    $g_eventerror = $err  ; to check for after this function returns
    Exit
Endfunc

here is the XML file:

(save it to a txt file and rename it "Dynamic Grammar.xml")

<GRAMMAR LANGID="409">
   <RULE NAME="OPEN" TOPLEVEL="ACTIVE">
      <PHRASE>open</PHRASE>
      <RULEREF NAME="FILE_NAME" PROPNAME="NAME"/>
   </RULE>
   <RULE NAME="FILE_NAME" DYNAMIC="TRUE">
   <L PROPNAME="RANDOM_NAME">
      <P VAL="7">one</P>
      </L>
   </RULE>
</GRAMMAR>

I have another question: the SAPI 5.1 documentation says that the "SpSharedRecoContext" implements the "ISpRecoContext" interfaces... but, how can I use it?

Share this post


Link to post
Share on other sites
Juvigy

try this workaround for nothing :

Local $oVBS = ObjCreate("ScriptControl") 
$oVBS.language = "VBScript" 
Global Const $Nothing = $oVBS.eval("Nothing") 
$oVBS = $Nothing

Share this post


Link to post
Share on other sites
oMBRa

try this workaround for nothing :

Local $oVBS = ObjCreate("ScriptControl") 
$oVBS.language = "VBScript" 
Global Const $Nothing = $oVBS.eval("Nothing") 
$oVBS = $Nothing

wow, it worked, nice workaround... thx

Share this post


Link to post
Share on other sites
playlet

---

Edited by playlet

Share this post


Link to post
Share on other sites
oMBRa

Can you please post the whole working script? I'm very interested in this, are you planning to post something in the examples forum?

Playlet

Below there is a little example. In order to use it you obviously need the SAPI SDK 5.1.

Just say at the microphone "run notepad" and if the phrase is recognized, notepad should open.

$oRecoContext = ObjCreate('SAPI.SpSharedRecoContext')
If @error Then
    MsgBox(0, '', 'Object Creation Failed')
    Exit
EndIf

ConsoleWrite('You can speak now' & @CRLF)
ObjEvent($oRecoContext, 'RecoContext_')

Global $oVBS = ObjCreate("ScriptControl")
$oVBS.Language = "VBScript"
Global Const $oNothing = $oVBS.Eval("Nothing")

$oGrammar = $oRecoContext.CreateGrammar()

$oTopRule = $oGrammar.Rules.Add('RUN', 1, 0)
$oWordsRule = $oGrammar.Rules.Add('WORDS', BitOR(1, 32), 1)

$oAfterCmdState = $oTopRule.AddState()

$oTopRule.InitialState.AddWordTransition($oAfterCmdState, 'run')
$oAfterCmdState.AddRuleTransition($oNothing, $oWordsRule)

$oWordsRule.InitialState.AddWordTransition($oNothing, 'notepad')
;~ $oWordsRule.InitialState.AddWordTransition($oNothing, 'whatever')

$oGrammar.Rules.Commit()

$oGrammar.CmdSetRuleState('RUN', 1)


While 1
    Sleep(10)
WEnd


Func RecoContext_FalseRecognition($iStreamNumber, $vStreamPosition, $iResult)
    ConsoleWrite("(no recognition)" & @CRLF)
EndFunc   ;==>RecoContext_FalseRecognition

Func RecoContext_Recognition($iStreamNumber, $vStreamPosition, $iRecognitionType, $iResult)
    ConsoleWrite($iResult.PhraseInfo.GetText() & @CRLF)
    If $iResult.PhraseInfo.Elements.Item(1).DisplayText = 'notepad' Then
        Run('notepad.exe')
    EndIf
EndFunc   ;==>RecoContext_Recognition

Share this post


Link to post
Share on other sites
playlet

---

Edited by playlet

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×