次の方法で共有


カスタム認識の制約の定義

音声認識にカスタム制約を定義して使用する方法について説明します。

重要な API: SpeechRecognitionTopicConstraintSpeechRecognitionListConstraintSpeechRecognitionGrammarFileConstraint

音声認識では、認識可能なボキャブラリを定義するために、少なくとも 1 つの制約が必要です。 制約が指定されていない場合は、ユニバーサル Windows アプリの定義済みのディクテーション文法が使用されます。 音声認識を参照してください。

制約の追加

SpeechRecognizer.Constraints プロパティを使用して、音声認識エンジンに制約を追加します。

ここでは、アプリ内から使用される 3 種類の音声認識の制約について説明します。 (Cortana 音声コマンドの制約については、「 Cortana を使用して音声コマンドを使用してフォアグラウンド アプリをアクティブ化する」を参照してください)。

各音声認識エンジンは、1 つの制約コレクションを持つことができます。 次の制約の組み合わせのみが有効です。

  • 単一トピック制約 (ディクテーションまたは Web 検索)
  • Windows 10 Fall Creators Update (10.0.16299.15) 以降では、1 つのトピック制約をリスト制約と組み合わせることができます
  • リスト制約または文法ファイル制約の組み合わせ。

Important

認識プロセスを開始する前に 、SpeechRecognizer.CompileConstraintsAsync メソッドを呼び出して制約をコンパイルします。

Web 検索用の文法を指定 (SpeechRecognitionTopicConstraint)

トピック制約 (ディクテーションまたは Web 検索文法) は、音声認識エンジンの制約コレクションに追加する必要があります。

SpeechRecognitionListConstraintSpeechRecognitionTopicConstraint と組み合わせて使用すると、ディクテーション中に使用される可能性が高いと思われるドメイン固有のキーワードのセットを指定することで、ディクテーションの精度を向上させることができます。

ここでは、制約コレクションに 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) には、セマンティック解釈タグが含まれています。 これらのタグは、文法の一致データをアプリに返すメカニズムを提供します。 文法は、World Wide Web Consortium (W3C) Semantic Interpretation for Speech Recognition (SISR) 1.0 仕様に準拠している必要があります。

ここでは、"yes" と "no" のバリエーションを認識します。

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

通常、各認識操作の制約の読み込み、アンロード、コンパイルを行うよりも、制約を 1 回読み込み、必要に応じて有効および無効にする方が効率的です。 必要に応じて 、IsEnabled プロパティを使用します。

制約の数を制限すると、音声認識エンジンが検索して音声入力と照合するために必要なデータの量を制限できます。 これにより、音声認識のパフォーマンスと精度の両方が向上します。

現在の認識操作のコンテキストでアプリが期待できる語句に基づいて、有効にする制約を決定します。 たとえば、現在のアプリ コンテキストで色を表示する場合、動物の名前を認識する制約を有効にする必要がない可能性があります。

ユーザーに対して話すべき内容を促すには、SpeechRecognizerUIOptions.AudiblePrompt プロパティと SpeechRecognizerUIOptions.ExampleText プロパティを使用します。これらのプロパティの設定は SpeechRecognizer.UIOptions プロパティによって行います。 認識操作中にユーザーが言える内容を準備すると、アクティブな制約に一致する語句を読み上げる可能性が高くなります。

  • 音声での対話

Samples