Question Speech Recognition question

manbearpig001

Member
Joined
Jun 15, 2012
Messages
8
Programming Experience
5-10
Hey,
I have been trying to create a set of grammar rules for a speech recognition program with an array, but cannot seem to accomplish this task. Here is the general idea:

VB.NET:
Dim commandlist As New Recognition.SrgsGrammar.SrgsOneOf(websites(0), websites(1), websites(2))
        commandRule.Add(commandlist)
        gram.Rules.Add(commandRule)
        gram.Root = commandRule
        recog.LoadGrammar(New Recognition.Grammar(gram))

I have a string array - websites() - and for each string in this array, I want to add it to the commandlist. Unfortunately, the only way I know of doing this is by writing it one by one, as seen above. I tried doing this:

VB.NET:
Dim commandlist As New Recognition.SrgsGrammar.SrgsOneOf
        dim i as integer = 0
        do until i = websites.items.count
        commandlist.items.add(websites(i))
        i +=1
        loop
        commandRule.Add(commandlist)
        gram.Rules.Add(commandRule)
        gram.Root = commandRule
        recog.LoadGrammar(New Recognition.Grammar(gram))

But this did not work, because I cannot convert strings into srgsoneof... Anyone have any idea how this could be done?
 
So I need to find a way to upload every string from the array websites() to SrgsOneOf
As correctly pointed out by Dunfiddlin in post 2 the SrgsOneOf class has a constructor that takes a String array as parameter, so if that is what you are trying to do the you just have to pass that array to the constructor.
 
As correctly pointed out by Dunfiddlin in post 2 the SrgsOneOf class has a constructor that takes a String array as parameter, so if that is what you are trying to do the you just have to pass that array to the constructor.

Interestingly enough, I tried this a while back and no cases were recognized by Windows Speech Recognition with the array. Here is what I have now, and it does not work:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim gram As New Recognition.SrgsGrammar.SrgsDocument
Dim commandRule As New Recognition.SrgsGrammar.SrgsRule("commands")
Dim sr As StreamReader = System.IO.File.OpenText("C:\Users\andrewbourhis\Documents\Speech Recog Library\websites.txt")
Dim sdata As String
sdata = sr.ReadToEnd
websites = Split(sdata, ControlChars.NewLine)
Dim webst As Integer = 0
Do Until webst = websites.Count
Websitesbox.Items.Add(websites(webst))
webst += 1
Loop
sr.Close()
Dim commandlist As New SrgsOneOf(websites)
commandRule.Add(commandlist)
gram.Rules.Add(commandRule)
gram.Root = commandRule
recog.LoadGrammar(New Recognition.Grammar(gram))
SerialPort1.WriteBufferSize = 4
SerialPort1.Open()
Timer1.Enabled = True
recog.Enabled = True
End Sub

Dim muted As Boolean = False
Private Sub reco_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognitionEventArgs) Handles recog.SpeechRecognized
Dim buff() As Byte = {0, 0, 0, 0}
Dim stacy As New Synthesis.SpeechSynthesizer
Dim webst As Integer = 0
Do Until webst = websites.Count 'sift through each website in the text file and go to it if it has been said
If websites(webst) = e.Result.Text Then
Process.Start("www." & e.Result.Text & ".com")
End If
webst += 1
Loop
Select Case e.Result.Text


Case "mute"
If muted = True Then Exit Sub
muted = True
SendMessageW(Me.Handle, WM_APPCOMMAND, _
Me.Handle, New IntPtr(APPCOMMAND_VOLUME_MUTE))
Case "unmute"
If muted = True Then
SendMessageW(Me.Handle, WM_APPCOMMAND, _
Me.Handle, New IntPtr(APPCOMMAND_VOLUME_MUTE))
muted = False
End If
Case "play my music", "play my music please", "play some music"
System.Diagnostics.Process.Start("www.pandora.com")
Case ("solder on")
Dim i As Integer = 5
Dim i1 As Integer = 5
Do Until i = 9
buff = {1, 0, i, i1}
SerialPort1.Write(buff, 0, 5)
System.Threading.Thread.Sleep(50)
If i1 = 5 Then
i += 1
i1 = 0
Else
i1 = 5
End If
Loop


Case Else
stacy.Speak("Sorry, I don't understand")


End Select
End Sub


<DllImport("user32.dll")> _
Public Shared Function SendMessageW(ByVal hWnd As IntPtr, _
ByVal Msg As Integer, ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As IntPtr
End Function


EDIT: keep in mind some of the commands will not work with the website array as of now, because of the if statement. I only intend on making the websites work right now, so disregard the case select statement.
 
manbearpig001 said:
websites = Split(sdata, ControlChars.NewLine)
This was the first that I noticed, because of this documentation:
help said:
Delimiter
Optional. Any single character used to identify substring limits.
but it was later changed to this:
new help said:
Delimiter

Optional. Any string of characters used to identify substring limit
Much simpler though, use the File.ReadAllLines method to get the array. As it is your speech recog code currently can be expressed like this:
Dim websites = IO.File.ReadAllLines("....websites.txt")
recog.LoadGrammar(New Grammar(New SrgsDocument(New SrgsRule("commands", New SrgsOneOf(websites)))))

I have of course tested this, and the rule is recognised using any of the alternate words from that file.

Use the posting editor and put the code in code box when you post code, as it is now it is too much a strain to bother reading it thoroughly.
 
Back
Top