目前我尝试在我的UWP应用程序中实现语音识别功能,到目前为止我创建了一个连续的听写功能来识别用户的语音,但我希望它只是识别语法文件中的单词或阶段如何为它创建和添加语法?
这是我的持续识别代码:
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
SpeechRecognizer contSpeechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();
await contSpeechRecognizer.CompileConstraintsAsync();
contSpeechRecognizer.ContinuousRecognitionSession.ResultGenerated += ContinuousRecognitionSession_ResultGenerated;
contSpeechRecognizer.ContinuousRecognitionSession.AutoStopSilenceTimeout = TimeSpan.FromDays(1);
contSpeechRecognizer.ContinuousRecognitionSession.Completed += ContinuousRecognitionSession_Completed;
await contSpeechRecognizer.ContinuousRecognitionSession.StartAsync();
}
private async void ContinuousRecognitionSession_Completed(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionCompletedEventArgs args)
{
await contSpeechRecognizer.ContinuousRecognitionSession.StartAsync();
}
private async void ContinuousRecognitionSession_ResultGenerated(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionResultGeneratedEventArgs args)
{
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
speechResult = args.Result.Text;
});
}
答案 0 :(得分:1)
根据您的要求,您可以将SRGS file添加到contSpeechRecognizer
。我已经根据您的细分代码创建了一个简单的示例。
以下是Colors.grxml
文件,用作您的约束
contSpeechRecognizer
。
<grammar xml:lang="en-US"
root="colors"
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="colors">
<one-of>
<item>
<one-of>
<item>green</item>
<item>yellow</item>
<item>orange</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>
<强>用法强>
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
SpeechRecognizer contSpeechRecognizer = 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");
contSpeechRecognizer.Constraints.Add(grammarFileConstraint);
await contSpeechRecognizer.CompileConstraintsAsync();
contSpeechRecognizer.ContinuousRecognitionSession.AutoStopSilenceTimeout = TimeSpan.FromSeconds(2);
contSpeechRecognizer.ContinuousRecognitionSession.ResultGenerated += ContinuousRecognitionSession_ResultGenerated;
contSpeechRecognizer.ContinuousRecognitionSession.Completed += ContinuousRecognitionSession_Completed;
await contSpeechRecognizer.ContinuousRecognitionSession.StartAsync();
}