edu.cmu.sphinx.jsapi
Class JSGFGrammar

java.lang.Object
  extended by edu.cmu.sphinx.linguist.language.grammar.Grammar
      extended by edu.cmu.sphinx.jsapi.JSGFGrammar
All Implemented Interfaces:
GrammarInterface, Configurable

public class JSGFGrammar
extends Grammar

Defines a BNF-style grammar based on JSGF grammar rules in a file.

The Java Speech Grammar Format (JSGF) is a BNF-style, platform-independent, and vendor-independent textual representation of grammars for use in speech recognition. It is used by the Java Speech API (JSAPI) .

Here we only intend to give a couple of examples of grammars written in JSGF, so that you can quickly learn to write your own grammars. For more examples and a complete specification of JSGF, go to

http://java.sun.com/products/java-media/speech/forDevelopers/JSGF/ .

Example 1: "Hello World" in JSGF

The example below shows how a JSGF grammar that generates the sentences "Hello World":

  #JSGF V1.0
  
  public <helloWorld> = Hello World;
 
Figure 1: Hello grammar that generates the sentences "Hello World".

The above grammar is saved in a file called "hello.gram". It defines a public grammar rule called "helloWorld". In order for this grammar rule to be publicly accessible, we must be declared it "public". Non-public grammar rules are not visible outside of the grammar file.

The location of the grammar file(s) is(are) defined by the baseGrammarURLproperty. Since all JSGF grammar files end with ".gram", it will automatically search all such files at the given URL for the grammar. The name of the grammar to search for is specified by grammarName. In this example, the grammar name is "helloWorld".

Example 2: Command Grammar in JSGF

This examples shows a grammar that generates basic control commands like "move a menu thanks please", "close file", "oh mighty computer please kindly delete menu thanks". It is the same as one of the command & control examples in the JSGF specification . It is considerably more complex than the previous example. It defines the public grammar called "basicCmd".

  #JSGF V1.0
  
  public <basicCmd> = <startPolite> <command> <endPolite>;
  
  <command> = <action> <object>;
  <action> = /10/ open |/2/ close |/1/ delete |/1/ move;
  <object> = [the | a] (window | file | menu);
  
  <startPolite> = (please | kindly | could you | oh mighty computer) *;
  <endPolite> = [ please | thanks | thank you ];
 
Figure 2: Command grammar that generates simple control commands.

The features of JSGF that are shown in this example includes:

From JSGF to Grammar Graph

After the JSGF grammar is read in, it is converted to a graph of words representing the grammar. Lets call this the grammar graph. It is from this grammar graph that the eventual search structure used for speech recognition is built. Below, we show the grammar graphs created from the above JSGF grammars. The nodes "<sil>" means "silence".


Figure 3: Grammar graph created from the Hello World grammar.


Figure 4: Grammar graph created from the Command grammar.

Limitations

There is a known limitation with the current JSGF support. Grammars that contain non-speech loops currently cause the recognizer to hang.

For example, in the following grammar

  #JSGF V1.0
  grammar jsgf.nastygram;
  public  = I saw a ((cat* | dog* | mouse*)+)+;
 
the production: ((cat* | dog* | mouse*)+)+ can result in a continuous loop, since (cat* | dog* | mouse*) can represent no speech (i.e. zero cats, dogs and mice), this is equivalent to ()+. To avoid this problem, the grammar writer should ensure that there are no rules that could possibly match no speech within a plus operator or kleene star operator.

Dynamic grammar behavior

It is possible to modify the grammar of a running application. Some rules and notes:

Implementation Notes

  1. All internal probabilities are maintained in LogMath log base.


Field Summary
static java.lang.String PROP_BASE_GRAMMAR_URL
          Sphinx property that defines the location of the JSGF grammar file.
static java.lang.String PROP_GRAMMAR_NAME
          Sphinx property that defines the location of the JSGF grammar file.
static java.lang.String PROP_GRAMMAR_NAME_DEFAULT
          Default value for PROP_GRAMMAR_NAME
static java.lang.String PROP_LOG_MATH
          Sphinx property that defines the logMath component.
 
