SemanticResultKey 類別

定義

將索引鍵字串與SemanticResultValue值關聯在一起,以定義SemanticValue物件。

public ref class SemanticResultKey
public class SemanticResultKey
type SemanticResultKey = class
Public Class SemanticResultKey
繼承
SemanticResultKey

範例

下列範例會 Grammar 建立 來辨識 「My password is ...」格式的密碼輸入,其中實際輸入會與萬用字元相符。

萬用字元會以語意索引鍵標記,處理常式會 SpeechRecognized 檢查此標記是否存在,以確認密碼輸入是否已發生。

private void pwdGrammar()
{
  GrammarBuilder pwdBuilder = new GrammarBuilder("My Password is");
  GrammarBuilder wildcardBuilder = new GrammarBuilder();
  wildcardBuilder.AppendWildcard();
  SemanticResultKey wildcardKey= new SemanticResultKey("Password", wildcardBuilder);
  pwdBuilder+=wildcardKey;
  Grammar grammar = new Grammar(pwdBuilder);
  grammar.Name = "Password input";

  grammar.SpeechRecognized += delegate(object sender, SpeechRecognizedEventArgs eventArgs)
  {
    SemanticValue semantics = eventArgs.Result.Semantics;
    RecognitionResult result=eventArgs.Result;

    if (!semantics.ContainsKey("Password"))
    {
      SpeechUI.SendTextFeedback(eventArgs.Result, "No Password Provided", false);
    }
    else
    {
      RecognizedAudio pwdAudio = result.GetAudioForWordRange(result.Words[3], result.Words[result.Words.Count - 1]);
      MemoryStream pwdMemoryStream = new MemoryStream();
      pwdAudio.WriteToAudioStream(pwdMemoryStream);
      if (!IsValidPwd(pwdMemoryStream))
      {
        string badPwd = System.IO.Path.GetTempPath() + "BadPwd" + (new Random()).Next().ToString() + ".wav";
        FileStream waveStream = new FileStream(badPwd, FileMode.Create);
        pwdAudio.WriteToWaveStream(waveStream);
        waveStream.Flush();
        waveStream.Close();
        SpeechUI.SendTextFeedback(eventArgs.Result, "Invalid Password", false);

      }
    }
  };
  grammar.Enabled = true;
  _recognizer.LoadGrammar(grammar);
  UpdateGrammarTree(_grammarTreeView, _recognizer);

}

備註

System.Speech 中語意運算式的基本單位是 SemanticValue ,這是索引鍵/值組。

使用 SemanticResultKey 物件時,您可以標記 SemanticResultValue 物件和字串中包含的 GrammarBuilder 實例,以便從辨識上的實例輕鬆存取 SemanticValue 這些值。

您可以使用 SemanticResultValueSemanticResultKey 物件搭配 GrammarBuilderChoices 物件來定義語音辨識文法的語意結構。 若要存取辨識結果中的語意資訊,請透過 上的 RecognizedPhrase 屬性取得 的 SemanticsSemanticValue 實例。

建構函式

SemanticResultKey(String, GrammarBuilder[])

指派語意索引鍵給一或多個用來建立語音辨識文法的GrammarBuilder物件。

SemanticResultKey(String, String[])

指派語意索引鍵給一或多個用來建立語音辨識文法的String執行個體。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToGrammarBuilder()

傳回從目前 GrammarBuilder 執行個體建構的 SemanticResultKey 執行個體。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