Share via


SrgsRule Constructor (String, SrgsElement[])

Initializes a new instance of the SrgsRule class, creating a rule in an SRGS grammar that is populated with the elements of the passed array.

Namespace: Microsoft.Speech.Recognition.SrgsGrammar
Assembly: Microsoft.Speech (in microsoft.speech.dll)

Syntax

'Declaration

Parameters

  • id
    A string object that is the identifier of the rule. The identifier must be unique within a given grammar.
  • elements
    An array of SrgsElement elements.

Exceptions

Exception type Condition
ArgumentNullException

id is null.

elements is null.

ArgumentOutOfRangeException

id is empty.

FormatException

id is not a proper rule identifier.

Remarks

The SrgsRule(string, SrgsElement[]) constructor initializes the Id property.

The SrgsRule(string, SrgsElement[]) constructor throws FormatException in the following circumstances:

  • id is not a valid XML name, as defined in Extensible Markup Language (XML) 1.0 (Second Edition)—see http://www.w3.org/TR/2000/REC-xml-20001006. To paraphrase this definition, a valid XML name must begin with a letter, an underscore ('_'), or a colon (':') and can be followed by zero or more NameChar characters (also defined in the XML specification).

  • id is "NULL" or "VOID" or "GARBAGE".

  • id contains at least one invalid rule ID character. These characters are: '?', '*', '+', '|', '(', '), '^', '$', '/', ';', '.', '=', '<', '>', '[', ']', '{', '}', '\\', ' ', '\t', '\r', and '\n'.

Example

The following example creates a grammar that recognizes the phrase "A nation that has won the World Cup is" followed by the name of a country that has won the World Cup. The example creates a public rule named WorldCupWinner. It then creates two SrgsRule elements using arrays of SrgsOneOf elements and adds them as rule references to WorldCupWinner.

public void WorldSoccerWinners ()
{
  SrgsDocument srgsGrammar = new SrgsDocument ();
  SrgsRule rootRule = new SrgsRule ("WorldCupWinner");

  rootRule.Scope = SrgsRuleScope.Public;

  // Add the introduction
  rootRule.Elements.Add (new SrgsItem ("A nation that has won the world cup is"));

  // Create the rootRule for the European nations
  SrgsOneOf oneOfEurope = new SrgsOneOf (new SrgsItem ("England"), new SrgsItem ("France"), new SrgsItem ("Germany"), new SrgsItem ("Italy"));
  SrgsRule ruleEurope = new SrgsRule ("EuropeanNations", oneOfEurope);

  // Create the rootRule for the South American nations
  SrgsOneOf oneOfSAmerica = new SrgsOneOf (new SrgsItem ("Argentina"), new SrgsItem ("Brazil"), new SrgsItem ("Uruguay"));
  SrgsRule ruleSAmerica = new SrgsRule ("SouthAmericanNations", oneOfSAmerica);

  // Add both rules to root list through a 'one of'
  rootRule.Elements.Add (new SrgsOneOf (new SrgsItem (new SrgsRuleRef (ruleEurope)), new SrgsItem (new SrgsRuleRef (ruleSAmerica))));

  // Add all the rules to the grammar
  srgsGrammar.Rules.Add (rootRule, ruleEurope, ruleSAmerica);
  srgsGrammar.Root = rootRule;
}

The created grammar has the following form.

<grammar version="1.0" xml:lang="en-US" xmlns="http://www.w3.org/2001/06/grammar" root="WorldCupWinner">
    <rule id="WorldCupWinner" scope="public">
        <item>A nation that has won the world cup is</item>
        <one-of>
            <item>
                <ruleref uri="#EuropeanNations" />
            </item>
            <item>
                <ruleref uri="#SouthAmericanNations" />
            </item>
        </one-of>
    </rule>
    <rule id="EuropeanNations">
        <one-of>
            <item>England</item>
            <item>France</item>
            <item>Germany</item>
            <item>Italy</item>
        </one-of>
    </rule>
    <rule id="SouthAmericanNations">
        <one-of>
            <item>Argentina</item>
            <item>Brazil</item>
            <item>Uruguay</item>
        </one-of>
    </rule>
</grammar>

Platforms

Development Platforms

Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Vista Ultimate Edition, Windows Vista Business Edition, Windows Vista Enterprise Edition

Target Platforms

See Also

Reference

SrgsRule Class
SrgsRule Members
Microsoft.Speech.Recognition.SrgsGrammar Namespace