2013-05-05 111 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。

相關問題