我正在尝试创建一个程序,该程序可以识别用户在说什么,并据此来决定要做什么,但是我似乎无法在Internet上用vb找到任何代码。网络和控制台应用程序。甚至有可能以非面向对象的语言来执行此操作,并且控制台应用程序中没有任何事件吗?
我曾尝试将c#控制台代码转换为vb.net控制台代码,但由于从面向对象的编程语言转换为非面向对象的编程语言而无法使用,因此无法显示文字。
我还试图使其仅以某种形式运行,但由于它定位了语法文件,但未能将其识别为一个文件而无法正常工作
这是我尝试过的代码: 1.我的VB表单代码
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。我翻译的C#代码
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
答案 0 :(得分:0)
这是https://docs.microsoft.com/en-us/dotnet/api/system.speech.recognition.speechrecognitionengine处示例的翻译。我在自己的Windows 10 PC上进行了尝试,可以确认它是否有效。我使用了Visual Studio Community 2019,并使用了.NET FW上VB控制台应用程序的默认设置。希望这会有所帮助!
另一方面,VB有两种风格:(1)旧的VBA / VB6; (2)和最新的VB.NET。 VB.NET是一种完全现代的OOP语言。 VB.NET和C#是被称为Roslyn的通用编译平台“紧密结合”的兄弟姐妹。 VB6已停产,VBA仍作为Microsoft Office的一部分存在。
关于VBA / VB6和OOP,请查看Is VBA an OOP language, and does it support polymorphism?,这是一本有趣而又周到的读物。
' NOTE: Must target .NET Framework, 3.0 or later (not .NET Core!)
Imports System.Console
' Make reference to System.Speech (System.Speech.dll)
' https://docs.microsoft.com/en-us/dotnet/api/system.speech.recognition.speechrecognitionengine
Imports System.Speech.Recognition
Module Program
Sub Main()
' Create an in-process speech recognizer for the en-US locale.
Using recognizer As New SpeechRecognitionEngine(
New Globalization.CultureInfo("en-US"))
' Create and load a dictation grammar.
recognizer.LoadGrammar(New DictationGrammar())
' Add a handler for the speech recognized event.
AddHandler recognizer.SpeechRecognized,
AddressOf recognizer_SpeechRecognized
' Configure input to the speech recognizer.
recognizer.SetInputToDefaultAudioDevice()
' Start asynchronous, continuous speech recognition.
recognizer.RecognizeAsync(RecognizeMode.Multiple)
' Keep the console window open.
Do
ReadLine()
Loop
End Using
End Sub
' Handle the SpeechRecognized event.
Sub recognizer_SpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs)
WriteLine("Recognized text: " + e.Result.Text)
End Sub
End Module