public abstract class Grammar extends java.lang.Object implements Configurable, GrammarInterface
GrammarNodes
linked together by GrammarArcs
. Calling getInitialNode
will return the first node of the grammar graph. To traverse the grammar graph, one should call
GrammarNode.getSuccessors, which will return an array of GrammarArcs, from which you can reach the neighboring
GrammarNodes.
Note that all grammar probabilities are maintained in LogMath log domain.
Modifier and Type | Field and Description |
---|---|
protected Dictionary |
dictionary |
protected GrammarNode |
initialNode |
protected java.util.logging.Logger |
logger |
static java.lang.String |
PROP_ADD_FILLER_WORDS
Property to control whether filler words are inserted into the graph
|
static java.lang.String |
PROP_ADD_SIL_WORDS
Property to control whether silence words are inserted into the graph
|
static java.lang.String |
PROP_DICTIONARY
Property that defines the dictionary to use for this grammar
|
static java.lang.String |
PROP_OPTIMIZE_GRAMMAR
The default value for PROP_SHOW_GRAMMAR.
|
static java.lang.String |
PROP_SHOW_GRAMMAR
Property to control the the dumping of the grammar
|
Constructor and Description |
---|
Grammar() |
Grammar(boolean showGrammar,
boolean optimizeGrammar,
boolean addSilenceWords,
boolean addFillerWords,
Dictionary dictionary) |
Modifier and Type | Method and Description |
---|---|
void |
allocate()
Create the grammar
|
protected abstract GrammarNode |
createGrammar()
Creates a grammar.
|
protected GrammarNode |
createGrammar(java.lang.String bogusText)
Create class from reference text (not implemented).
|
protected GrammarNode |
createGrammarNode(boolean isFinal)
Creates an empty grammar node in this grammar.
|
protected GrammarNode |
createGrammarNode(int identity,
boolean isFinal)
Creates a grammar node in this grammar with the given identity
|
protected GrammarNode |
createGrammarNode(int identity,
java.lang.String word)
Returns a new GrammarNode with the given single word.
|
protected GrammarNode |
createGrammarNode(int identity,
java.lang.String[][] alts)
Returns a new GrammarNode with the given set of alternatives.
|
protected GrammarNode |
createGrammarNode(java.lang.String word)
Returns a new GrammarNode with the given single word.
|
void |
deallocate()
Deallocate resources allocated to this grammar
|
void |
dumpGrammar(java.lang.String name)
Dumps the grammar
|
void |
dumpRandomSentences(int count)
Dump a set of random sentences that fit this grammar
|
void |
dumpRandomSentences(java.lang.String path,
int count)
Dump a set of random sentences that fit this grammar
|
void |
dumpStatistics()
Dumps statistics for this grammar
|
Dictionary |
getDictionary()
Gets the dictionary for this grammar
|
java.util.Set<GrammarNode> |
getGrammarNodes()
returns the set of of nodes in this grammar
|
GrammarNode |
getInitialNode()
Returns the initial node for the grammar
|
int |
getNumNodes()
returns the number of nodes in this grammar
|
java.lang.String |
getRandomSentence()
Returns a random sentence that fits this grammar
|
protected void |
newGrammar()
Prepare to create a new grammar
|
void |
newProperties(PropertySheet ps)
This method is called when this configurable component needs to be reconfigured.
|
protected void |
postProcessGrammar()
Perform the standard set of grammar post processing.
|
@S4Boolean(defaultValue=false) public static final java.lang.String PROP_SHOW_GRAMMAR
@S4Boolean(defaultValue=true) public static final java.lang.String PROP_OPTIMIZE_GRAMMAR
@S4Boolean(defaultValue=false) public static final java.lang.String PROP_ADD_SIL_WORDS
@S4Boolean(defaultValue=false) public static final java.lang.String PROP_ADD_FILLER_WORDS
@S4Component(type=Dictionary.class) public static final java.lang.String PROP_DICTIONARY
protected java.util.logging.Logger logger
protected Dictionary dictionary
protected GrammarNode initialNode
public Grammar(boolean showGrammar, boolean optimizeGrammar, boolean addSilenceWords, boolean addFillerWords, Dictionary dictionary)
public Grammar()
public void newProperties(PropertySheet ps) throws PropertyException
Configurable
newProperties
in interface Configurable
ps
- a property sheet holding the new dataPropertyException
- if there is a problem with the properties.public void allocate() throws java.io.IOException
java.io.IOException
- if IO went wrongpublic void deallocate()
public GrammarNode getInitialNode()
getInitialNode
in interface GrammarInterface
protected void postProcessGrammar()
public void dumpStatistics()
public void dumpRandomSentences(java.lang.String path, int count)
path
- the name of the file to dump the sentences tocount
- dumps no more than this. May dump less than this depending upon the number of uniqe sentences in the
grammar.public void dumpRandomSentences(int count)
count
- dumps no more than this. May dump less than this depending upon the number of uniqe sentences in the
grammar.public java.lang.String getRandomSentence()
public void dumpGrammar(java.lang.String name)
name
- name of the file to dump topublic int getNumNodes()
public java.util.Set<GrammarNode> getGrammarNodes()
getGrammarNodes
in interface GrammarInterface
protected void newGrammar()
protected abstract GrammarNode createGrammar() throws java.io.IOException
java.io.IOException
- if the grammar could not be loadedprotected GrammarNode createGrammar(java.lang.String bogusText) throws java.lang.NoSuchMethodException
bogusText
- dummy variablejava.lang.NoSuchMethodException
- if called with reference sentencepublic Dictionary getDictionary()
protected GrammarNode createGrammarNode(int identity, java.lang.String[][] alts)
identity
- the id for this nodealts
- the set of alternative word lists for this GrammarNodeprotected GrammarNode createGrammarNode(java.lang.String word)
word
- the word for this grammar nodeprotected GrammarNode createGrammarNode(boolean isFinal)
isFinal
- if true, this is a final nodeprotected GrammarNode createGrammarNode(int identity, java.lang.String word)
identity
- the id for this nodeword
- the word for this grammar nodeprotected GrammarNode createGrammarNode(int identity, boolean isFinal)
identity
- the identity of the nodeisFinal
- if true, this is a final node