C#中的语义解释SRGS

时间:2018-11-01 08:13:50

标签: c# xml

我现在开始使用Microsoft Hololense,并且想编写一个简单的C#程序,包括语音识别。如何在c#程序中的grxml中访问标记值“是”?这里的代码

Colors.grxml

<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>

现在使用C#:

private async void btnStartRecognition_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. 'yes', 'yeah'";
        speechRecognizer.Constraints.Add(grammarFileConstraint);

        // Compile the constraint.
        await speechRecognizer.CompileConstraintsAsync();

        // Start recognition.
        Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = await speechRecognizer.RecognizeWithUIAsync();

        //Here i want to access the Tag-Value "Yes" if the user said yeah. 
        //I tried speechRecognitionResult.SemanticInterpretation.ToString

    }

1 个答案:

答案 0 :(得分:0)

对于UWP应用,我认为使用speechRecognitionResult.SemanticInterpretation.Properties提供了一种找到解决方案的好方法

我找到了一个示例:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/SpeechRecognitionAndSynthesis