I'm trying to create a program that can recognise what a user is saying, and make a decision on what to do based on that, but i can't seem to find any code on the internet for it in vb.net and a console application. Is it even possible to do it in a non-object orientated language and there not being any events in a console application?
I've tried converting c# console code to vb.net console code and it doesn't word due to the issues of converting from an object orientated programming langue to a non object-orientated one.
I've also tried to just get it to run in a form, and that doesn't work due to it locating the grammar file, but failing to recognise it as one
This is the code i've tried: 1. My VB form code
Imports System.Speech
Imports System.Speech.Recognition
Public Class Form1
Dim WithEvents recog As New Recognition.SpeechRecognitionEngine
Private Sub setcolo(ByVal colour As System.Drawing.Color)
Dim synth As New Synthesis.SpeechSynthesizer
Me.BackColor = colour
End Sub
2. My translated C# code
Imports System, System.Speech.Recognition, System.Speech.Synthesis, System.Globalization
Module Module1
Dim ss As New System.Speech.Synthesis.SpeechSynthesizer
Dim sre As Speech.Recognition.SpeechRecognitionEngine
Dim done As Boolean = False
Dim speechOn As Boolean = True
Sub Main(args As String)
Try
ss.SetOutputToDefaultAudioDevice()
Console.WriteLine("\n(Speaking: I am awake)")
ss.Speak("I am awake")
Dim ci = New CultureInfo("en-us")
sre = New Speech.Recognition.SpeechRecognitionEngine(ci)
sre.SetInputToDefaultAudioDevice()
'sre.SpeechRecognized = sre.SpeechRecognized + sre_SpeechRecognized()
Dim ch_StartStopCommands As Speech.Recognition.Choices = New Speech.Recognition.Choices
ch_StartStopCommands.Add("speech on")
ch_StartStopCommands.Add("speech off")
ch_StartStopCommands.Add("klatu barada nikto")
Dim gb_StartStop As Speech.Recognition.GrammarBuilder = New Speech.Recognition.GrammarBuilder()
gb_StartStop.Append(ch_StartStopCommands)
Dim g_StartStop As Speech.Recognition.Grammar = New Speech.Recognition.Grammar(gb_StartStop)
Dim ch_Numbers As Speech.Recognition.Choices = New Speech.Recognition.Choices()
ch_Numbers.Add("1")
ch_Numbers.Add("2")
ch_Numbers.Add("3")
ch_Numbers.Add("4")
Dim gb_WhatIsXplusY As Speech.Recognition.GrammarBuilder = New Speech.Recognition.GrammarBuilder()
gb_WhatIsXplusY.Append("What is")
gb_WhatIsXplusY.Append(ch_Numbers)
gb_WhatIsXplusY.Append("plus")
gb_WhatIsXplusY.Append(ch_Numbers)
Dim g_WhatIsXplusY As Speech.Recognition.Grammar = New Speech.Recognition.Grammar(gb_WhatIsXplusY)
sre.LoadGrammarAsync(g_StartStop)
sre.LoadGrammarAsync(g_WhatIsXplusY)
sre.RecognizeAsync(Speech.Recognition.RecognizeMode.Multiple)
While (done = False)
End While
Console.WriteLine("\nHit <enter> to close shell\n")
Console.ReadLine()
Catch ex As Exception
Console.WriteLine(ex.Message)
Console.ReadLine()
End Try
End Sub
Function sre_SpeechRecognized(sender As Object, e As Speech.Recognition.SpeechRecognizedEventArgs)
Dim txt As String = e.Result.Text
Dim confidence As Double = e.Result.Confidence
Console.WriteLine("\nRecognized: " & txt)
If (confidence < 0.6) Then Return ""
If (txt.IndexOf("speech on") >= 0) Then
Console.WriteLine("Speech is now ON")
speechOn = True
End If
If (txt.IndexOf("speech off") >= 0) Then
Console.WriteLine("Speech is now OFF")
speechOn = False
End If
If (speechOn = False) Then Return ""
If (txt.IndexOf("klatu") >= 0 And txt.IndexOf("barada") >= 0) Then
'((SpeechRecognitionEngine)sender).RecognizeAsyncCancel()
done = True
Console.WriteLine("(Speaking: Farewell)")
ss.Speak("Farewell")
End If
If (txt.IndexOf("What") >= 0 And txt.IndexOf("plus") >= 0) Then
Dim words() As String = txt.Split(" ")
Dim num1 As Integer = Int(words(2))
Dim num2 As Integer = Int(words(4))
Dim sum As Integer = num1 + num2
Console.WriteLine("(Speaking: " & words(2) & " plus " & words(4) & " equals " & sum & ")")
ss.SpeakAsync(words(2) & " plus " & words(4) & " equals " & sum)
End If
Return ""
End Function
End Module
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
recog.SetInputToDefaultAudioDevice()
Dim gram As New Recognition.SrgsGrammar.SrgsDocument("D:\Voiceregogform\Voiceregogform\bin\Debug\Voiceregogform.xml")
Dim colourr As Recognition.SrgsGrammar.SrgsRule
Dim colourlist As New Recognition.SrgsGrammar.SrgsOneOf("red", "yellow", "indigo", "aqua", "green")
colourr.Add(colourlist)
gram.Rules.Add(colourr)
gram.Root = colourr
recog.LoadGrammar(New Recognition.Grammar(gram))
recog.RecognizeAsync()
End Sub
Private Sub recog_RecognizeCompleted(sender As Object, e As RecognizeCompletedEventArgs) Handles recog.RecognizeCompleted
recog.RecognizeAsync()
End Sub
Private Sub recog_SpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs) Handles recog.SpeechRecognized
Select Case e.Result.Text
Case "red"
setcolo(Color.Red)
Case "yellow"
setcolo(Color.Yellow)
Case "aqua"
setcolo(Color.Aqua)
Case "green"
setcolo(Color.Green)
Case "indigo"
setcolo(Color.Indigo)
Case "blue"
setcolo(Color.Blue)
End Select
End Sub
End Class