SemanticResultValue 類別

定義

代表語意值,並選擇性地將值與語音辨識文法的元件相關聯。

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

範例

下列範例會傳 Grammar 回可辨識命令 「Set/Change/Alter Foreground/Background ... [色彩清單]」。 SemanticResultValueSemanticResultKey 實例 (搭配 ChoicesGrammarBuilder 物件) ,可用來定義可在辨識上剖析的語意。 剖析的語意會決定所要求的色彩,以及要修改前景或背景。

private Grammar FgBgColorGrammar()
{
  Grammar grammar = null;

  // Allow the command to begin with set, alter, change.
  Choices introChoices = new Choices();
  foreach (string introString in new string[] { "Change", "Set", "Alter" })
  {
    GrammarBuilder introGB = new GrammarBuilder(introString);
    introChoices.Add(new SemanticResultValue(introGB, String.Format("Command: {0}", introString)));
  }

  GrammarBuilder cmdIntro = new GrammarBuilder(introChoices);

  // Define the arguments for the command to select foreground or background
  // and to change their color as semantic values.
  Choices fgOrbgChoice = new Choices();
  GrammarBuilder backgroundGB=new GrammarBuilder("background");
  backgroundGB.Append(new SemanticResultValue(true));
  fgOrbgChoice.Add(backgroundGB);
  fgOrbgChoice.Add((GrammarBuilder)new SemanticResultValue("foreground", false));
  SemanticResultKey fgOrbgChoiceKey = new SemanticResultKey("BgOrFgBool", fgOrbgChoice);
  Choices colorChoice = new Choices();
  foreach (string colorName in System.Enum.GetNames(typeof(KnownColor)))
  {

    // Use implicit conversion of SemanticResultValue to GrammarBuilder.
    colorChoice.Add(
        (GrammarBuilder) (new SemanticResultValue(colorName, (Color.FromName(colorName)).Name)));
  }

  // Create a GrammarBuilder for CmdArgs to be appended to CmdIntro using
  // semantic keys.
  GrammarBuilder cmdArgs = new GrammarBuilder();
  cmdArgs.Append(new SemanticResultKey("BgOrFgBool", fgOrbgChoice));
  cmdArgs.AppendWildcard();
  cmdArgs.Append(new SemanticResultKey("colorStringList", colorChoice));

  GrammarBuilder cmds =
      GrammarBuilder.Add(
                       cmdIntro,
                       new GrammarBuilder(new SemanticResultKey("Cmd Args", cmdArgs)));
  grammar = new Grammar(cmds);
  grammar.Name = "Tree [Set,change,alter] [foreground,background] * color";
  return grammar;
}

備註

SemanticResultValue搭配 和 Choices 使用 和 SemanticResultKey 物件 GrammarBuilder 是設計 的 Grammar 語意結構的最簡單方式。 透過 上的 RecognizedPhrase 屬性取得 的 實例,可存取片語的 SemanticValueSemantics 語意資訊。

注意

由 物件管理 SemanticResultValue 的值是由 Object 傳遞至其建構函式的實例所定義。 這個 Object 的基礎型別必須是 boolintfloatstring 。 任何其他類型都會防止使用 SemanticResultValue 建構 Grammar 實例。

實例的 SemanticResultValue 一般用法會將 實例與 的可辨識元件 Grammar 產生關聯,例如片語、規則或 Choices 物件。 如果相關聯的元件是當做辨識作業的一部分使用,則會 SemanticResultValue 用來在傳回片語的語意中定義值。

根據用來建立 SemanticResultValue 的建構函式而定,有兩個基本方法可將實例與文法專案產生關聯 SemanticResultValue

若要在 Grammar 辨識中使用 ,所有 SemanticResultValue 實例都必須與該 Grammar 所使用的其中一個 SemanticValue 物件相關聯。 這是藉由將語意索引鍵與 SemanticResultValue 建立關聯來完成。

語意索引鍵可以使用 物件明確地附加至 SemanticResultValueSemanticResultKeySemanticResultValue 未明確附加至索引鍵的實例會附加至預設 SemanticValue 的根索引鍵。

SemanticResultValue在 用來設定 Value 之後,不論它是否以預設根索引鍵或任何特定的 SemanticResultKey 標記,該值都不得修改,否則會在辨識作業期間發生例外狀況。

下列範例會造成例外狀況,因為它會設定並修改 的 Grammar 根目錄 Value

GrammarBuilder gb=new GrammarBuilder();
gb.Append(new SemanticResultValue("One"));
gb.Append(new SemanticResultValue("Two"));

另一方面,允許下列範例中的程式碼。 雖然它會定義 的 SemanticResultValue 多個實例,但它們會包含在 物件中 Choices ,而且只會使用其中一個實例來設定索引鍵 bgOrfgText 的值。

Choices fgOrbgChoice = new Choices();
fgOrbgChoice.Add((GrammarBuilder)new SemanticResultValue("background"));
fgOrbgChoice.Add((GrammarBuilder)new SemanticResultValue("foreground"));
SemanticResultKey fgOrbgChoiceKey = new SemanticResultKey("BgOrFgText", fgOrbgChoice);

建構函式

SemanticResultValue(GrammarBuilder, Object)

初始化 SemanticResultValue 類別的新執行個體,並且使語意值與 GrammarBuilder 物件產生關聯。

SemanticResultValue(Object)

初始化 SemanticResultValue 類別的新執行個體,並指定語意值。

SemanticResultValue(String, Object)

初始化 SemanticResultValue 類別的新執行個體,並且使語意值與 String 物件產生關聯。

方法

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToGrammarBuilder()

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

ToString()

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

(繼承來源 Object)

適用於

另請參閱