學習如何在你的 Windows 應用程式 SDK 應用程式中定義並使用語音辨識的自訂限制。
這很重要
語音辨識需要 MSIX 套件的識別碼。
Windows.Media.SpeechRecognition這些 API 只有在你的應用程式以套件身份(打包或以外部位置打包)執行時才可用。 未封裝的應用程式無法使用這些 API。
主要 API
概觀
語音辨識至少需要一個限制來定義可辨識的詞彙。 如果你沒有指定限制,就會使用預設的語音輸入文法。 詳見 語音辨識。
利用這個 SpeechRecognizer.Constraints 屬性為語音識別器添加限制。 有三種類型的約束:
- 語音識別主題限制 — 基於預設文法(聽寫或網路搜尋)的限制。
- 語音識別清單限制 — 基於單字或片語清單的限制。
- 語音識別文法檔案約束 — 在語音辨識文法規範(SRGS)檔案中定義的限制。
每個語音識別器可以有一個約束集合。 以下組合有效:
- 單一主題限制(語音輸入或網頁搜尋)
- 單一主題限制與清單限制的結合
- 列表限制與/或文法檔案限制的組合
這很重要
在開始識別程序前,先呼叫 SpeechRecognizer.CompileConstraintsAsync 編譯約束條件。
指定一個網頁搜尋文法(語音識別主題限制)
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。
一次載入約束,然後視需要啟用或停用限制,比每次識別操作的載入、卸載與編譯約束更有效率。
限制主動約束數量可提升語音辨識的效能與準確度。 根據應用程式在當前情境中預期的詞語,決定要啟用哪些限制。