共用方式為


定義自訂辨識限制式

瞭解如何定義和使用語音辨識的自訂條件約束。

重要 API:SpeechRecognitionTopicConstraint、SpeechRecognitionListConstraint、SpeechRecognitionGrammarFileConstraint

語音辨識至少需要一個條件約束來定義可辨識的詞彙。 如果未指定任何條件約束,則會使用通用 Windows 應用程式的預先定義聽寫文法。 請參閱語音辨識

新增限制式

使用SpeechRecognizer.Constraints屬性,將條件約束新增至語音辨識器。

在這裡,我們將討論從應用程式內使用的三種語音辨識條件約束。 (如需 Cortana 語音命令條件約束,請參閱 在 Cortana 中使用語音命令啟動前景應用程式。

每個語音辨識器都可以有一個條件約束集合。 只有這些條件約束組合有效:

  • 單一主題條件約束 (聽寫或 Web 搜尋)
  • 針對 Windows 10 Fall Creators Update (10.0.16299.15) 和更新版本,單一主題條件約束可以與清單條件約束結合
  • 清單條件約束和/或文法檔案條件約束的組合。

重要

呼叫 SpeechRecognizer.CompileConstraintsAsync 方法,在開始辨識程式之前先編譯條件約束。

指定網頁搜尋文法 (SpeechRecognitionTopicConstraint)

主題條件約束(聽寫或 Web 搜尋文法)必須新增至語音辨識器的條件約束集合。

注意

您現在可以使用 SpeechRecognitionListConstraint 搭配 Web 服務 SpeechRecognitionTopicConstraint,透過提供一組您認為可能在聽寫時用到的網域特定關鍵字,來提高聽寫正確性。

在這裡,我們會將 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) 包含語意解譯標記。 這些標籤提供將文法比對資料傳回至應用程式的機制。 文法必須符合萬維網聯盟 (W3C) 語音辨識 (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

加載條件約束一次、視需要啟用和停用條件約束,而不是載入、卸除和編譯每個辨識作業的條件約束通常更有效率。 視需要使用IsEnabled屬性。

限制條件約束數目可限制語音辨識器需要搜尋和比對語音輸入的資料量。 這可以改善語音辨識的效能和精確度。

根據應用程式在目前辨識作業內容中預期使用的詞組,決定啟用哪些條件約束。 例如,如果目前的應用程式內容是顯示色彩,您可能不需要啟用可辨識動物名稱的條件約束。

若要提示使用者說出哪些內容,請使用SpeechRecognizerUIOptions.AudiblePrompt和SpeechRecognizerUIOptions.ExampleText 屬性,這些屬性是透過SpeechRecognizer.UIOptions 屬性所設定。 準備使用者在辨識作業期間可以說出的內容,會增加他們說話與作用中條件約束相符的可能性。

範例