You are reading help file online using chmlib.com
|
Interface: ISpeechPhraseReplacement
The following Visual Basic form code demonstrates the ISpeechPhraseReplacement interface and its relation to the ISpeechPhraseElement interface. To run this code, create a form with the following controls:
Paste this code into the Declarations section of the form.
This example is based on the ISpeechPhraseElement code example. Individual phrase elements from the recognition result are listed in the list box. The DisplayAttributes and DisplayText properties of each phrase element are shown. The DisplayAttributes and Text properties of the phrase replacements are displayed next to the elements which can be replaced by them.
Option Explicit
Const WAVEFILENAME = "C:\ISpeechPhraseReplacement.wav"
Dim R As SpeechLib.SpInprocRecognizer
Dim G As SpeechLib.ISpeechRecoGrammar
Dim F As SpeechLib.SpFileStream
Dim E As SpeechLib.ISpeechPhraseElement
Dim Rep As SpeechLib.ISpeechPhraseReplacement
Dim Reps As SpeechLib.ISpeechPhraseReplacements
Dim V As SpeechLib.SpVoice
Dim V2 As SpeechLib.SpVoice 'Plays the wave file back
Dim WithEvents C As SpeechLib.SpInProcRecoContext
Private Sub Command1_Click()
List1.Clear
Call SpeakToFile(Text1.Text, WAVEFILENAME)
F.Open WAVEFILENAME
Set R.AudioInputStream = F
End Sub
Private Sub Command2_Click()
List1.Clear
C.Recognizer.EmulateRecognition Text1.Text
End Sub
Private Sub Form_Load()
' Create Recognizer, RecoContext, Grammar, and Voice
Set R = New SpInprocRecognizer
Set C = R.CreateRecoContext
Set G = C.CreateGrammar(16)
Set V = New SpVoice
Set V.Voice = V.GetVoices("gender=male").Item(0)
Set V2 = New SpVoice
' Load Grammar with solitaire XML, set active
G.CmdLoadFromFile "c:\sol.xml", SLOStatic
G.CmdSetRuleIdState 0, SGDSActive 'Set C & C active
G.DictationSetState SGDSActive 'Set Dictation active
Text1.Text = "I spent twenty-eight dollars on route sixty-six"
Command1.Caption = "&Recognition"
Command2.Caption = "&EmulateRecognition"
End Sub
Private Sub SpeakToFile(ByVal strText As String, ByVal strFName As String)
Set F = New SpFileStream 'Create stream
F.Open strFName, SSFMCreateForWrite, True 'Open as the filename
Set V.AudioOutputStream = F 'Set voice output to file
V.Speak strText, SVSFIsXML 'Speak synchronously
F.Close 'Close file
End Sub
Private Sub C_Recognition(ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant, _
ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
ByVal Result As SpeechLib.ISpeechRecoResult)
Dim ECount As Integer 'Count of elements
Dim ii As Integer
Dim nn As Integer
Dim R1 As Integer, R2 As Integer
'Arrays with an entry for every PhraseElement
Dim arrElements() As String
Dim arrReplaces() As String
ECount = Result.PhraseInfo.Elements.Count - 1
ReDim arrElements(ECount)
ReDim arrReplaces(ECount)
'Load PhraseElements into an array
nn = 0
For Each E In Result.PhraseInfo.Elements
arrElements(nn) = E.DisplayAttributes & " " & E.DisplayText
nn = nn + 1
Next
'Load PhraseReplacements (if any) in an array
Set Reps = Result.PhraseInfo.Replacements
If Not Reps Is Nothing Then
For Each Rep In Reps
'Get the first and last elements
'replaced by this Replacement
R1 = Rep.FirstElement
R2 = R1 + Rep.NumberOfElements - 1
For ii = 0 To ECount
'Is element within the range of this replacement?
If (ii >= R1) And (ii <= R2) Then
arrReplaces(ii) = Rep.DisplayAttributes & " " & Rep.Text
End If
Next ii
Next
End If
Dim X As String
For ii = 0 To ECount
'Get PhraseElement and pad with blanks
X = arrElements(ii) & String(15, " ")
X = Left(X, 15)
'Put element index in front
X = "Element" & Format(ii, "00") & ": " & X
'Put Replacement elements at the end
X = X & arrReplaces(ii)
List1.AddItem X
Next ii
End Sub
Private Sub C_EndStream(ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant, _
ByVal StreamReleased As Boolean)
'Recognition uses the Filestream, EmulateReco does not
If ActiveControl.Caption = "&Recognition" Then
F.Close
DoEvents
F.Open WAVEFILENAME
V2.SpeakStream F
F.Close
End If
List1.AddItem ""
End Sub
You are reading help file online using chmlib.com
|