SrgsDocument 建構函式

定義

初始化 SrgsDocument 類別的新執行個體。

多載

SrgsDocument()

初始化 SrgsDocument 類別的新執行個體。

SrgsDocument(GrammarBuilder)

SrgsDocument 物件初始化 GrammarBuilder 類別的新執行個體。

SrgsDocument(SrgsRule)

初始化 SrgsDocument 類別的新執行個體,並指定要成為文法根規則的 SrgsRule 物件。

SrgsDocument(String)

初始化 SrgsDocument 類別的新執行個體,並指定用來填入 SrgsDocument 執行個體之 XML 文件的位置。

SrgsDocument(XmlReader)

從參考 XML 格式文法檔案的 SrgsDocument 執行個體,初始化 XmlReader 類別的新執行個體。

備註

使用類別的函 SrgsDocument 式,您可以從 SrgsDocument GrammarBuilder SrgsRule 包含 XML 格式文法路徑的字串,建立、或物件的實例, XmlReader 或者您可以起始空白的實例 SrgsDocument

SrgsDocument()

初始化 SrgsDocument 類別的新執行個體。

public:
 SrgsDocument();
public SrgsDocument ();
Public Sub New ()

範例

下列範例會建立 SrgsDocument 物件,然後建立名為的公用規則 winnerRule 。 然後,它會建立 SrgsItem ,其中包含「已贏得世界盃的全國是:」字串,並將此專案新增至規則的 Elements 屬性。 然後,此範例會建立兩個規則 (ruleEuroperuleSAmerica) ,其中每個都是 SrgsOneOf 包含三個物件的物件 SrgsItem 。 之後, SrgsOneOf 會建立另一個物件,其中包含 SrgsRuleRef 參考和的 ruleEurope 物件 ruleSAmerica 。 新 SrgsOneOf 物件接著會加入的 Elements 屬性 winnerRule 。 之後,所有三個規則 (winnerRuleruleEuroperuleSAmerica) 都會加入的屬性中 Rules SrgsDocument 。 最後,這三個規則會編譯成文法的二進位標記法。

public void WorldSoccerWinners ()  
{  

  // Create an SrgsDocument, create a new rule  
  // and set its scope to public.  
  SrgsDocument document = new SrgsDocument();  
  SrgsRule winnerRule = new SrgsRule("WorldCupWinner");  
  winnerRule.Scope = SrgsRuleScope.Public;  

  // Add the introduction.  
  winnerRule.Elements.Add(new SrgsItem("A nation that has won the World Cup is: "));  

  // Create the rule for the European nations.  
  SrgsOneOf oneOfEurope = new SrgsOneOf(new SrgsItem[] {new SrgsItem("England"),   
    new SrgsItem("France"), new SrgsItem("Germany"), new SrgsItem("Italy")});  
  SrgsRule ruleEurope = (new SrgsRule("EuropeanNations", new SrgsElement[] {oneOfEurope}));  

  // Create the rule for the South American nations.  
  SrgsOneOf oneOfSAmerica = new SrgsOneOf(new SrgsItem[] {new SrgsItem("Argentina"),   
    new SrgsItem("Brazil"), new SrgsItem("Uruguay")});  
  SrgsRule ruleSAmerica = (new SrgsRule("SouthAmericanNations", new SrgsElement[] {oneOfSAmerica}));  

  // Add references to winnerRule for ruleEurope and ruleSAmerica.  
  winnerRule.Elements.Add(new SrgsOneOf(new SrgsItem[] {(new SrgsItem   
    (new SrgsRuleRef(ruleEurope))), new SrgsItem(new SrgsRuleRef(ruleSAmerica))}));  

  // Add all the rules to the document and make winnerRule   
  // the root rule of the document.  
  document.Rules.Add(new SrgsRule[] {winnerRule, ruleEurope, ruleSAmerica});  
  document.Root = winnerRule;  

  String fileName = Path.GetTempFileName();  
  using (FileStream stream = new FileStream(fileName, FileMode.Create))   
  {   

    // Compile the grammar to a binary format.  
    SrgsGrammarCompiler.Compile(document, stream);   
  }  
}  

備註

這個函式會建立空的 SrgsDocument 實例。 若要在空白實例內建立文法 SrgsDocument ,請新增代表 SRGS 元素的類別實例,例如 SrgsRuleSrgsRuleRefSrgsOneOfSrgsItem

適用於

SrgsDocument(GrammarBuilder)

SrgsDocument 物件初始化 GrammarBuilder 類別的新執行個體。

public:
 SrgsDocument(System::Speech::Recognition::GrammarBuilder ^ builder);
public SrgsDocument (System.Speech.Recognition.GrammarBuilder builder);
new System.Speech.Recognition.SrgsGrammar.SrgsDocument : System.Speech.Recognition.GrammarBuilder -> System.Speech.Recognition.SrgsGrammar.SrgsDocument
Public Sub New (builder As GrammarBuilder)

參數

builder
GrammarBuilder

GrammarBuilder 物件,用於建立 SrgsDocument 執行個體。

例外狀況

buildernull

範例

下列範例會使用物件,在實例中建立文法 GrammarBuilder Choices 。 然後,它會 SrgsDocumentGrammarBuilder 物件建立。

GrammarBuilder builder = null;  

// Create new Choices objects and add countries/regions, and create GrammarBuilder objects.  
Choices choicesEurope = new Choices(new string[] { "England", "France", "Germany", "Italy" });  
GrammarBuilder europe = new GrammarBuilder(choicesEurope);  

