定义自定义识别约束

了解如何在 Windows 应用 SDK 应用中定义和使用语音识别的自定义约束。

重要

语音识别需要 MSIX 包标识。 仅当应用在使用包标识的情况下运行时(已打包,或采用外部位置打包),Windows.Media.SpeechRecognition API 才可用。 未打包的应用不能使用这些 API。

关键 API

概述

语音识别至少需要一个约束来定义可识别的词汇。 如果未指定约束,则使用预定义听写语法。 请参阅 语音识别。

使用属性 SpeechRecognizer.Constraints 向语音识别器添加约束。 有三种类型的约束可用:

  • SpeechRecognitionTopicConstraint — 基于预定义语法(听写或 Web 搜索)的约束。
  • SpeechRecognitionListConstraint — 基于字词或短语列表的约束。
  • SpeechRecognitionGrammarFileConstraint — 在语音识别语法规范 (SRGS) 文件中定义的约束。

每个语音识别器可以有一个约束集合。 以下组合有效:

  • 单一主题限制(听写或网页搜索)
  • 单个主题约束与列表约束的组合
  • 列表约束和/或语法文件约束的组合

重要

在启动识别过程之前调用 SpeechRecognizer.CompileConstraintsAsync 以编译约束。

指定 Web 搜索语法(SpeechRecognitionTopicConstraint)

private async void WeatherSearch_Click(object sender, RoutedEventArgs e)
{
    var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();

    var webSearchGrammar = new Windows.Media.SpeechRecognition.SpeechRecognitionTopicConstraint(
        Windows.Media.SpeechRecognition.SpeechRecognitionScenario.WebSearch, "webSearch");

    speechRecognizer.UIOptions.AudiblePrompt = "Say what you want to search for...";
    speechRecognizer.UIOptions.ExampleText = @"Ex. 'weather for London'";
    speechRecognizer.Constraints.Add(webSearchGrammar);

    await speechRecognizer.CompileConstraintsAsync();

    Windows.Media.SpeechRecognition.SpeechRecognitionResult result =
        await speechRecognizer.RecognizeWithUIAsync();

    resultTextBlock.Text = result.Text;
}

指定程序化列表约束(SpeechRecognitionListConstraint)

列表约束是一种从字词或短语列表创建简单语法的轻量方法。 列表约束非常适用于识别短而不同的短语:

private async void YesOrNo_Click(object sender, RoutedEventArgs e)
{
    var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();

    string[] responses = { "Yes", "No" };

    var listConstraint = new Windows.Media.SpeechRecognition.SpeechRecognitionListConstraint(
        responses, "yesOrNo");

    speechRecognizer.UIOptions.ExampleText = @"Ex. 'yes', 'no'";
    speechRecognizer.Constraints.Add(listConstraint);

    await speechRecognizer.CompileConstraintsAsync();

    Windows.Media.SpeechRecognition.SpeechRecognitionResult result =
        await speechRecognizer.RecognizeWithUIAsync();

    resultTextBlock.Text = result.Text;
}

指定 SRGS 语法约束(SpeechRecognitionGrammarFileConstraint)

SRGS 语法为复杂的语音交互提供了一整套功能,包括指定必须说出字词的顺序、组合多个列表中的单词、链接到其他语法以及使用语义解释:

private async void Colors_Click(object sender, RoutedEventArgs e)
{
    var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();

    var storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(
        new Uri("ms-appx:///Colors.grxml"));
    var grammarFileConstraint =
        new Windows.Media.SpeechRecognition.SpeechRecognitionGrammarFileConstraint(
            storageFile, "colors");

    speechRecognizer.UIOptions.ExampleText = @"Ex. 'blue background', 'green text'";
    speechRecognizer.Constraints.Add(grammarFileConstraint);

    await speechRecognizer.CompileConstraintsAsync();

    Windows.Media.SpeechRecognition.SpeechRecognitionResult result =
        await speechRecognizer.RecognizeWithUIAsync();

    resultTextBlock.Text = result.Text;
}

对于符合 SRGS 规则的基于 XML 的语法文档,请使用 .grxml 文件扩展名。 将文件的 生成操作 设置为 内容,并将 复制到输出目录 设置为 始终复制。

有关 SRGS 元素和属性的详细信息,请参阅 SRGS 语法 XML 参考。

管理约束

加载约束集合后,可以通过将 IsEnabled 属性设置为 true 或 false 来启用或禁用单个约束。 默认值为 true。

一次加载约束,然后根据需要启用或禁用约束比加载、卸载和编译每个识别操作的约束更有效。

限制活动约束的数量可提高语音识别的性能和准确性。 根据应用在当前上下文中可能期望的短语确定启用哪些约束。