Freigeben über


Speech grammars example

I figured it's about time for another example.

A lot of scenarios require the speech recognizer to listen for a constrained set of phrases - a "grammar". The app developer can express this with the W3C's SRGS XML format. The WinFX speech API provides a set of classes for producing an SRGS grammar document. (SRGS = "Speech Recognition Grammar Specification", and you can read all about it at the W3C)

So here's an example of a grammar that recognizes simple algebraic statements like "3 x y", "7 x", "4 y".

Function MathGrammar() As SrgsDocument

   'create a one-of to detect the numbers 1 through 10

Dim numberOneOf As New SrgsOneOf

   Dim i As Integer

   For i = 1 To 10

      numberOneOf.Add(New SrgsItem(i.ToString))

   Next

   'wrap this in an optional item

   Dim numberItem As New SrgsItem

   numberItem.Add(numberOneOf)

   numberItem.SetRepeat(0, 1)

   'create a one-of to detect "x" and "y"

   Dim variableOneOf As New SrgsOneOf("x", "y")

   'wrap this in an optional item

   Dim variableItem1 As New SrgsItem

   variableItem1.Add(variableOneOf)

   variableItem1.SetRepeat(0, 1)

   'and again, in a second optional item

   Dim variableItem2 As New SrgsItem

   variableItem2.Add(variableOneOf)

   variableItem2.SetRepeat(0, 1)

   'string the three items together into a rule

   Dim productRule As New SrgsRule("product")

   productRule.Add(numberItem)

   productRule.Add(variableItem1)

   productRule.Add(variableItem2)

   'put the rule into an srgs document

   Dim srgs As New SrgsDocument

   srgs.Rules.Add(productRule)

   srgs.Root = productRule

   MathGrammar = srgs

End Function

One thing to note about this is that I'm adding numerals, but the underlying recognizer will normalize this into words. So, although the grammar contains the character "7", the recognizer listens for "seven".

If you want to paste this into an app to see what happens, here's the rest of the code:

Imports

System.Speech.GrammarBuilding

Imports

System.Speech.Recognition

Public

Class Form1

Dim WithEvents reco As New DesktopRecognizer

'insert MathGrammar function here

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

reco.LoadGrammar(New Grammar(MathGrammar()))

End Sub

Private Sub reco_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognitionEventArgs) Handles reco.SpeechRecognized

Me.Text = e.Result.Text

End Sub

End

Class

(Don't forget to add the reference to Speech to your project.)

Hope this is helpful.

Cheers,

/Rob