Click here to Skip to main content
15,847,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When I try to load the grammar I made programatically I got an error: SAPI does not implement phonetic alphabet selection.

My code:

VB
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>
Posted
Updated 2-Jul-12 6:11am
v6
Comments
Sergey Alexandrovich Kryukov 2-Jul-12 3:43am    
In what line? Why do you use all those files and what's in those files?
--SA
Member 9097242 2-Jul-12 4:01am    
"engine.LoadGrammar(gram)" - there is the error:
SAPI does not implement phonetic alphabet selection.
Sergey Alexandrovich Kryukov 2-Jul-12 4:25am    
Thank you, but now it good to know what is in gram. What's in that file?
--SA
Member 9097242 2-Jul-12 4:51am    
The grammar, but it don't works
Dave Kreskowiak 2-Jul-12 9:58am    
I think he meant you should post the contents of the file.

1 solution

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
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 2-Jul-12 16:02pm    
Looks like a catch, my 5.
--SA
ledtech3 2-Jul-12 19:50pm    
I was going to suggest that this morning but ran out of time after stepping the code.I also noted that if the Program is run under Vista or win 7 with UAC enabled you may have trouble with the location you are trying to create the folder and file in with out being elevated.
My 5 Dave.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900