语法的语言与语音识别器的语言不匹配
本文关键字:语言 不匹配 语法 语音识别 | 更新日期: 2023-09-27 18:11:51
大家好!这是关于微软服务器语音SDK v11.0(服务器版)。
我在MSDN样本中运行了测试示例。因此,英语短语——红色、蓝色——很容易辨认。但是我也想识别俄语-安装微软语音识别语言-TELE (ru-RU)并运行我的应用程序/
代码: static void DoWork()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("ru-RU");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU");
// Create a new SpeechRecognitionEngine instance.
SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
// Configure the input to the recognizer.
sre.SetInputToWaveFile(@"c:'Test'красный.wav");
// Create a simple grammar that recognizes "red", "green", or "blue".
Choices colors = new Choices();
// colors.Add(new string[] { "red", "green", "blue","красный" });
colors.Add(new string[] { "красный" }); //russian word- "red"
// Create a GrammarBuilder object and append the Choices object.
GrammarBuilder gb = new GrammarBuilder();
gb.Culture = new CultureInfo("ru-RU"); // add Culture Info
gb.Append(colors);
Console.WriteLine(gb.Culture.CultureTypes);
// Create the Grammar instance and load it into the speech recognition engine.
Grammar g = new Grammar(gb);
sre.LoadGrammar(g);
// Register a handler for the SpeechRecognized event.
sre.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
// Start recognition.
sre.Recognize();
}
// Create a simple handler for the SpeechRecognized event.
static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine(String.Format("Speech recognized: {0}",e.Result.Text));
}
错误出现在这行:
sre.LoadGrammar(g);
The language for the grammar does not match the language of the speech recognizer.
那么,如何修复这个错误呢?我尝试设置CultureInfo,但它不工作…谢谢你!
更改GrammarBuilder.Culture
属性将解决此错误。
GrammarBuilder gb = new GrammarBuilder();
添加:
gb.Culture = Thread.CurrentThread.CurrentCulture
使语法文化与识别器匹配
看起来你需要
SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(
new System.Globalization.CultureInfo("ru-RU")))
语法构建器文化必须匹配语音识别器。最简单的方法是将语法文化设置为与语音识别器相同,如下所示:
var sr = new SpeechRecognizer();
var gb = new GrammarBuilder();
// set the culture
gb.Culture = sr.RecognizerInfo.Culture;