【问题标题】:How can i fix language for the grammar does not match the language of the speech recognizer error in vb如何修复语法的语言与 vb 中语音识别器错误的语言不匹配
【发布时间】:2014-10-10 03:27:58
【问题描述】:

你好,Visual Basic 我正在尝试创建一个语音识别系统,可以帮助我在计算机上的生活。我打电话给我的助手贾维斯。对于这个项目,我导入了 system.speech.dll。现在我收到这个错误:

创建表单时出错。有关详细信息,请参阅 Exception.InnerException。错误是:语法的语言与语音识别器的语言不匹配。

这是我的代码

Imports System.Speech.Recognition
Imports System.Speech.Recognition.SrgsGrammar
Imports System.Runtime.InteropServices 'For Monitor Command

Public Class Form1
    'This object represents the Speech recognition engine
    Private recognizer As SpeechRecognizer
    Dim QuestionEvent As String

    Public Sub New()

        InitializeComponent() ' This call is required by the Windows Form Designer.

        recognizer = New SpeechRecognizer() ' Add any initialization after the InitializeComponent() call.

        AddHandler recognizer.SpeechDetected, AddressOf recognizer_SpeechDetected 'this event is raised when the user begins to speak

        AddHandler recognizer.SpeechRecognitionRejected, AddressOf recognizer_SpeechRecognitionRejected 'this is raised when spoken words are not recognized as compliant to our grammar rules

        AddHandler recognizer.SpeechRecognized, AddressOf recognizer_SpeechRecognized 'this is raised when the application correctly recognizes spoken words


        'The rule is that each choice in the first Append method can be combined with each word specified
        'in the second method.
        Dim grammar As New GrammarBuilder()
        grammar.Append(New Choices(System.IO.File.ReadAllLines("Commands.txt")))

        'A grammar must be loaded into the engine. This is possible by loading an object or an xml file
        recognizer.LoadGrammar(New Grammar(grammar))


    End Sub

    Private Sub recognizer_SpeechRecognized(ByVal sender As Object, ByVal e As SpeechRecognizedEventArgs)
        Dim Jarvis = CreateObject("sapi.spvoice")
        Select e.Result.Text.ToUpper

            'GREETINGS
            Case Is = "HELLO JARVIS"
                Jarvis.Speak("Hello sir")
            Case Is = "GOODBYE JARVIS"
                Jarvis.Speak("Until next time")
                Me.Close()

                'DATE / TIME / WEATHER
            Case Is = "WHAT TIME IS IT"
                Jarvis.Speak(Format(Now, "Short Time"))
            Case Is = "HOWS THE WEATHER"
                System.Diagnostics.Process.Start("https://www.google.com/webhp?sourceid=chrome-instant&ion=1&ie=UTF-8#output=search&sclient=psy-ab&q=weather&oq=&gs_l=&pbx=1&bav=on.2,or.r_cp.r_qf.&bvm=bv.47008514,d.eWU&fp=6c7f8a5fed4db490&biw=1366&bih=643&ion=1&pf=p&pdl=300")
                Jarvis.Speak("Searching for local weather")


                'WEBSITES
            Case Is = "RUN FACEBOOK"
                System.Diagnostics.Process.Start("http://www.facebook.com")
            Case Is = "RUN GOOGLE"
                System.Diagnostics.Process.Start("http://www.google.com")
            Case Is = "RUN YAHOO"
                System.Diagnostics.Process.Start("http://www.yahoo.com")
            Case Is = "RUN PANDORA"
                System.Diagnostics.Process.Start("http://www.pandora.com")
            Case Is = "RUN THE PIRATE BAY"
                System.Diagnostics.Process.Start("http://www.thepiratebay.se")
            Case Is = "RUN YOUTUBE"
                System.Diagnostics.Process.Start("http://www.youtube.com")

                'MISCELLANEOUS
            Case Is = "SHOW COMMANDS"
                lbCommands.Visible = True
                Jarvis.speak("Here we are")
            Case Is = "HIDE COMMANDS"
                lbCommands.Visible = False
                Jarvis.speak("Very well")
            Case Is = "OPEN DISK DRIVE"
                Dim oWMP = CreateObject("WMPlayer.OCX.7")
                Dim CDROM = oWMP.cdromCollection
                If CDROM.Count = 2 Then
                    CDROM.Item(1).Eject()
                    Jarvis.speak("Its now open")
                End If
            Case Is = "MONITOR OFF"
                SendMessage(Me.Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, 2)
                Jarvis.Speak("I'll just close my eyes for a minute")
        End Select
    End Sub
    'LABEL DETECT / REJECT
    Private Sub recognizer_SpeechDetected(ByVal sender As Object, ByVal e As SpeechDetectedEventArgs)

        Label1.ForeColor = Color.Green : Label1.BackColor = Color.Transparent : Label1.Text = "Speech engine has detected that you spoke something"
    End Sub
    Private Sub recognizer_SpeechRecognitionRejected(ByVal sender As Object, ByVal e As SpeechRecognitionRejectedEventArgs)

        Label1.ForeColor = Color.Red : Label1.BackColor = Color.Transparent : Label1.Text = ("Sorry but the " & e.Result.Text & " phrase could not be recognized")

    End Sub

    'LOADS THE COMMANDS INTO AN ARRAY AND LISTBOX
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Commands() As String = System.IO.File.ReadAllLines("Commands.txt")
        For i As Integer = 0 To Commands.Count - 1
            lbCommands.Items.Add(Commands(i))
        Next
    End Sub

    'HIDES COMMANDS LIST BOX
    Private Sub lbCommands_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbCommands.SelectedIndexChanged
        lbCommands.Visible = False
    End Sub

    'FOR MONITOR COMMAND
    Public WM_SYSCOMMAND As Integer = &H112
    Public SC_MONITORPOWER As Integer = &HF170
    <DllImport("user32.dll")> _
    Private Shared Function SendMessage(ByVal hWnd As Integer, ByVal hMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    End Function

End Class

由于这行代码recognizer.LoadGrammar(New Grammar(grammar)),我收到了该错误,但是我不确定如何修复该行

Here is an image of the error

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: vb.net visual-studio-2010 visual-studio speech-recognition


    【解决方案1】:

    我已经解决了。

    您可以在声明识别器之前使用此代码修复此问题

    先导入这个

    Imports System.Threading
    Imports System.Globalization
    

    第二次添加这个

       Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB")
    

    '或您当前的语言和国家'

       Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-GB")
    

    '或您当前的语言和国家'

    【讨论】:

      【解决方案2】:

      就像我坐在不同的论坛上一样。我在朋友的电脑上遇到了同样的问题。所以我做了这个(它只是代码的一部分,因为所有的代码都很长):

      ...
      RecognizerInfo recognizerInfo = null;
      
      foreach (RecognizerInfo ri in SpeechRecognitionEngine.InstalledRecognizers())
      {
         if ((ri.Culture.TwoLetterISOLanguageName.Equals("en")) && (recognizerInfo == null))
         {
            recognizerInfo = ri;
            break;
         }
      
      }
      
      SpeechRecognitionEngine SpeachRecognition = new SpeechRecognitionEngine(recognizerInfo);
      
      GrammarBuilder gb = new GrammarBuilder(startLiserninFraze);
      gb.Culture = recognizerInfo.Culture;
      grammar = new Grammar(gb);
      SpeachRecognition.RequestRecognizerUpdate();
      SpeachRecognition.LoadGrammar(grammar);
      SpeachRecognition.SpeechRecognized += SpeachRecognition_SpeechRecognized;
      SpeachRecognition.SetInputToDefaultAudioDevice();
      SpeachRecognition.RecognizeAsync(RecognizeMode.Multiple);
      ...
      

      所以这应该有效。我的朋友 PC 支持 2 个“en”或“eng”实例。不知道为什么。所以代码选择第一个。我在互联网上发现了一些代码和平,其中一些是由我制作的。

       SpeachRecognition.SpeechRecognized += SpeachRecognition_SpeechRecognized;
      

      当一切都被识别时,就会发生事件。只需输入:

      SpeachRecognition.SpeechRecognized +=

      然后按 TAB 键几次。然后在代码的底部它会生成类似的东西:

      void SpeachRecognition_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
         {
             //then will be some line that you need to replace with your code
         }
      

      我希望这会有所帮助。 :)

      您的选择更短。但是在输入它之后,我在运行 ower VS 2013 时收到了关于未安装 sintesyzer 或语言不可用的不同错误。删除代码发布版本后,我从 VS 运行了相同的错误,但从资源管理器中它没问题。有人知道这是为什么吗?

      【讨论】:

        【解决方案3】:

        注意:通过SpeechRecognitionEngine类型的对象的构造,包括CultureInfo的参数以及Grammar对象的culture属性应该是一样的。 例如 - 我为两个对象使用“en-US”获得了相同的描述异常。 但是,当我在德语环境中对这两个对象都使用“en-GB”时,程序按预期工作! 因此,首先检测允许的文化可能很有用。 在我看来,你可以避免对 >> Thread.CurrentThread.CurrentCulture SpeechRecognitionEngine 和 Grammar 对象的正确设置允许/安装的文化。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-07
          • 1970-01-01
          • 2019-06-26
          相关资源
          最近更新 更多