How Tos

Last reviewed: 7/15/2011

Article ID: H071129

HOW: Enumerating SAPI grammar rules and phrase properties

The information in this article applies to:

  • SpeechKit 7

Summary

SpeechKit 7 provides support for enumerating SAPI grammar rule and phrase properties on recognition results.

More Information

For most applications, processing the spoken result on grammar recognition events is adequate.

For more complicated grammar and processing scenarios however, it may be desirable to process the rule and phrase properties within the recognition results.

SpeechKit 7 provides 2 new speech resources: CSRPhraseProperty and CSRRule that can be enumerated on recognition events.

The following example illustrates enumerating phrase properties.

private void NChantSR1_HasEvent(object sender, SpeechKit.HasEventArgs e)
{
// Get the number of events
int numberOfEvents = NChantSR1.GetResourceCount(ChantSpeechResource.CSREvent,0,0);
for (int i = 0; i < numberOfEvents; i++)
{
    // Get the event from the event queue
    NChantSREvent nChantSREvent = NChantSR1.GetChantSREvent(0);

    // Process the type of callback event
    switch (nChantSREvent.ChantCallback)
    {
        case (int)ChantCallback.CCSRHasPhrase :
            // Get the number of phrase properties
            int numberOfPhraseProperties = nChantSREvent.GetResourceCount(ChantSpeechResource.CSRPhraseProperty);
            for (int j = 0; j < numberOfPhraseProperties; j++)
            {
                NChantPhraseProperty nChantPhraseProperty = nChantSREvent.GetChantPhraseProperty(j);
                // Phrase properties are a tree structure
                ProcessPhraseProperty(nChantPhraseProperty);                   
            }
            break;
        default :
            break;
    }
    // Remove the event from the event queue
    NChantSR1.RemoveResource(ChantSpeechResource.CSREvent, 0, "");
}
}
private void ProcessPhraseProperty(NChantPhraseProperty nChantPhraseProperty)
{
// Get the number of phrase properties
int numberOfPhraseProperties = nChantPhraseProperty.GetResourceCount(ChantSpeechResource.CSRPhraseProperty);
for (int j = 0; j < numberOfPhraseProperties; j++)
{
    NChantPhraseProperty nChildChantPhraseProperty = nChantPhraseProperty.GetChantPhraseProperty(j);
    // Phrase properties are a tree structure
    ProcessPhraseProperty(nChildChantPhraseProperty);                   
}
// Process property values
int id = nChantPhraseProperty.ID;
string name = nChantPhraseProperty.Name;
}

The following example illustrates enumerating rules.

private void NChantSR1_HasEvent(object sender, SpeechKit.HasEventArgs e)
{
// Get the number of events
int numberOfEvents = NChantSR1.GetResourceCount(ChantSpeechResource.CSREvent,0,0);
for (int i = 0; i < numberOfEvents; i++)
{
    // Get the event from the event queue
    NChantSREvent nChantSREvent = NChantSR1.GetChantSREvent(0);

    // Process the type of callback event
    switch (nChantSREvent.ChantCallback)
    {
        case (int)ChantCallback.CCSRHasPhrase :
            // Get the number of phrase properties
            int numberOfRules = nChantSREvent.GetResourceCount(ChantSpeechResource.CSRRule);
            for (int j = 0; j < numberOfRules; j++)
            {
                NChantRule nChantRule = nChantSREvent.GetChantRule(j);
                // Rules are a tree structure
                ProcessRule(nChantRule);                   
            }
            break;
        default :
            break;
    }
    // Remove the event from the event queue
    NChantSR1.RemoveResource(ChantSpeechResource.CSREvent, 0, "");
}
}
private void ProcessRule(NChantRule nChantRule)
{
// Get the number of phrase properties
int numberOfRules = nChantRule.GetResourceCount(ChantSpeechResource.CSRRule);
for (int j = 0; j < numberOfRules; j++)
{
    NChantRule nChildChantRule = nChantRule.GetChantRule(j);
    // Rules are a tree structure
    ProcessRule(nChildChantRule);                   
}
// Process property values
int id = nChantRule.ID;
string name = nChantRule.Name;
}