Delen via


SrgsDocument Constructors

Definition

Initializes a new instance of the SrgsDocument class.

Overloads

SrgsDocument()

Initializes a new instance of the SrgsDocument class.

SrgsDocument(GrammarBuilder)

Initializes a new instance of the SrgsDocument class from a GrammarBuilder object.

SrgsDocument(SrgsRule)

Initializes a new instance of the SrgsDocument class and specifies an SrgsRule object to be the root rule of the grammar.

SrgsDocument(String)

Initializes a new instance of the SrgsDocument class specifying the location of the XML document that is used to fill in the SrgsDocument instance.

SrgsDocument(XmlReader)

Initializes a new instance of the SrgsDocument class from an instance of XmlReader that references an XML-format grammar file.

Remarks

Using the constructors for the SrgsDocument class, you can create an instance of SrgsDocument from a GrammarBuilder, SrgsRule, or XmlReader object, from a string that contains the path to an XML-format grammar, or you can initiate a blank instance of SrgsDocument.

SrgsDocument()

Source:
SrgsDocument.cs
Source:
SrgsDocument.cs
Source:
SrgsDocument.cs

Initializes a new instance of the SrgsDocument class.

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

Examples

The following example creates an SrgsDocument object and then creates a public rule named winnerRule. It then creates an SrgsItem that consists of the string "A nation that has won the World Cup is:", and adds this item to the Elements property of the rule. The example then creates two more rules (ruleEurope and ruleSAmerica), each of which is an SrgsOneOf object that contains three SrgsItem objects. After that, another SrgsOneOf object is created that contains SrgsRuleRef objects that refer to ruleEurope and ruleSAmerica. The new SrgsOneOf object is then added to the Elements property of winnerRule. After this, all three rules (winnerRule, ruleEurope, and ruleSAmerica) are added to the Rules property of SrgsDocument. Finally, the three rules are compiled into a binary representation of the grammar.

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);
  }
}

Remarks

This constructor creates an empty SrgsDocument instance. To build a grammar within an empty SrgsDocument instance, add instances of classes that represent SRGS elements, such as SrgsRule, SrgsRuleRef, SrgsOneOf, and SrgsItem.

Applies to

SrgsDocument(GrammarBuilder)

Source:
SrgsDocument.cs
Source:
SrgsDocument.cs
Source:
SrgsDocument.cs

Initializes a new instance of the SrgsDocument class from a GrammarBuilder object.

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)

Parameters

builder
GrammarBuilder

The GrammarBuilder object used to create the SrgsDocument instance.

Exceptions

builder is null.

Examples

The following example builds a grammar in a GrammarBuilder instance using Choices objects. It then creates an SrgsDocument from the GrammarBuilder object.

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);

Applies to

SrgsDocument(SrgsRule)

Source:
SrgsDocument.cs
Source:
SrgsDocument.cs
Source:
SrgsDocument.cs

Initializes a new instance of the SrgsDocument class and specifies an SrgsRule object to be the root rule of the grammar.

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)

Parameters

grammarRootRule
SrgsRule

The root rule in the SrgsDocument object.

Exceptions

grammarRootRule is null.

Examples

The following example creates two rules (chooseCities and destCities) for choosing origin and destination cities for a flight. The example initializes the SrgsDocument instance using the chooseCities rule as an argument. The example writes the contents of the rules collection and the name of the root rule to the console.

// 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);

Remarks

This constructor adds the specified rule to the SrgsRulesCollection of the SrgsDocument object and sets it as the Root rule for the grammar.

Applies to

SrgsDocument(String)

Source:
SrgsDocument.cs
Source:
SrgsDocument.cs
Source:
SrgsDocument.cs

Initializes a new instance of the SrgsDocument class specifying the location of the XML document that is used to fill in the SrgsDocument instance.

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)

Parameters

path
String

The location of the SRGS XML file.

Exceptions

path is null.

path is an empty string.

Examples

The following example creates a new SrgsDocument from the file named "srgsDocumentFile.xml".

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

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

Applies to

SrgsDocument(XmlReader)

Source:
SrgsDocument.cs
Source:
SrgsDocument.cs
Source:
SrgsDocument.cs

Initializes a new instance of the SrgsDocument class from an instance of XmlReader that references an XML-format grammar file.

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)

Parameters

srgsGrammar
XmlReader

The XmlReader object that was created with the SrgsDocument XML instance.

Exceptions

srgsGrammar is null.

Examples

The following example creates a new instance of SrgsDocument from an instance of XmlReader that references the file "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();
}

Applies to