2013-05-05 109 views
1

我刚刚尝试使用来自thr链接http://msdn.microsoft.com/en-us/library/hh378403(v=office.14).aspx的PLS词典使用自定义发音。 我的.pls文件如下Microsoft.Speech.dll中发生未处理的异常类型'System.FormatException'

Additional information:词典数据无效或损坏。

<?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> 

和我的C#程序是

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()); 
     } 
} 

但每当我尝试编译它,并用它来recognisig“规模”,它总是显示“类型的未处理的异常“System.FormatException '发生在Microsoft.Speech.dll 附加信息:词典数据无效或损坏。“

虽然使用内联语音,但它的工作原理是在外部发音链接似乎不起作用。 有什么解决方法吗?任何帮助,将不胜感激。请。

+1

我无法重现您的问题。虽然,我确实对上面的代码进行了两次编辑,但它对我很有帮助。我用System。*替换了使用的Microsoft *,并引用了这些程序集。我没有安装Microsoft.Speech程序集。但是我没有看到你所看到的格式异常。可以试试这对System.Speech组件,看看你是否仍然有问题?当你在txt编辑器中打开它们时,你还要保存你的grxml和pls文件的格式是什么? UTF-8是标准的。 – 2013-05-06 11:29:31

+0

嘿谢谢@MattJohnson。它有效的人。但是这是否意味着microsoft。*程序集不支持这个事情,可能这就是为什么我得到system.formatException或词典数据invlaid或损坏错误。是的,我正在保存我的grxml格式,并且使用UTF-8标准。不过,我想知道为什么不微软。*工作? – devsda 2013-05-07 08:58:15

+0

没问题。从Microsoft.Speech转到System.Speech应该没有什么区别。我的理解是,他们都支持W3C规范,并且他们没有特定的语法格式要求。 – 2013-05-07 14:03:12

回答

1

将上面的代码从Microsoft.Speech.Recogonition移动到System.Speech.Recognition可以解决上述格式异常的问题。

至于为什么Microsoft.Speech.Recognition正在抛出错误,我不确定。我知道的官方W3C规范并正式支持在XML文件中的词汇标签(虽然微软有非常具体的规则到词库标签应该位于这似乎是正确的):

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

词汇表:语法格式不能解决词典的加载或语法引用的单词的发音问题。 W3C语音浏览器工作组正在考虑开发标准词典格式。如果并且当一种格式被开发时,将对该语法规范进行适当的更新。

的词库格式的实际规格尚未完成:

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

总而言之,如果微软自己的例子链接here不起作用,这似乎是一个错误。特别是如果你所做的只是修改格式异常,就是从Microsoft.Speech改为System.Speech。

相关问题