了解如何定义和使用自定义约束进行语音识别。
重要 API: SpeechRecognitionTopicConstraint、 SpeechRecognitionListConstraint、 SpeechRecognitionGrammarFileConstraint
语音识别至少需要一个约束来定义可识别的词汇。 如果未指定约束,则使用通用 Windows 应用的预定义听写语法。 请参阅 语音识别。
添加约束
使用 SpeechRecognizer.Constraints 属性向语音识别器添加约束。
在这里,我们将介绍应用中使用的三种语音识别约束。 (对于 Cortana 语音命令约束,请参阅 通过 Cortana 使用语音命令激活前台应用。
- SpeechRecognitionTopicConstraint - 基于预定义语法(听写或 Web 搜索)的约束。
- SpeechRecognitionListConstraint - 基于字词或短语列表的约束。
- SpeechRecognitionGrammarFileConstraint - 在语音识别语法规范 (SRGS) 文件中定义的约束。
每个语音识别器可以有一个约束集合。 只有这些约束组合有效:
- 单主题约束(听写或 Web 搜索)
- 对于 Windows 10 Fall Creators Update(10.0.16299.15)和更新版本,单个主题约束可以与列表约束结合使用
- 列表约束和/或语法文件约束的组合。
重要
调用 SpeechRecognizer.CompileConstraintsAsync 方法,在开始识别过程之前编译约束。
指定 Web 搜索语法(SpeechRecognitionTopicConstraint)
主题约束(听写或 Web 搜索语法)必须添加到语音识别器的约束集合中。
注释
可以将 SpeechRecognitionListConstraint 与 SpeechRecognitionTopicConstraint 结合使用,通过提供一组在听写过程中可能使用的特定于域的关键字来提高听写准确性。
在这里,我们将 Web 搜索语法添加到约束集合。
private async void WeatherSearch_Click(object sender, RoutedEventArgs e)
{
// Create an instance of SpeechRecognizer.
var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();
// Listen for audio input issues.
speechRecognizer.RecognitionQualityDegrading += speechRecognizer_RecognitionQualityDegrading;
// Add a web search grammar to the recognizer.
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);
// Compile the constraint.
await speechRecognizer.CompileConstraintsAsync();
// Start recognition.
Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = await speechRecognizer.RecognizeWithUIAsync();
//await speechRecognizer.RecognizeWithUIAsync();
// Do something with the recognition result.
var messageDialog = new Windows.UI.Popups.MessageDialog(speechRecognitionResult.Text, "Text spoken");
await messageDialog.ShowAsync();
}
指定程序化列表约束(SpeechRecognitionListConstraint)
必须将列表约束添加到语音识别器的约束集合中。
请记住以下几点:
- 可以将多个列表约束添加到约束集合。
- 可以使用任何实现 IIterable<String> 的集合来处理字符串值。
在这里,我们以编程方式将单词数组指定为列表约束,并将其添加到语音识别器的约束集合中。
private async void YesOrNo_Click(object sender, RoutedEventArgs e)
{
// Create an instance of SpeechRecognizer.
var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();
// You could create this array dynamically.
string[] responses = { "Yes", "No" };
// Add a list constraint to the recognizer.
var listConstraint = new Windows.Media.SpeechRecognition.SpeechRecognitionListConstraint(responses, "yesOrNo");
speechRecognizer.UIOptions.ExampleText = @"Ex. 'yes', 'no'";
speechRecognizer.Constraints.Add(listConstraint);
// Compile the constraint.
await speechRecognizer.CompileConstraintsAsync();
// Start recognition.
Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = await speechRecognizer.RecognizeWithUIAsync();
// Do something with the recognition result.
var messageDialog = new Windows.UI.Popups.MessageDialog(speechRecognitionResult.Text, "Text spoken");
await messageDialog.ShowAsync();
}
指定 SRGS 语法约束(SpeechRecognitionGrammarFileConstraint)
必须将 SRGS 语法文件添加到语音识别器的约束集合中。
SRGS 版本 1.0 是用于创建用于语音识别的 XML 格式语法的行业标准标记语言。 尽管通用 Windows 应用提供了使用 SRGS 创建语音识别语法的替代方法,但你可能会发现,使用 SRGS 创建语法会产生最佳结果,尤其是对于更涉及的语音识别方案。
SRGS 语法提供了一整套功能,可帮助你为应用构建复杂的语音交互。 例如,使用 SRGS 语法,可以:
- 指定必须识别单词和短语的顺序。
- 将来自多个列表和短语的字词合并以进行识别。
- 链接至其他语法资源。
- 为替代词或短语分配权重,以增加或减少用于匹配语音输入的可能性。
- 包括可选字词或短语。
- 使用特殊规则来帮助筛选掉未指定的或未预料的输入,例如与语法不匹配的随机语音或背景噪音。
- 使用语义定义语音识别对应用意味着什么。
- 可以在语法中内嵌指定发音,或者通过链接至词典来指定。
有关 SRGS 元素和属性的详细信息,请参阅 SRGS 语法 XML 参考 。 若要开始创建 SRGS 语法,请参阅 如何创建基本 XML 语法。
请记住以下几点:
- 可以将多个语法文件约束添加到约束集合。
- 对符合 SRGS 规则的基于 XML 的语法文档使用 .grxml 文件扩展名。
此示例使用在名为 srgs.grxml 的文件中定义的 SRGS 语法(稍后所述)。 在文件属性中,“包操作”设置为“内容”,并将“复制到输出目录”设置为“始终复制”:
private async void Colors_Click(object sender, RoutedEventArgs e)
{
// Create an instance of SpeechRecognizer.
var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();
// Add a grammar file constraint to the recognizer.
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);
// Compile the constraint.
await speechRecognizer.CompileConstraintsAsync();
// Start recognition.
Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = await speechRecognizer.RecognizeWithUIAsync();
// Do something with the recognition result.
var messageDialog = new Windows.UI.Popups.MessageDialog(speechRecognitionResult.Text, "Text spoken");
await messageDialog.ShowAsync();
}
此 SRGS 文件 (srgs.grxml) 包括语义解释标记。 这些标记提供一种机制,用于将语法匹配数据返回到应用。 语法必须符合万维网联盟(W3C)的规范,即 语音识别(SISR)1.0 语义解释。
在这里,我们监听“是”和“否”的各种变体。
<grammar xml:lang="en-US"
root="yesOrNo"
version="1.0"
tag-format="semantics/1.0"
xmlns="http://www.w3.org/2001/06/grammar">
<!-- The following rules recognize variants of yes and no. -->
<rule id="yesOrNo">
<one-of>
<item>
<one-of>
<item>yes</item>
<item>yeah</item>
<item>yep</item>
<item>yup</item>
<item>un huh</item>
<item>yay yus</item>
</one-of>
<tag>out="yes";</tag>
</item>
<item>
<one-of>
<item>no</item>
<item>nope</item>
<item>nah</item>
<item>uh uh</item>
</one-of>
<tag>out="no";</tag>
</item>
</one-of>
</rule>
</grammar>
管理约束
加载约束集合进行识别后,应用可以通过将约束的 IsEnabled 属性设置为 true 或 false 来管理为识别作启用哪些约束。 默认设置为 true。
通常,更有效的方法是一次性加载约束,然后根据需要启用或禁用它们,而不是在每次识别操作中反复加载、卸载和编译约束。 根据需要使用 IsEnabled 属性。
限制约束的数量可以限制语音识别器需要搜索和匹配语音输入的数据量。 这可以提高语音识别的性能和准确性。
根据应用程序在当前识别操作的上下文中预期的短语来确定需要启用哪些约束。 例如,如果当前应用上下文显示颜色,则可能不需要启用识别动物名称的约束。
若要提示用户输入可说出的内容,请使用 SpeechRecognizerUIOptions.AudiblePrompt 和 SpeechRecognizerUIOptions.ExampleText 属性,这些属性由 SpeechRecognizer.UIOptions 属性设置。 准备用户在识别操作期间可以说的内容会增加他们说出与有效约束条件匹配的短语的可能性。