You are reading help file online using chmlib.com
|
Interface: ISpeechRecoGrammar
The SetWordSequenceData method defines a word sequence buffer for use by the speech recognition (SR) engine.
Some recognition grammars specify every word and phrase that they are capable of recognizing. The Solitaire grammar is one of these; it contains only a small amount of data, and all its data elements were known to the grammar designer. But it would be impractical to create such a grammar for a speech-enabled order-entry system handling several hundred thousand inventory items.
In order to eliminate the need to specify every recognizable word in a grammar, yet still maintain the high quality of rule-based recognition, SAPI provides applications with a means to link the grammar to application-specific and user-specific data. An order-entry application could display the text for several inventory items on a user's monitor, and send this text to the engine for recognition. When a user spoke a part number, the engine would more easily recognize it from the few words on the user's screen than from thousands of part numbers specified in a grammar.
To accomplish this, the SR engine maintains a text buffer which is associated with the XML grammar tag <TEXTBUFFER>. When the recognition process arrives at a <TEXTBUFFER> tag, it expects that the application has placed text in this buffer, and that the user's speech represents one word or phrase out of the words and phrases in the buffer. If recognition is successful, the words or phrases selected from the buffer are recognized as if they had been specified in the grammar rule literally.
The SetWordSequenceData method sends this text to the engine's buffer. It is associated with the SetTextSelection method, which describes the range of text the user has selected with the mouse. The SetWordSequenceData method always sends selection data with the text data, because changing the text in the buffer invalidates the previous selection data. The SetTextSelection method sends selection data only.
ISpeechRecoGrammar.SetWordSequenceData(
Text As String,
TextLength As Long,
Info As SpTextSelectionInformation
)
None.
The following Visual Basic form code demonstrates the use of the SetWordSequenceData and SetTextSelection methods. To run this code, create a form with the following controls:
Paste this code into the Declarations section of the form.
Because selecting data in the text box is an important visual part of this example, the HideSelection property of the text box should be set to False.
The Form_Load procedure creates a recognizer, a recognition context, and a grammar object. It fills the text box with the names of several types of animals. It writes an XML grammar file which contains a grammar rule using a text tag. The rule expects a sentence like "Send me a *," where "*" is a type of animal listed in the text box.
Double-click on an animal type in the text box to select it, and then click Command1. The Command1 procedure gets the selected animal type and builds a sentence such as, "Send me a hamster," or "Send me a chinchilla." If no animal type is selected in the text box, the sample uses the first animal listed. The procedure then speaks the sentence into a wave file and sends the file to the recognition context for recognition.
The Recognition event procedure displays some of the recognition data in the list box. It first determines if the recognition result satisfied a grammar rule; if so, it displays the name of the rule. In this case, PETS is the only rule that can be satisfied. The procedure then displays the individual phrase elements of the sentence.
Option Explicit
Const WAVEFILENAME = "C:\SetWordSequenceData.wav"
Const XMLFILENAME = "c:\texttag.xml"
Dim R As SpeechLib.SpInprocRecognizer
Dim G As SpeechLib.ISpeechRecoGrammar
Dim F As SpeechLib.SpFileStream
Dim E As SpeechLib.ISpeechPhraseElement
Dim V As SpeechLib.SpVoice
Dim V2 As SpeechLib.SpVoice 'Plays the wave file back
Dim WithEvents C As SpeechLib.SpInProcRecoContext
Dim TSI As SpeechLib.SpTextSelectionInformation
Private Sub WriteGrammar(strFName)
Open strFName For Output As #1
Print #1, "<GRAMMAR>"
Print #1, " <RULE NAME=""PETS"" TOPLEVEL=""ACTIVE"">"
Print #1, " <O>please</O>"
Print #1, " <P>send me a</P>"
Print #1, " <TEXTBUFFER/>" '<-- Calls for WordSequenceData
Print #1, " </RULE>"
Print #1, "</GRAMMAR>"
Close #1
End Sub
Private Sub SetTextSelection(T As Control)
TSI.ActiveOffset = 0 'Start of text
TSI.ActiveLength = Len(T.Text) 'Length of text
TSI.SelectionOffset = T.SelStart + 1 'Start of selected text
TSI.SelectionOffset = T.SelLength 'Length of selected text
G.SetTextSelection TSI 'Send text-selection data to SR engine
End Sub
Private Sub SetWordSequenceData(T As Control)
Call SetTextSelection(T) 'Set up text-selection data
G.SetWordSequenceData T.Text, Len(T.Text), TSI 'Send the text itself to engine
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 Command1_Click()
Dim W As String
'Get selected word for voice to speak
If Text1.SelLength Then
W = Mid(Text1.Text, Text1.SelStart + 1, Text1.SelLength)
Else
W = "pony"
End If
W = "send me a " & W 'Voice speaks this string
'Send buffer text and selection data to engine
Call SetWordSequenceData(Text1)
List1.Clear
Call SpeakToFile(W, WAVEFILENAME)
F.Open WAVEFILENAME
Set R.AudioInputStream = F
End Sub
Private Sub Form_Load()
' Create Recognizer, RecoContext, Grammar, and Voice
Set R = New SpInprocRecognizer
Set C = R.CreateRecoContext
Set G = C.CreateGrammar()
Set V = New SpVoice
Set V.Voice = V.GetVoices("gender=male").Item(0)
Set V2 = New SpVoice
Set TSI = New SpeechLib.SpTextSelectionInformation
'Write a grammar with a <TEXTTAG> transition, then use it
Call WriteGrammar(XMLFILENAME)
G.CmdLoadFromFile XMLFILENAME, SLODynamic
G.CmdSetRuleIdState 0, SGDSActive 'Set C & C active
G.DictationSetState SGDSActive 'Set Dictation active
Text1.Text = "pony dog cat mouse rabbit hamster chinchilla parrot turtle"
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 X As String
Dim ii As Integer
If Not Result.PhraseInfo.Rule.Name = "" Then
List1.AddItem " Result matches rule """ & Result.PhraseInfo.Rule.Name & """"
End If
ii = 0
For Each E In Result.PhraseInfo.Elements
X = "element " & Format(ii, "00") & ": " & E.DisplayText
List1.AddItem X
ii = ii + 1
Next
End Sub
Private Sub C_EndStream(ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant, _
ByVal StreamReleased As Boolean)
F.Close
DoEvents
F.Open WAVEFILENAME
V2.SpeakStream F
F.Close
End Sub
You are reading help file online using chmlib.com
|