Working with the Grammar

Creating a Grammar

A grammar is made up of concepts that in turn are made of phrases.   Each concept is a collection of phrases, which denote the same idea.  For example, if ‘yes’ is the concept then ‘yes’, ‘yes please’ and ‘yup’ could all be phrases for ‘yes’.

Every decode requires a grammar.  There are three ways of specifying a grammar: create a grammar from scratch, load a standard grammar,  or load a standard grammar and customize it.

The speech port maintains up to 64 grammar sets.  Each grammar is dynamically defined at runtime. Simply choose which grammar set to use by specifying a number between 0 – 63, inclusive.

To create a custom grammar:

C:    int LV_SRE_AddPhrase (HPORT hport, int GrammarSet, const char* Concept, const char* Phrase);

C++:  int LVSpeechPort::AddPhrase (int GrammarSet, const char* Concept, const char* Phrase);

New concepts are added along side of new phrases, and use the same functions; the port detects when the concept is new and adds it to an internal list, along with the new phrase.  If the concept already exists, the new phrase is added to that concept in the list.  This means each concept must have at least one phrase; at decode time, it is the phrase that is recognized, but the concept that is returned.  Grouping the phrases into concepts allows a more compact, sensible grammar, and more importantly, increases accuracy during recognition.

The following code fragment creates a simple yes/no grammar.  The "no" concept contains the phrases "no" and "nope; the "yes" concept contains the phrases as a BNF representation of "yes", "yes please", and "correct".  It is also possible to add each phrase represented by the BNF individually, or to represent each phrase as a string of phonemes (sounds),  or to mix the three methods (words, BNF, phonemes).  When decoding, the Speech Engine looks for each of the phrases, and returns the concept that contains that phrase.

Code Example:  Creating a Grammar

#include <LVSpeechPort.h>

 

#ifdef _C_

 

//the C way

void SPAddYesNoConcepts(HPORT hport)

{

LV_SRE_AddPhrase(hport, 1, “no”, “no”);

LV_SRE_AddPhrase(hport, 1, “no”, “nope”);

LV_SRE_AddPhrase(hport, 1, “yes”, ”(yes [please]) | correct”);

}

 

#else ifdef _CPLUSPLUS_

 

//the C++ way

void SPAddYesNoConcepts(LVSpeechPort &myPort)

{

myPort.AddPhrase(1, "no", "no");

myport.AddPhrase(1, "no", "nope");

myport.AddPhrase(1, "yes", "(yes [please]) | correct");

}

 

#endif

Removing A Concept

At times the client application may need to remove a concept from a grammar.  Typically this is to resolve ambiguities in recognition or as a repair strategy if the Speech Engine misrecognizes the speech.  For example, "John Brown", "John Crown" and "Mary Frankenstein" are three phrases, each in different concepts.  The speaker says "John Brown" but the Speech Engine picks "John Crown".  The client application may try to confirm the "John Crown" result with the speaker, but the speaker says "no".  The client application can remove "John Crown" since that is a known incorrect answer, leaving only "John Brown" and "Mary Frankenstein".  Now that the similar "John Crown" is no longer in the grammar, the Speech Engine will pick "John Brown", the correct answer.  When the client application removes the concept, the concept and all of its phrases are removed from the grammar.

C:    int LV_SRE_RemoveConcept(HPORT hport,int GrammarSet, const char* Concept);

C++:  int LVSpeechPort::RemoveConcept(int GrammarSet, const char* Concept);

Code Example:  Removing A Concept

#include <LVSpeechPort.h>

 

#ifdef _C_

 

//the C way

void RemoveBadConcept(HPORT hport, const char* badConcept)

{

LV_SRE_RemoveConcept(hport, 1, badConcept);

}

 

#else ifdef _CPLUSPLUS_

 

//the C++ way

void RemoveBadConcept(LVSpeechPort& myPort, const char* badConcept)

{

myPort.RemoveConcept(1, badConcept);

}

 

#endif

Clearing a Grammar

Grammars can also be cleared all at once, allowing the grammar set index to be reused.  If the grammar is cleared, all concepts and phrases are removed.

C:    int LV_SRE_ResetGrammar(HPORT hport,int GrammarSet);

C++:  int LVSpeechPort::ResetGrammar(int GrammarSet);

Code Example:  Clearing a Grammar

#include <LVSpeechPort.h>

 

#ifdef _C_

 

//the C way

void ClearGrammar(HPORT hport, int GrammarSet)

{

LV_SRE_ResetGrammar(hport, GrammarSet);

}

 

#else ifdef _CPLUSPLUS_

 

//the C++ way

void ClearGrammar(LVSpeechPort& myPort, int GrammarSet)

{

myPort.ResetGrammar(GrammarSet);

}

 

#endif

Standard Grammars

There are several predefined grammars available, referred to as standard grammars.  The standard grammars automatically define grammars for regular recognition tasks like strings of single digits, monetary values, dates, etc.  The Speech Engine automatically formats the concepts recognized using the standard grammars into an appropriate single concepts.  For example, if the Speech Engine recognized the 3 concepts "1", "2", "3", the decoder returns "123".  Only one standard grammar can be loaded in each grammar set; to reuse the grammar set the client application must call LV_SRE_ResetGrammar (C API) or LVSpeechPort::ResetGrammar (C++ API).

C:    int LV_SRE_LoadStandardGrammar(HPORT hport, int GrammarSet, int StdGrammar);

C++:  int LVSPeechPort::LoadStandardGrammar(int GrammarSet, int StdGrammar);

Code Example:  Loading a Standard Grammar

#include <LVSpeechPort.h>

 

#ifdef _C_

 

//the C way

void UseStandardGrammar(HPORT hport)

{

LV_SRE_LoadStandardGrammar(hport, 1, GRAMMAR_DIGITS);

}

 

#else ifdef _CPLUSPLUS_

 

//the C++ way

void UseStandardGrammar(LVSpeechPort& myPort, int StdGrammar)

{

myPort.LoadStandardGrammar(1, GRAMMAR_DIGITS);

}

 

#endif

Customizing a Standard Grammar

Finally, a standard grammar can be loaded, and then customized with calls to LV_SRE_AddPhrase (C API) or LVSpeechPort (C++ API).  The Speech Engine will specially format any concepts which match the standard grammar, while leaving the custom concepts unaltered.  Both sets of concepts are returned.  The standard grammar GRAMMAR_DIGITS is a special case; this grammar cannot be customized, because it relies on special models to function.

Code Example:  Customizing a Standard Grammar

#include <LVSpeechPort.h>

 

#ifdef _C_

 

//the C way

void UseAndCustomizeStandardGrammar(HPORT hport)

{

LV_SRE_LoadStandardGrammar(hport, 1, GRAMMAR_NUMBER);

LV_SRE_AddPhrase(hport, 1, "yes", "yup|correct");

LV_SRE_AddPhrase(hport, 1, "no", "(no ([sir]|way))|nope");

}

 

#else ifdef _CPLUSPLUS

 

//the C++ way

void UseAndCustomizeStandardGrammar(LVSpeechPort& myPort)

{

myPort.LoadStandardGrammar(1, GRAMMAR_NUMBER);

myPort.AddPhrase(1, "yes", "yup|correct");

myPort.AddPhrase(1, "no", "(no ([sir]|way))|nope");

}

 

#endif


Complete Help Topic List | Speech Engine Product Information