|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectedu.cmu.sphinx.linguist.language.grammar.Grammar
edu.cmu.sphinx.jsapi.JSGFGrammar
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; |
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
baseGrammarURL
property. 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 ]; |
The features of JSGF that are shown in this example includes:
"<sil>"
means "silence".
Figure 3: Grammar graph created from the Hello World grammar.
Figure 4: Grammar graph created from the Command grammar.
For example, in the following grammar
#JSGF V1.0 grammar jsgf.nastygram; publicthe 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.= I saw a ((cat* | dog* | mouse*)+)+;
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, getGrammarNodes, getName, getNumNodes, getRandomSentence |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final java.lang.String PROP_BASE_GRAMMAR_URL
public static final java.lang.String PROP_GRAMMAR_NAME
public static final java.lang.String PROP_GRAMMAR_NAME_DEFAULT
public static final java.lang.String PROP_LOG_MATH
Constructor Detail |
public JSGFGrammar()
Method Detail |
public void register(java.lang.String name, Registry registry) throws PropertyException
Configurable
register
in interface Configurable
register
in class Grammar
PropertyException
public void newProperties(PropertySheet ps) throws PropertyException
Configurable
newProperties
in interface Configurable
newProperties
in class Grammar
PropertyException
public RuleGrammar getRuleGrammar()
public void setBaseURL(java.net.URL url)
url
- the URL context of the grammarspublic void loadJSGF(java.lang.String grammarName) throws java.io.IOException
grammarName
- the name of the grammar
java.io.IOException
- if an error occurs while loading or
compiling the grammarpublic GrammarNode getInitialNode()
getInitialNode
in class Grammar
public void commitChanges() throws java.io.IOException
java.io.IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |