1
votes

currently i try to implement speech recognition function to my UWP app, so far i create a continuous dictation function to recognize user's speech,but i want it just recognize the word or phase that in the grammar file how to create and add grammar for it?
here is my code for continuous recognition:

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; 

    });
}
1

1 Answers

1
votes

For your requirement, you could add the SRGS file to contSpeechRecognizer. And I have create a simple sample base on your segment code.

The following is Colors.grxml file that used as constraint for your 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>

Usage

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();
}

For more, please refer to Define custom recognition constraints.