Windows アプリ SDK アプリで音声認識にカスタム制約を定義して使用する方法について説明します。
Important
音声認識には MSIX パッケージ ID が必要です。
Windows.Media.SpeechRecognition API は、アプリがパッケージ ID (パッケージ化または外部の場所でパッケージ化) で実行されている場合にのみ使用できます。 パッケージ化されていないアプリでは、これらの API を使用できません。
主要API
- SpeechRecognitionTopicConstraint
- SpeechRecognitionListConstraint
- SpeechRecognitionGrammarFileConstraint
Overview
音声認識では、認識可能なボキャブラリを定義するために、少なくとも 1 つの制約が必要です。 制約を指定しない場合は、定義済みのディクテーション文法が使用されます。 音声認識を参照してください。
SpeechRecognizer.Constraints プロパティを使用して、音声認識エンジンに制約を追加します。 次の 3 種類の制約を使用できます。
- SpeechRecognitionTopicConstraint — 定義済みの文法 (ディクテーションまたは Web 検索) に基づく制約。
- SpeechRecognitionListConstraint — 単語または語句のリストに基づく制約。
- SpeechRecognitionGrammarFileConstraint — 音声認識文法仕様 (SRGS) ファイルで定義されている制約。
各音声認識エンジンは、1 つの制約コレクションを持つことができます。 次の組み合わせが有効です。
- 1 つのトピック制約 (ディクテーションまたは Web 検索)
- 1 つのトピック制約とリスト制約の組み合わせ
- リスト制約と文法ファイル制約の組み合わせ
Important
SpeechRecognizer.CompileConstraintsAsyncを呼び出して、認識プロセスを開始する前に制約をコンパイルします。
Web 検索用の文法を指定 (SpeechRecognitionTopicConstraint)
private async void WeatherSearch_Click(object sender, RoutedEventArgs e)
{
var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();
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);
await speechRecognizer.CompileConstraintsAsync();
Windows.Media.SpeechRecognition.SpeechRecognitionResult result =
await speechRecognizer.RecognizeWithUIAsync();
resultTextBlock.Text = result.Text;
}
プログラムによるリスト制約を指定する (SpeechRecognitionListConstraint)
リスト制約は、単語または語句のリストから単純な文法を作成するための軽量な方法です。 リスト制約は、短い個別の語句を認識する場合に適しています。
private async void YesOrNo_Click(object sender, RoutedEventArgs e)
{
var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();
string[] responses = { "Yes", "No" };
var listConstraint = new Windows.Media.SpeechRecognition.SpeechRecognitionListConstraint(
responses, "yesOrNo");
speechRecognizer.UIOptions.ExampleText = @"Ex. 'yes', 'no'";
speechRecognizer.Constraints.Add(listConstraint);
await speechRecognizer.CompileConstraintsAsync();
Windows.Media.SpeechRecognition.SpeechRecognitionResult result =
await speechRecognizer.RecognizeWithUIAsync();
resultTextBlock.Text = result.Text;
}
SRGS 文法制約を指定する (SpeechRecognitionGrammarFileConstraint)
SRGS 文法には、単語の読み上げ順序の指定、複数のリストからの単語の組み合わせ、他の文法へのリンク、セマンティック解釈の使用など、複雑な音声操作のための機能の完全なセットが用意されています。
private async void Colors_Click(object sender, RoutedEventArgs e)
{
var speechRecognizer = 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");
speechRecognizer.UIOptions.ExampleText = @"Ex. 'blue background', 'green text'";
speechRecognizer.Constraints.Add(grammarFileConstraint);
await speechRecognizer.CompileConstraintsAsync();
Windows.Media.SpeechRecognition.SpeechRecognitionResult result =
await speechRecognizer.RecognizeWithUIAsync();
resultTextBlock.Text = result.Text;
}
SRGS 規則に準拠する XML ベースの文法ドキュメントには、 .grxml ファイル拡張子を使用します。 ファイルのビルド アクションをコンテンツに設定し、出力ディレクトリにコピーして常にコピーします。
SRGS 要素と属性の詳細については、 SRGS 文法 XML リファレンスを参照してください。
制約の管理
制約コレクションを読み込んだ後は、 IsEnabled プロパティを true または falseに設定することで、個々の制約を有効または無効にすることができます。 既定値は true です。
制約を 1 回読み込んでから、必要に応じて有効または無効にする方が、各認識操作の制約の読み込み、アンロード、コンパイルよりも効率的です。
アクティブな制約の数を制限すると、音声認識のパフォーマンスと精度の両方が向上します。 現在のコンテキストでアプリで想定できる語句に基づいて、有効にする制約を決定します。
関連資料
Windows developer