首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MS语音平台11识别器支持ARPA编译语法吗?

MS语音平台11识别器支持ARPA编译语法吗?
EN

Stack Overflow用户
提问于 2018-09-18 14:42:49
回答 2查看 217关注 0票数 10

如何与MS语音一起使用ARPA文件?Microsoft Speech Platform 11 Recognizer的文档意味着可以从ARPA文件编译语法。

我可以使用以下命令行编译一个ARPA文件--例如,小小的由Microsoft提供示例:

代码语言:javascript
复制
CompileGrammar.exe -In stock.arpa -InFormat ARPA

我能够在以下测试中使用生成的CFG文件:

代码语言:javascript
复制
using Microsoft.Speech.Recognition;

// ...

using (var engine = new SpeechRecognitionEngine(new CultureInfo("en-US")))
{
    engine.LoadGrammar(new Grammar("stock.cfg"));
    var result = engine.EmulateRecognize("will stock go up");
    Assert.That(result, Is.Not.Null);
}

这个测试通过了,但是请注意它使用EmulateRecognize()。当我切换到使用一个实际的音频文件时,如下所示:

代码语言:javascript
复制
using (var engine = new SpeechRecognitionEngine(new CultureInfo("en-US"))) 
{
    engine.LoadGrammar(new Grammar("stock.cfg"));
    engine.SetInputToWaveFile("go-up.wav");
    var result = engine.Recognize();
}

结果始终为null,测试失败。

微软国家相当清楚的支持,但即使是非常简单的例子似乎不起作用。我做错了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-04 16:14:45

这个问题有两个不同的答案,取决于您使用的的哪个版本。(见:System.Speech.Recognition和Microsoft.Speech.Recognition有什么区别?)

System.Speech (桌面版)

在本例中,请参见赛亚1223的答案。那里的示例代码工作得很好。

Microsoft.Speech (服务器版本)

也许因为服务器版本不包括“听写引擎”,Microsoft.Speech库显然永远不会匹配ARPA源CFG。然而,它仍将假设通过SpeechRecognitionRejected事件所说的话。以下是s 1223的桌面代码的必要更改:

  1. 当然,将使用语句从System.Speech更改为Microsoft.Speech。
  2. SpeechRecognitionRejected事件添加一个事件处理程序。
  3. 在事件处理程序中,检查e.Result.Text属性以获得最终假设。

下面的片段应该有助于说明:

代码语言:javascript
复制
static string transcription;

static void Main(string[] args)  
{
  using (var recognizer = new SpeechRecognitionEngine(new CultureInfo("en-us")))
  {
    engine.SpeechRecognitionRejected += SpeechRecognitionRejectedHandler;
    // ...
  }
}

void SpeechRecognitionRejectedHandler(object sender, SpeechRecognitionRejectedEventArgs e)
{
  if (e.Result != null && !string.IsNullOrEmpty(e.Result.Text))
    transcription = e.Result.Text;
}

此处理程序在识别结束时被调用一次。例如,这里是s 1223的代码的输出,但是使用了所有可用的事件处理程序和一组额外的日志记录(强调我的):

开始异步识别..。 在SpeechDetectedHandler中: - AudioPosition = 00:00:01.2300000 在SpeechHypothesizedHandler中: -语法名称=股票;结果文本= Go 在SpeechHypothesizedHandler中: -语法名称=股票;结果文本=意志 在SpeechHypothesizedHandler中: -语法名称=股票;结果文本=遗嘱股票 在SpeechHypothesizedHandler中: -语法名称=股票;结果文本=遗嘱股票 在SpeechHypothesizedHandler中: -语法名称=股票;结果文本=遗嘱股票上涨 SpeechRecognitionRejectedHandler:中的 -语法名称=股票;结果文本=威尔股票上涨 在RecognizeCompletedHandler中。 - AudioPosition = 00:00:03.2000000;InputStreamEnded = True -没有结果。 好了。

票数 1
EN

Stack Overflow用户

发布于 2018-09-27 15:44:03

关于你的问题:

MS语音平台11识别器支持ARPA编译语法吗?

答案是肯定的。

在我这边工作过的代码,只需更改以下三个属性:Culture/Grammar/WaveFile.我不知道你的完整代码,但基于我的测试和演示代码,我想根本原因是我们需要在我们这边处理SpeechRecognized,而您可能还没有这样做。

代码语言:javascript
复制
static bool completed;

        static void Main(string[] args)  
        {
            // Initialize an in-process speech recognition engine.  
            using (SpeechRecognitionEngine recognizer =
               new SpeechRecognitionEngine(new CultureInfo("en-us")))
            {

                // Create and load a grammar.   
                Grammar dictation = new Grammar("stock.cfg");
                dictation.Name = "Dictation Grammar";

                recognizer.LoadGrammar(dictation);

                // Configure the input to the recognizer.  
                recognizer.SetInputToWaveFile("test.wav");

                // Attach event handlers for the results of recognition.  
                recognizer.SpeechRecognized +=
                  new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
                recognizer.RecognizeCompleted +=
                  new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted);

                // Perform recognition on the entire file.  
                Console.WriteLine("Starting asynchronous recognition...");
                completed = false;
                recognizer.RecognizeAsync();

                // Keep the console window open.  
                while (!completed)
                {
                    Console.ReadLine();
                }
                Console.WriteLine("Done.");
            }

            Console.WriteLine();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }

        // Handle the SpeechRecognized event.  
        static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result != null && e.Result.Text != null)
            {
                Console.WriteLine("  Recognized text =  {0}", e.Result.Text);
            }
            else
            {
                Console.WriteLine("  Recognized text not available.");
            }
        }

        // Handle the RecognizeCompleted event.  
        static void recognizer_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Console.WriteLine("  Error encountered, {0}: {1}",
                e.Error.GetType().Name, e.Error.Message);
            }
            if (e.Cancelled)
            {
                Console.WriteLine("  Operation cancelled.");
            }
            if (e.InputStreamEnded)
            {
                Console.WriteLine("  End of stream encountered.");
            }
            completed = true;
        }

而wav的内容只是“将股票上涨”(持续时间约为2秒)。

有关更多信息:_

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52389148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档