Choices choicesSAmerica = new Choices(new string[] { "Argentina", "Brazil", "Uruguay" });  
GrammarBuilder sAmerica = new GrammarBuilder(choicesSAmerica);  

Choices worldCupWinnerChoices = new Choices(new GrammarBuilder[] {choicesEurope, choicesSAmerica});  

// Create new GrammarBuilder from a Choices object.  
builder = new GrammarBuilder(worldCupWinnerChoices);  

// Create an SrgsDocument object from a GrammarBuilder object.  
SrgsDocument document = new SrgsDocument(builder);  

適用於

SrgsDocument(SrgsRule)

初始化 SrgsDocument 類別的新執行個體,並指定要成為文法根規則的 SrgsRule 物件。

public:
 SrgsDocument(System::Speech::Recognition::SrgsGrammar::SrgsRule ^ grammarRootRule);
public SrgsDocument (System.Speech.Recognition.SrgsGrammar.SrgsRule grammarRootRule);
new System.Speech.Recognition.SrgsGrammar.SrgsDocument : System.Speech.Recognition.SrgsGrammar.SrgsRule -> System.Speech.Recognition.SrgsGrammar.SrgsDocument
Public Sub New (grammarRootRule As SrgsRule)

參數

grammarRootRule
SrgsRule

SrgsDocument 物件中的 root rule

例外狀況

grammarRootRulenull

範例

下列範例會建立兩個規則 chooseCities , (和 destCities) 選擇航班的來源和目的地城市。 此範例會 SrgsDocument 使用 chooseCities 規則做為引數來初始化實例。 此範例會將規則集合的內容和根規則的名稱寫入主控台。

// Create a rule that contains a list of destination cities.  
SrgsRule destCities = new SrgsRule("Destination");  
SrgsOneOf toCities = new SrgsOneOf(new string[] { "New York", "Seattle", "Denver" });  
destCities.Add(toCities);  

// Create a list of origin cities and supporting phrases.  
SrgsOneOf fromCities = new SrgsOneOf(new SrgsItem[] {   
  new SrgsItem("Dallas"), new SrgsItem("Miami"), new SrgsItem("Chicago") });  
SrgsItem intro = new SrgsItem("I want to fly from");  
SrgsItem to = new SrgsItem("to");  

// Create the root rule of the grammar, and assemble the components.  
SrgsRule chooseCities = new SrgsRule("Trip");  
chooseCities.Add(intro);  
chooseCities.Add(fromCities);  
chooseCities.Add(to);  
chooseCities.Add(new SrgsRuleRef(destCities));  

// Create the SrgsDocument and specify the root rule to add.  
SrgsDocument bookFlight = new SrgsDocument(chooseCities);  

// Add the rule for the destination cities to the document's rule collection.  
bookFlight.Rules.Add(new SrgsRule[] { destCities });  

// Display the contents of the Rules collection and the name of the root rule.  
foreach (SrgsRule rule in bookFlight.Rules)  
{  
  Console.WriteLine("Rule " + rule.Id + " is in the rules collection");  
}  
Console.WriteLine("Root Rule " + bookFlight.Root.Id);  

// Create a Grammar object and load it to the recognizer.  
Grammar g = new Grammar(bookFlight);  
g.Name = ("City Chooser");  
recognizer.LoadGrammarAsync(g);  

備註

這個函式會將指定的規則加入至物件的, SrgsRulesCollection SrgsDocument 並將它設定為 Root 文法的規則。

適用於

SrgsDocument(String)

初始化 SrgsDocument 類別的新執行個體,並指定用來填入 SrgsDocument 執行個體之 XML 文件的位置。

public:
 SrgsDocument(System::String ^ path);
public SrgsDocument (string path);
new System.Speech.Recognition.SrgsGrammar.SrgsDocument : string -> System.Speech.Recognition.SrgsGrammar.SrgsDocument
Public Sub New (path As String)

參數

path
String

SRGS XML 檔的位置。

例外狀況

pathnull

path 為空字串。

範例

下列範例會 SrgsDocument 從名為 "srgsDocumentFile.xml" 的檔案建立新的。

string srgsDocumentFile = Path.Combine(Path.GetTempPath(), "srgsDocumentFile.xml");  
SrgsDocument document = null;  

if (File.Exists(srgsDocumentFile))  
   document = new SrgsDocument(srgsDocumentFile);  

適用於

SrgsDocument(XmlReader)

從參考 XML 格式文法檔案的 SrgsDocument 執行個體,初始化 XmlReader 類別的新執行個體。

public:
 SrgsDocument(System::Xml::XmlReader ^ srgsGrammar);
public SrgsDocument (System.Xml.XmlReader srgsGrammar);
new System.Speech.Recognition.SrgsGrammar.SrgsDocument : System.Xml.XmlReader -> System.Speech.Recognition.SrgsGrammar.SrgsDocument
Public Sub New (srgsGrammar As XmlReader)

參數

srgsGrammar
XmlReader

透過 XmlReader XML 執行個體所建立的 SrgsDocument 物件。

例外狀況

srgsGrammarnull

範例

下列範例 SrgsDocument XmlReader 會從參考檔案 "srgsDocumentFile.xml" 的實例,建立的新實例。

string srgsDocumentFile = Path.Combine(Path.GetTempPath(), "srgsDocumentFile.xml");  
SrgsDocument document = null;  

if (File.Exists(srgsDocumentFile))  
{  
  XmlReader reader = XmlReader.Create(srgsDocumentFile);  
  document = new SrgsDocument(reader);  
  reader.Close();  
}  

適用於