语音识别语法有问题

编程入门 行业动态 更新时间:2024-10-11 15:22:04
本文介绍了语音识别语法有问题-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="www.w3/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

更多推荐

语音识别语法有问题

本文发布于:2023-11-25 09:31:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629228.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   语音识别

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!