學習如何定義並使用語音辨識的自訂約束。
重要 API: 語音識別主題限制、 語音識別清單限制、 語音識別文法檔案限制
語音辨識至少需要一個限制來定義可辨識的詞彙。 若未指定限制,則使用通用 Windows 應用程式的預設語音輸入文法。 詳見 語音辨識。
新增條件約束
使用 SpeechRecognizer.Constraints 屬性為語音識別器新增限制。
在這裡,我們將介紹應用程式內部使用的三種語音辨識限制。 (關於 Cortana 語音指令的限制,請參見 透過 Cortana 啟用帶有語音指令的前景應用程式。)
- 語音辨識主題限制——基於預設文法(聽寫或網路搜尋)的限制。
- 語音識別清單限制——基於單字或片語清單的限制。
- 語音識別文法檔案約束——在語音辨識文法規範(SRGS)檔案中定義的限制。
每個語音識別器可以有一個約束集合。 只有以下這些約束組合有效:
- 單一主題限制(語音輸入或網頁搜尋)
- 對於 Windows 10 秋季創作者更新(10.0.16299.15)及更新版本,單一主題限制可以與清單限制結合使用
- 是清單限制和/或文法檔案限制的組合。
這很重要
在開始辨識程序前,呼叫 SpeechRecognizer.CompileConstraintsAsync 方法來編譯約束。
指定一個網頁搜尋文法(語音識別主題限制)
主題約束(如語音輸入或網路搜尋文法)必須加入語音識別器的限制集合中。
備註
你可以將 SpeechRecognitionListConstraint 與 SpeechRecognitionTopicConstraint 結合使用,透過提供一組你認為在語音輸入時可能使用的領域特定關鍵字,來提升語音輸入的準確度。
在這裡,我們將一個網頁搜尋文法加入限制集合。
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)包含語意詮釋標籤。 這些標籤提供了一種機制,讓你能將文法匹配資料回傳到你的應用程式。 文法必須符合萬維網聯盟(W3C)語音 辨識語意解釋(SISR)1.0 規範。
在這裡,我們聆聽「是」和「否」的變體。
<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 屬性設為 真 或 假來管理哪些約束可啟用以進行識別操作。 預設設定是 正確的。
通常一次載入約束,視需要啟用或停用約束,比每次讀取、卸載和編譯每個識別操作的約束更高效。 依需求使用 IsEnabled 屬性。
限制限制數量可限制語音辨識器需要搜尋並與語音輸入匹配的資料量。 這不僅能提升語音辨識的效能,也能提升準確度。
根據應用程式在當前識別操作情境中預期的詞語,決定哪些限制是啟用的。 舉例來說,如果目前的應用程式上下文是顯示一種顏色,你大概不需要啟用能辨識動物名稱的限制。
要提示使用者可說什麼,請使用 SpeechRecognizerUIOptions.AudiblePrompt 和 SpeechRecognizerUIOptions.ExampleText 屬性,這些屬性皆由 SpeechRecognizer.UIOptions 屬性設定。 在識別操作中為使用者能說出的話語做好準備,能增加他們說出可與主動約束相匹配的詞語的可能性。