语音识别语法有问题-VB.NET [英] Trouble with the Speech Recognition Grammar - VB.NET

查看:142
本文介绍了语音识别语法有问题-VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试加载语法时,我以编程方式编写了一个错误消息:SAPI无法实现语音字母选择.

我的代码:

When I try to load the grammar I made programatically I got an error: SAPI does not implement phonetic alphabet selection.

My code:

Imports System.Speech
Imports System.Xml
Imports System.Speech.Recognition
Imports System.IO

Public Class Form1
    Dim engine As New Speech.Recognition.SpeechRecognitionEngine
    Dim gram As new Speech.Recognition.Grammar("C:\Test\Speech\mygrammar.xml")
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim choices = New Recognition.Choices()

        choices.Add("Red")
        choices.Add("Blue")
        choices.Add("Green")
        choices.Add("Yellow")
        Dim gb = New GrammarBuilder()

        gb.Append(choices)

        Dim doc = New Speech.Recognition.SrgsGrammar.SrgsDocument(gb)
        IO.Directory.CreateDirectory("C:\Test")
        IO.Directory.CreateDirectory("C:\Test\Speech")
        Dim xWriter = System.Xml.XmlWriter.Create("C:\Test\Speech\mygrammar.xml")

        doc.WriteSrgs(xWriter)
        xWriter.Close()

        Dim compiledFile = New FileStream("C:\Test\Speech\mycompiledgrammar.cfg", FileMode.OpenOrCreate)

        Speech.Recognition.SrgsGrammar.SrgsGrammarCompiler.Compile("C:\Test\Speech\mygrammar.xml", compiledFile)
        compiledFile.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        engine.LoadGrammar(gram) 'There's the error
        engine.SetInputToWaveFile("D:\record.wav")
    End Sub
End Class



语法:



Grammar:

<?xml version="1.0" encoding="utf-8">
<grammar xml:lang="en-US" root="root" tag-format="properties-ms/1.0" version="1.0" xmlns="http://www.w3.org/2001/06/grammar">
    <rule id="root" scope="private">
        <one-of>
            <item>Yellow</item>
        </one-of>
    </rule>
</grammar>

推荐答案

您的gram声明在触发Form.Load事件之前运行,因此在您的应用程序首次运行时, grammar.xml文件不存在并且为空.

编写文件后,您的代码应创建Grammar对象:
Your gram declaration runs BEFORE the Form.Load event is fired, so on the first run of your app, the grammar.xml file doesn''t exist and is empty.

Your code should be creating the Grammar object after it has written the file:
Public Class Form1
    Dim engine As New SpeechRecognitionEngine

    Dim gram As Grammar

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Dim choices = New Recognition.Choices()
 
        choices.Add("Red")
        choices.Add("Blue")
        choices.Add("Green")
        choices.Add("Yellow")
        Dim gb = New GrammarBuilder()
 
        gb.Append(choices)
 
        Dim doc = New Speech.Recognition.SrgsGrammar.SrgsDocument(gb)
        IO.Directory.CreateDirectory("C:\Test")
        IO.Directory.CreateDirectory("C:\Test\Speech")
        Dim xWriter = System.Xml.XmlWriter.Create("C:\Test\Speech\mygrammar.xml")
 
        doc.WriteSrgs(xWriter)
        xWriter.Close()
 
        Dim compiledFile = New FileStream("C:\Test\Speech\mycompiledgrammar.cfg", FileMode.OpenOrCreate)
 
        Speech.Recognition.SrgsGrammar.SrgsGrammarCompiler.Compile("C:\Test\Speech\mygrammar.xml", compiledFile)
        compiledFile.Close()

        gram = New Grammar("C:\Test\Speech\mygrammar.xml")    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If gram IsNot Nothing Then
            engine.LoadGrammar(gram)
            engine.SetInputToWaveFile("D:\record.wav")
        Else
            '' The grammar wasn''t loaded...
            '' Do something about the condition or notify the user.
        End If
    End Sub
End Class


这篇关于语音识别语法有问题-VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