【发布时间】:2015-04-07 00:10:10
【问题描述】:
我刚刚尝试使用来自链接 http://msdn.microsoft.com/en-us/library/hh378403(v=office.14).aspx 的 PLS 词典使用自定义发音。 我的 .pls 文件如下
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());
}
}
但每当我尝试编译它并将其用于识别“比例”时,它总是显示“Microsoft.Speech.dll 中发生了“System.FormatException”类型的未处理异常 附加信息:词典数据无效或损坏。 "
虽然使用内联发音时它可以工作,但外部链接中的发音似乎不起作用。 有什么解决方法吗?任何帮助,将不胜感激。请。
【问题讨论】:
-
我无法重现您的问题。虽然,我确实对上面的代码进行了两次编辑,它对我有用。我用 System.* 替换了 using Microsoft.* 并引用了这些程序集。我没有安装 Microsoft.Speech 程序集。但我没有得到您所看到的格式异常。可以针对 System.Speech 程序集尝试此操作,看看是否还有问题?另外,当您在 txt 编辑器中打开 grxml 和 pls 文件时,它们以什么格式保存? UTF-8 是标准的。
-
嘿,谢谢@MattJohnson。它工作的人。但这是否意味着 microsoft.* 程序集不支持这个东西,可能这就是为什么我得到 system.formatException 或 lexicon data invlaid 或损坏错误的原因。是的,我保存我的 grxml 和 pls 的格式是 UTF-8 标准。我仍然想知道为什么 microsoft.* 不起作用?
-
没问题。从 Microsoft.Speech 迁移到 System.Speech 应该没有任何区别。我的理解是它们都支持 W3C 规范,并且没有特定的语法格式要求。
标签: c# grammar speech formatexception