Share via


SrgsDocument Costruttori

Definizione

Inizializza una nuova istanza della classe SrgsDocument.

Overload

SrgsDocument()

Inizializza una nuova istanza della classe SrgsDocument.

SrgsDocument(GrammarBuilder)

Inizializza una nuova istanza della classe SrgsDocument da un oggetto GrammarBuilder.

SrgsDocument(SrgsRule)

Inizializza una nuova istanza della classe SrgsDocument e specifica un oggetto SrgsRule affinché sia la regola radice della grammatica.

SrgsDocument(String)

Inizializza una nuova istanza della classe SrgsDocument che specifica il percorso del documento XML utilizzato per compilare l'istanza SrgsDocument.

SrgsDocument(XmlReader)

Inizializza una nuova istanza della classe SrgsDocument da un'istanza dell'oggetto XmlReader che fa riferimento a un file della grammatica in formato XML.

Commenti

Usando i costruttori per la SrgsDocument classe, è possibile creare un'istanza di SrgsDocument da un GrammarBuilderoggetto , SrgsRuleo XmlReader da una stringa contenente il percorso di una grammatica in formato XML oppure è possibile avviare un'istanza vuota di SrgsDocument.

SrgsDocument()

Origine:
SrgsDocument.cs
Origine:
SrgsDocument.cs
Origine:
SrgsDocument.cs

Inizializza una nuova istanza della classe SrgsDocument.

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

Esempio

Nell'esempio seguente viene creato un SrgsDocument oggetto e quindi viene creata una regola pubblica denominata winnerRule. Crea quindi un oggetto SrgsItem costituito dalla stringa "Una nazione che ha vinto la Coppa del Mondo è:" e aggiunge questo elemento alla Elements proprietà della regola. Nell'esempio vengono quindi create altre due regole (ruleEurope e ruleSAmerica), ognuna delle quali è un SrgsOneOf oggetto che contiene tre SrgsItem oggetti . Successivamente, viene creato un altro SrgsOneOf oggetto che contiene SrgsRuleRef oggetti che fanno riferimento a ruleEurope e ruleSAmerica. Il nuovo SrgsOneOf oggetto viene quindi aggiunto alla Elements proprietà di winnerRule. Successivamente, tutte e tre le regole (, e ) vengono aggiunte alla Rules proprietà di SrgsDocument.ruleSAmericaruleEuropewinnerRule Infine, le tre regole vengono compilate in una rappresentazione binaria della grammatica.

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

Commenti

Questo costruttore crea un'istanza vuota SrgsDocument . Per compilare una grammatica all'interno di un'istanza vuota SrgsDocument , aggiungere istanze di classi che rappresentano elementi SRGS, ad esempio SrgsRule, SrgsRuleRef, SrgsOneOfe SrgsItem.

Si applica a

SrgsDocument(GrammarBuilder)

Origine:
SrgsDocument.cs
Origine:
SrgsDocument.cs
Origine:
SrgsDocument.cs

Inizializza una nuova istanza della classe SrgsDocument da un oggetto 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)

Parametri

builder
GrammarBuilder

Oggetto GrammarBuilder utilizzato per creare l'istanza SrgsDocument.

Eccezioni

builder è null.

Esempio

Nell'esempio seguente viene compilata una grammatica in un'istanza GrammarBuilder usando Choices oggetti . Crea quindi un SrgsDocument oggetto dall'oggetto GrammarBuilder .

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

Si applica a

SrgsDocument(SrgsRule)

Origine:
SrgsDocument.cs
Origine:
SrgsDocument.cs
Origine:
SrgsDocument.cs

Inizializza una nuova istanza della classe SrgsDocument e specifica un oggetto SrgsRule affinché sia la regola radice della grammatica.

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)

Parametri

grammarRootRule
SrgsRule

L'oggetto root rule nell'oggetto SrgsDocument.

Eccezioni

grammarRootRule è null.

Esempio

Nell'esempio seguente vengono create due regole (chooseCities e destCities) per la scelta delle città di origine e di destinazione per un volo. Nell'esempio viene inizializzata l'istanza SrgsDocument utilizzando la chooseCities regola come argomento. Nell'esempio vengono scritti i contenuti della raccolta regole e il nome della regola radice nella 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);

Commenti

Questo costruttore aggiunge la regola specificata all'oggetto SrgsRulesCollection dell'oggetto SrgsDocument e la imposta come Root regola per la grammatica.

Si applica a

SrgsDocument(String)

Origine:
SrgsDocument.cs
Origine:
SrgsDocument.cs
Origine:
SrgsDocument.cs

Inizializza una nuova istanza della classe SrgsDocument che specifica il percorso del documento XML utilizzato per compilare l'istanza SrgsDocument.

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)

Parametri

path
String

Percorso del file SRGS XML.

Eccezioni

path è null.

Il parametro path è una stringa vuota.

Esempio

Nell'esempio seguente viene creato un nuovo SrgsDocument dal file denominato "srgsDocumentFile.xml".

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

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

Si applica a

SrgsDocument(XmlReader)

Origine:
SrgsDocument.cs
Origine:
SrgsDocument.cs
Origine:
SrgsDocument.cs

Inizializza una nuova istanza della classe SrgsDocument da un'istanza dell'oggetto XmlReader che fa riferimento a un file della grammatica in formato XML.

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)

Parametri

srgsGrammar
XmlReader

L'oggetto XmlReader creato con l'istanza XML SrgsDocument.

Eccezioni

srgsGrammar è null.

Esempio

Nell'esempio seguente viene creata una nuova istanza di SrgsDocument da un'istanza di XmlReader che fa riferimento al 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();
}

Si applica a