uwp 自定义语音识别规则

xml  code

----------------------------------------------------

<Page

    x:Class="MyApp.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:MyApp"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <StackPanel>

        <Button Content="开始说话" Click="OnClick"/>

        <TextBlock TextWrapping="Wrap" FontSize="20">

            <Run>

                请说出:把颜色改为[红色]

            </Run>

            <LineBreak/>

            <Run>或者:把颜色设置为[红色]</Run>

            <LineBreak/>

            <Run>

                [红色]必须是以下值中的一个:红色、白色、蓝色、绿色

            </Run>

        </TextBlock>

        <TextBlock Name="tbRes" FontSize="24" TextWrapping="Wrap" Foreground="LightGreen"/>

    </StackPanel>

</Page>

C#  code

-----------------------------------------------------------------------------------------------------------------------------------

 public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;

        }

        /// <summary>

        /// Invoked when this page is about to be displayed in a Frame.

        /// </summary>

        /// <param name="e">Event data that describes how this page was reached.

        /// This parameter is typically used to configure the page.</param>

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

            // TODO: Prepare page for display here.

            // TODO: If your application contains multiple pages, ensure that you are

            // handling the hardware Back button by registering for the

            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.

            // If you are using the NavigationHelper provided by some templates,

            // this event is handled for you.

        }

        private async void OnClick(object sender, RoutedEventArgs e)

        {

            Button btn = sender as Button;

            btn.IsEnabled = false;

            using (SpeechRecognizer recognizer = new SpeechRecognizer())

            {

                try

                {

                    // 加载语法文件

                    StorageFile sgrsFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///sgrs.xml"));

                    // 创建识别约束

                    SpeechRecognitionGrammarFileConstraint grammarfileConstraint = new SpeechRecognitionGrammarFileConstraint(sgrsFile);

                    // 加入到约束集合中

                    recognizer.Constraints.Add(grammarfileConstraint);

                    // 编译约束

                    SpeechRecognitionCompilationResult compilationResult = await recognizer.CompileConstraintsAsync();

                    if (compilationResult.Status == SpeechRecognitionResultStatus.Success)

                    {

                        // 开始识别

                        SpeechRecognitionResult result = await recognizer.RecognizeAsync();

                        if(result.Status == SpeechRecognitionResultStatus.Success)

                        {

                            tbRes.Text = string.Format("识别结果:{0}。", result.Text);

                        }

                    }

                }

                catch (Exception ex)

                {

                    System.Diagnostics.Debug.WriteLine(ex.Message);

                }

            }

                btn.IsEnabled = true;

        }

    }

约束文件   sgr.xml

-----------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8" ?>

<grammar xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0" version="1.0" xml:lang="zh-cn" root="color">

  <rule id="color">

    <item repeat="0-1">把</item>

     <item>颜色</item>     

   

   <one-of>

      <item>设置为</item>

      <item>改为</item>

    </one-of>

    <one-of>

      <item>红色</item>

      <item>白色</item>

      <item>绿色</item>

      <item>蓝色</item>

    </one-of>

  </rule>

</grammar>

原文地址:https://www.cnblogs.com/bruce1992/p/14223599.html