up
Class Grammar

java.lang.Object
  extended by up.Grammar

public class Grammar
extends java.lang.Object

A probabilistic context-free grammar.

When a rule is added to the grammar, the rule itself as well as all terminal and nonterminal symbols occurring in the rule are assigned a code, a unique, non-negative integer. Two rules or symbols will receive the same code if and only if they are equal with respect to the method equals.


Constructor Summary
Grammar()
          Constructs a new grammar.
 
Method Summary
 BinaryRule addBinaryRule(java.lang.String lhs, java.lang.String rhs1, java.lang.String rhs2)
          Adds the specified binary rule to this grammar.
 PreterminalRule addPreterminalRule(java.lang.String lhs, java.lang.String rhs)
          Adds the specified preterminal rule to this grammar.
 UnaryRule addUnaryRule(java.lang.String lhs, java.lang.String rhs)
          Adds the specified unary rule to this grammar.
 java.util.List<BinaryRule> getBinaryRules()
          Returns the binary rules of this grammar.
 java.util.List<BinaryRule> getBinaryRulesWithLHS(int lhs)
          Returns the binary rules of this grammar with the specified left-hand side.
 java.util.List<BinaryRule> getBinaryRulesWithRHS1(int rhs1)
          Returns the binary rules of this grammar with the specified right-hand side.
 java.util.List<BinaryRule> getBinaryRulesWithRHS2(int rhs2)
          Returns the binary rules of this grammar with the specified right-hand side.
 java.lang.String getCategory(int code)
          Returns the category of this grammar with the specified code.
 int getCodeCategory(java.lang.String category)
          Returns the code of the specified syntactic category.
 int getCodeWord(java.lang.String word)
          Returns the code of the specified word.
 int getNBinaryRules()
          Returns the number of binary rules of this grammar.
 int getNCategories()
          Returns the number of categories of this grammar.
 int getNPreterminalRules()
          Returns the number of preterminal rules of this grammar.
 int getNRules()
          Returns the number of rules of this grammar.
 int getNUnaryRules()
          Returns the number of unary rules of this grammar.
 int getNWords()
          Returns the number of words of this grammar.
 java.util.List<PreterminalRule> getPreterminalRules()
          Returns the preterminal rules of this grammar.
 java.util.List<PreterminalRule> getPreterminalRulesWithLHS(int lhs)
          Returns the preterminal rules of this grammar with the specified left-hand side.
 java.util.List<PreterminalRule> getPreterminalRulesWithRHS(int rhs)
          Returns the preterminal rules of this grammar with the specified right-hand side.
 int getRootCode()
          Returns the code of the root category of this grammar.
 java.util.List<UnaryRule> getUnaryRules()
          Returns the unary rules of this grammar.
 java.util.List<UnaryRule> getUnaryRulesWithLHS(int lhs)
          Returns the unary rules of this grammar with the specified left-hand side.
 java.util.List<UnaryRule> getUnaryRulesWithRHS(int rhs)
          Returns the unary rules of this grammar with the specified right-hand side.
 int getUnknownCode()
          Returns the code of the unknown word.
 java.lang.String getWord(int code)
          Returns the word with the specified code.
 void seal()
          Seals the grammar.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Grammar

public Grammar()
Constructs a new grammar.

Method Detail

getNCategories

public int getNCategories()
Returns the number of categories of this grammar.

Returns:
the number of categories of this grammar

getNWords

public int getNWords()
Returns the number of words of this grammar.

Returns:
the number of words of this grammar

getCodeCategory

public int getCodeCategory(java.lang.String category)
Returns the code of the specified syntactic category.

Parameters:
category - the syntactic category whose code to return
Returns:
the code of the specified category, or -1 in case the specified category is not a category of this grammar

getRootCode

public int getRootCode()
Returns the code of the root category of this grammar.

Returns:
the code of the root category (start symbol) category of this grammar

getCategory

public java.lang.String getCategory(int code)
Returns the category of this grammar with the specified code.

Parameters:
code - the code whose corresponding category to return
Returns:
the category of this grammar with the specified code, * * or null in case this grammar does not have a category with the specified code

getCodeWord

public int getCodeWord(java.lang.String word)
Returns the code of the specified word.

Parameters:
word - the word whose code to return
Returns:
the code of the specified word, or -1 in case the specified word is not a word of this grammar

getUnknownCode

public int getUnknownCode()
Returns the code of the unknown word.

Returns:
the code of the unknown word

getWord

