语音识别语法的问题 - VB.NET

时间:2012-07-02 07:11:59

标签: vb.net speech-recognition grammar

当我尝试加载我以编程方式编写的语法时,我收到了一个错误: SAPI未实施语音字母选择。

我的代码:

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

1 个答案:

答案 0 :(得分:1)

您可能正在使用Windows Pre-Vista(NT5)...... 该错误是由于SAPI版本不是5.3或更高... 在Windows 7中测试代码,一切都应运行正常......

你可以在图书馆看到第299到325行的问题,我会在下面的链接中找到

Source code for the .NET framework in C#, RecognizerBase.cs source code in C# .NET

保持良好......