SpeechRecognizer EmulateRecognition Method (SAPI 5.3)
The EmulateRecognition method emulates recognition from a textual source rather than from a spoken source.
Using EmulateRecognition, applications can accept input from either speech or a textual source. All the events are fired back to the application exactly as if a normal recognition had taken place. The result phrase will have the semantic properties set in the same way as a spoken result. A recognition event will be produced only if the text actually parses through the active rules (if dictation is active, any text will parse).
SpeechRecognizer.EmulateRecognition( TextElements As Variant, [ElementDisplayAttributes As Variant = SDA_No_Trailing_Space], [LanguageId As Long = 0] )
- Specifies the elements of the phrase to to recognize. It must be one of two cases.
- If TextElements is a BSTR string then it is assumed that the elements in TextElements are assumed to be space delimited and DisplayAttributes parameter is ignored.
- If TextElements is an array of BSTR words then this parameter specifically lists each element in the phrase. ElementDisplayAttributes can be optionally specified as appropriate to the phrase's need.
- [Optional] Specifies the SpeechDisplayAttributes value for each word element. This value is specific to the language and usually determined by the speech recognition engine. By default the value is SDA_No_Trailing_Space and is considered standard for English languages. This parameter is only valid if an array of BSTRs for the TextElements parameter is specified. It must be one of three cases.
- If ElementDisplayAttributes is a NULL pointer, VT_NULL, or VT_EMPTY then SDA_No_Trailing_Space is assumed (which is the default).
- If it is a BSTR then it can be "" (empty string), " " (space), or " " (double space) and SAPI matches the SpeechDisplayAttribute uses it for all text elements. If an integer value (VT_I1 to VT_I4) is specified, then this value is the SpeechDisplayAttribute value and will is used for each element in the words array.
- If it is an array of integer values (VT_I1 to VT_I4) SAPI uses those values for the SpeechDisplayAttribute.
- [Optional] Specifies the LanguageId. This is the same as the Win32 Language Identifier (LANGID). By default the value is zero, indicating the system default is used.
Use this method (simulating speech) to test applications that use speech recognition. Also the restrictions in the parameters TextElements and ElementDisplayAttributes accommodate languages not using spaces to separate words.
The following Visual Basic form code demonstrates the use of EmulateRecognition. The application displays the successful recognition result of dictation. It also emulates speech by clicking the button.
To run this code, create a form with the following control:
- A command button called Command1
Paste this code into the Declarations section of the form.
The Form_Load procedure creates and activates a dictation grammar. Click Command1 to start emulated speech and display results.
Option Explicit Public WithEvents RC As SpSharedRecoContext Public myGrammar As ISpeechRecoGrammar Private Sub Form_Load() On Error GoTo EH Set RC = New SpSharedRecoContext Set myGrammar = RC.CreateGrammar myGrammar.DictationSetState SGDSActive EH: If Err.Number Then ShowErrMsg End Sub Private Sub Command1_Click() On Error GoTo EH RC.Recognizer.EmulateRecognition ("We the people") EH: If Err.Number Then ShowErrMsg End Sub Private Sub RC_Recognition _ (ByVal StreamNumber As Long, _ ByVal StreamPosition As Variant, _ ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _ ByVal Result As SpeechLib.ISpeechRecoResult) On Error GoTo EH MsgBox Result.PhraseInfo.GetText, vbInformation EH: If Err.Number Then ShowErrMsg End Sub Private Sub ShowErrMsg() ' Declare identifiers: Dim T As String T = "Desc: " & Err.Description & vbNewLine T = T & "Err #: " & Err.Number MsgBox T, vbExclamation, "Run-Time Error" End End Sub