Fields inherited from class edu.cmu.sphinx.linguist.language.grammar.Grammar
PROP_ADD_FILLER_WORDS, PROP_ADD_FILLER_WORDS_DEFAULT, PROP_ADD_SIL_WORDS, PROP_ADD_SIL_WORDS_DEFAULT, PROP_DICTIONARY, PROP_OPTIMIZE_GRAMMAR, PROP_OPTIMIZE_GRAMMAR_DEFAULT, PROP_SHOW_GRAMMAR, PROP_SHOW_GRAMMAR_DEFAULT
 
Constructor Summary
JSGFGrammar()
           
 
Method Summary
 void commitChanges()
          Commit changes to all loaded grammars and all changes of grammar since the last commitChange
 GrammarNode getInitialNode()
          Returns the initial node for the grammar
 RuleGrammar getRuleGrammar()
          Returns the RuleGrammar of this JSGFGrammar.
 void loadJSGF(java.lang.String grammarName)
          The JSGF grammar specified by grammarName will be loaded from the base url (tossing out any previously loaded grammars)
 void newProperties(PropertySheet ps)
          This method is called when this configurable component has new data.
 void register(java.lang.String name, Registry registry)
          Register my properties.
 void setBaseURL(java.net.URL url)
          Sets the URL context of the JSGF grammars.
 
Methods inherited from class edu.cmu.sphinx.linguist.language.grammar.Grammar
allocate, deallocate, dumpGrammar, dumpRandomSentences, dumpRandomSentences, dumpStatistics, getDictionary, getGrammarNodes, getName, getNumNodes, getRandomSentence
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROP_BASE_GRAMMAR_URL

public static final java.lang.String PROP_BASE_GRAMMAR_URL
Sphinx property that defines the location of the JSGF grammar file.

See Also:
Constant Field Values

PROP_GRAMMAR_NAME

public static final java.lang.String PROP_GRAMMAR_NAME
Sphinx property that defines the location of the JSGF grammar file.

See Also:
Constant Field Values

PROP_GRAMMAR_NAME_DEFAULT

public static final java.lang.String PROP_GRAMMAR_NAME_DEFAULT
Default value for PROP_GRAMMAR_NAME

See Also:
Constant Field Values

PROP_LOG_MATH

public static final java.lang.String PROP_LOG_MATH
Sphinx property that defines the logMath component.

See Also:
Constant Field Values
Constructor Detail

JSGFGrammar

public JSGFGrammar()
Method Detail

register

public void register(java.lang.String name,
                     Registry registry)
              throws PropertyException
Description copied from interface: Configurable
Register my properties. This method is called once early in the time of the component, shortly after the component is constructed. This component should register any configuration properties that it needs to register. If this configurable extends another configurable, super.register should also be called

Specified by:
register in interface Configurable
Overrides:
register in class Grammar
Parameters:
name - the name of the component
registry - the registry for this component
Throws:
PropertyException

newProperties

public void newProperties(PropertySheet ps)
                   throws PropertyException
Description copied from interface: Configurable
This method is called when this configurable component has new data. The component should first validate the data. If it is bad the component should return false. If the data is good, the component should record the the data internally and return true.

Specified by:
newProperties in interface Configurable
Overrides:
newProperties in class Grammar
Parameters:
ps - a property sheet holding the new data
Throws:
PropertyException - if there is a problem with the properties.

getRuleGrammar

public RuleGrammar getRuleGrammar()
Returns the RuleGrammar of this JSGFGrammar.

Returns:
the RuleGrammar

setBaseURL

public void setBaseURL(java.net.URL url)
Sets the URL context of the JSGF grammars.

Parameters:
url - the URL context of the grammars

loadJSGF

public void loadJSGF(java.lang.String grammarName)
              throws java.io.IOException
The JSGF grammar specified by grammarName will be loaded from the base url (tossing out any previously loaded grammars)

Parameters:
grammarName - the name of the grammar
Throws:
java.io.IOException - if an error occurs while loading or compiling the grammar

getInitialNode

public GrammarNode getInitialNode()
Returns the initial node for the grammar

Specified by:
getInitialNode in interface GrammarInterface
Overrides:
getInitialNode in class Grammar
Returns:
the initial grammar node

commitChanges

public void commitChanges()
                   throws java.io.IOException
Commit changes to all loaded grammars and all changes of grammar since the last commitChange

Throws:
java.io.IOException