public java.lang.String getWord(int code)
Returns the word with the specified code.

Parameters:
code - the code whose corresponding word to return
Returns:
the word of this grammar with the specified code, * or null in case this grammar does not have a word with the specified code

addPreterminalRule

public PreterminalRule addPreterminalRule(java.lang.String lhs,
                                          java.lang.String rhs)
Adds the specified preterminal rule to this grammar.

Parameters:
lhs - the left-hand side category (POS tag) of the rule
rhs - the right-hand side (word) of the rule
Returns:
the preterminal rule

addUnaryRule

public UnaryRule addUnaryRule(java.lang.String lhs,
                              java.lang.String rhs)
Adds the specified unary rule to this grammar.

Parameters:
lhs - the left-hand side category of the rule
rhs - the right-hand side category of the rule
Returns:
the unary rule

addBinaryRule

public BinaryRule addBinaryRule(java.lang.String lhs,
                                java.lang.String rhs1,
                                java.lang.String rhs2)
Adds the specified binary rule to this grammar.

Parameters:
lhs - the left-hand side category of the rule
rhs1 - the first category on the right-hand side of the rule
rhs2 - the second category on the right-hand side of the rule
Returns:
the binary rule

seal

public void seal()
Seals the grammar. This closes the grammar for the addition of new rules.


getNRules

public int getNRules()
Returns the number of rules of this grammar.

Returns:
the number of rules of this grammar.

getPreterminalRules

public java.util.List<PreterminalRule> getPreterminalRules()
Returns the preterminal rules of this grammar.

Returns:
the preterminal rules of this grammar

getNPreterminalRules

public int getNPreterminalRules()
Returns the number of preterminal rules of this grammar.

Returns:
the number of preterminal rules of this grammar

getPreterminalRulesWithLHS

public java.util.List<PreterminalRule> getPreterminalRulesWithLHS(int lhs)
Returns the preterminal rules of this grammar with the specified left-hand side.

Parameters:
lhs - the code of the left-hand side (category)
Returns:
the preterminal rules of this grammar with the specified left-hand side

getPreterminalRulesWithRHS

public java.util.List<PreterminalRule> getPreterminalRulesWithRHS(int rhs)
Returns the preterminal rules of this grammar with the specified right-hand side.

Parameters:
rhs - the code of the right-hand side (word)
Returns:
the preterminal rules of this grammar with the specified right-hand side

getUnaryRules

public java.util.List<UnaryRule> getUnaryRules()
Returns the unary rules of this grammar.

Returns:
the unary rules of this grammar

getNUnaryRules

public int getNUnaryRules()
Returns the number of unary rules of this grammar.

Returns:
the number of unary rules of this grammar

getUnaryRulesWithLHS

public java.util.List<UnaryRule> getUnaryRulesWithLHS(int lhs)
Returns the unary rules of this grammar with the specified left-hand side.

Parameters:
lhs - the code of the left-hand side
Returns:
the unary rules of this grammar with the specified left-hand side

getUnaryRulesWithRHS

public java.util.List<UnaryRule> getUnaryRulesWithRHS(int rhs)
Returns the unary rules of this grammar with the specified right-hand side.

Parameters:
rhs - the code of the right-hand side
Returns:
the unary rules of this grammar with the specified right-hand side

getBinaryRules

public java.util.List<BinaryRule> getBinaryRules()
Returns the binary rules of this grammar.

Returns:
the binary rules of this grammar

getNBinaryRules

public int getNBinaryRules()
Returns the number of binary rules of this grammar.

Returns:
the number of binary rules of this grammar

getBinaryRulesWithLHS

public java.util.List<BinaryRule> getBinaryRulesWithLHS(int lhs)
Returns the binary rules of this grammar with the specified left-hand side.

Parameters:
lhs - the left-hand side
Returns:
the binary rules of this grammar with the specified left-hand side

getBinaryRulesWithRHS1

public java.util.List<BinaryRule> getBinaryRulesWithRHS1(int rhs1)
Returns the binary rules of this grammar with the specified right-hand side.

Parameters:
rhs1 - the first category on the right-hand side
Returns:
the binary rules of this grammar that have the specified category as the first category on their right-hand side

getBinaryRulesWithRHS2

public java.util.List<BinaryRule> getBinaryRulesWithRHS2(int rhs2)
Returns the binary rules of this grammar with the specified right-hand side.

Parameters:
rhs2 - the second category on the right-hand side
Returns:
the binary rules of this grammar that have the specified category as the second category on their right-hand side