Se produjo una excepción no controlada del tipo 'System.FormatException' en Microsoft.Speech.dll

I have just tried to use custom pronunciation using a PLS lexicon from thr link http://msdn.microsoft.com/en-us/library/hh378403(v=office.14).aspx. My .pls file is as following

Additional information: The lexicon data is invalid or corrupted.

<?xml version="1.0" encoding="UTF-8"?>

<lexicon version="1.0" 
  xmlns="http://www.w3.org/2005/01/pronunciation-lexicon"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.w3.org/2005/01/pronunciation-lexicon 
  http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd"
  alphabet="x-microsoft-ups" xml:lang="en-US">

    <lexeme>
        <grapheme> scale </grapheme>
        <phoneme> S K A L E </phoneme>
    </lexeme>
</lexicon>

and my grammar file is as pronunciation.grxml


<?xml version="1.0" encoding="UTF-8"?>
<grammar 
    version="1.0" 
    xml:lang="en-US" 
    root="colors" 
    sapi:alphabet="x-microsoft-ups" 
    xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions" 
    xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0" >

  <lexicon uri="C:\Users\sony vaio\Documents\Visual Studio 2012\Projects\ConsoleApplication5\ConsoleApplication5\bin\Debug\Blue.pls" type="application/vdn.ms-sapi-lex"/>

  <rule id="colors" scope="public">
      <one-of>  
        <item> scale </item>
      </one-of>
  </rule>

</grammar>

and my c# program is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml;
using Microsoft.Speech;
using Microsoft.Speech.Recognition;
using Microsoft.Speech.Recognition.SrgsGrammar;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {

            using (SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))
            {

                String currDirPath = Environment.CurrentDirectory;

                string xmlGrammar = currDirPath + "\\pronunciation.grxml";
                string cfgGrammar = currDirPath + "\\pronunciation.cfg";
                FileStream fs = new FileStream(cfgGrammar, FileMode.Create);
                XmlReader reader = XmlReader.Create(xmlGrammar);

                //compile the grammar *.grxml to *.cfg file.
                SrgsGrammarCompiler.Compile(reader, (Stream)fs);
                fs.Close();
                Grammar g = new Grammar(cfgGrammar, "colors");
                Console.WriteLine(currDirPath+cfgGrammar);

                recognizer.LoadGrammarAsync(g);

                // Add a handler for the speech recognized event.
                recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
                // Configure the input to the speech recognizer.
                recognizer.SetInputToDefaultAudioDevice();

                // Start asynchronous, continuous speech recognition.
                recognizer.RecognizeAsync(RecognizeMode.Multiple);

                // Keep the console window open.
                while (true)
                {
                    Console.ReadLine();
                }
            }
        }

        // speech recognised event handler

        static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            string lines = e.Result.Text;
            Console.WriteLine("Speak loudly");
            Console.WriteLine("Recognized text: " + e.Result.Text);
            Console.WriteLine("semantic value: " + e.Result.Semantics.Value.ToString());
       }
}

but whenever I try to compile it and use it for recognisig "scale" it always shows that "An unhandled exception of type 'System.FormatException' occurred in Microsoft.Speech.dll Additional information: The lexicon data is invalid or corrupted. "

Although when inline pronunciation is used it works but the pronunciation in external link doesn't seems to work. Is there any workaround? Any help would be appreciated. Please.

preguntado el 05 de mayo de 13 a las 12:05

I was not able to reproduce your problem. Although, I did make two edits to the above code and it worked for me. I replaced the using Microsoft.* with System.* and referenced those assemblies instead. I don't have the Microsoft.Speech assemblies installed. But I am not getting the format exception that you are seeing. Can try this against the System.Speech assemblies and see if you still have issues? Also what formats are you saving your grxml and pls files in when you open them in a txt editor? UTF-8 is standard. -

Hey Thanks @MattJohnson. It worked Man. But does that mean the microsoft.* assembly doesn't support this thing and may be this is why I get the system.formatException or lexicon data invlaid or corrupt error. And yes the format I am saving my grxml and pls is in UTF-8 standard. Still I would like to know why not microsoft.* is working? -

No problem. Moving from Microsoft.Speech to System.Speech shouldn't make any difference. My understanding s that they both support the W3C spec and they don't have specific grammar format requirements. -

1 Respuestas

Moving the code above from Microsoft.Speech.Recogonition to System.Speech.Recognition fixes the issue with the format exception above.

As to why Microsoft.Speech.Recognition is throwing the error, I am not sure. I do know that the official W3C spec does officially support a lexicon tag in the xml file (although Microsoft has very specific rules as to where the lexicon tag is supposed to be located this appears to be correct):

http://www.w3.org/TR/speech-grammar/#S4.10

Lexicon: The grammar format does not address the loading of lexicons or the pronunciation of words referenced by the grammar. The W3C Voice Browser Working Group is considering the development of a standard lexicon format. If and when a format is developed appropriate updates will be made to this grammar specification.

The actual spec for the lexicon format is not completed yet:

http://www.w3.org/TR/lexicon-reqs/#pronun

In summary if Microsofts own example linked aquí does not work this appears to be a bug. Especially if all you did to fix the format exception is to change from Microsoft.Speech to System.Speech.

contestado el 07 de mayo de 13 a las 15:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.