edu.umn.cs.nlp.parser
Class PCNFGrammar

java.lang.Object
  extended by edu.umn.cs.nlp.parser.PCNFGrammar
All Implemented Interfaces:
GrammarInterface

public class PCNFGrammar
extends Object
implements GrammarInterface

Probabilistic grammar capable of parsing using the CKY parsing algorithm. Performs Viterbi traceback to find the most likely parse.

Rules in this grammar are required to be in Chomsky Normal Form (CNF).

Version:
$LastChangedDate: 2007-07-23 19:45:41 -0500 (Mon, 23 Jul 2007) $
Author:
Lane Schwartz

Constructor Summary
PCNFGrammar()
          Constructs a new probabilistic grammar, the rules of which are required to be in Chomsky Normal Form (CNF).
 
Method Summary
 void addLexicalRule(String lhs, String rhs, double logProb)
          Add a new unary rule to the grammar.
 void addRule(String lhs, String rhs1, String rhs2, double logProb)
          Add a new binary rule to the grammar.
static void main(String[] args)
          Example usage of this class with a small sample grammar and test sentences.
 ParseTree parse(String... token)
          Attempts to parse the given series of tokens using this grammar.
 ParseTree parse(String sentence)
          Attempts to parse the given sentence using this grammar.
 Collection<ParseTree> parseAll(String... sentences)
          Attempts to parse each of the given sentences using parse(String sentence).
 boolean parses(String... token)
          Determines if the given series of tokens can be successfully parsed by this grammar.
 boolean parses(String sentence)
          Determines if the given sentence can be successfully parsed by this grammar.
 void setLocale(Locale locale)
          Sets the locale.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PCNFGrammar

public PCNFGrammar()
Constructs a new probabilistic grammar, the rules of which are required to be in Chomsky Normal Form (CNF).

Method Detail

addRule

public void addRule(String lhs,
                    String rhs1,
                    String rhs2,
                    double logProb)
Add a new binary rule to the grammar.

Parameters:
lhs - The left-hand-side of the rule. Must be a non-terminal.
rhs1 - The left child of the rule. Must be a non-terminal.
rhs2 - The right child of the rule. Must be a non-terminal.
logProb - The log probability of the given rule.

addLexicalRule

public void addLexicalRule(String lhs,
                           String rhs,
                           double logProb)
Add a new unary rule to the grammar.

Parameters:
lhs - The left-hand-side of the rule. Must be a non-terminal.
rhs - The child of the rule. Must be a terminal.
logProb - The log probability of the given rule.

parses

public boolean parses(String sentence)
Determines if the given sentence can be successfully parsed by this grammar.

Specified by:
parses in interface GrammarInterface
Parameters:
sentence - The sentence to be parsed. Will be lowercased using the default locale and then tokenized prior to parsing.
Returns:
true if this grammar can parse the given sentence; false otherwise.
See Also:
setLocale(Locale)

parses

public boolean parses(String... token)
Determines if the given series of tokens can be successfully parsed by this grammar.

Specified by:
parses in interface GrammarInterface
Parameters:
token - Array of tokens specifying the sentence to be parsed. No lowercasing will be performed by this method.
Returns:
true if this grammar can parse the given sentence; false otherwise.

parse

public ParseTree parse(String sentence)
Attempts to parse the given sentence using this grammar. Uses the CKY parsing algorithm. Performs Viterbi traceback to find the most likely parse.

Specified by:
parse in interface GrammarInterface
Parameters:
sentence - The sentence to be parsed. Will be lowercased using the default locale and then tokenized prior to parsing.
Returns:
a valid parse tree representing the most likely parse if this grammar can parse the given sentence; ParseTree.NULL_PARSE otherwise.
See Also:
setLocale(Locale)

parse

public ParseTree parse(String... token)
Attempts to parse the given series of tokens using this grammar. Uses the CKY parsing algorithm. Performs Viterbi traceback to find the most likely parse.

Specified by:
parse in interface GrammarInterface
Parameters:
token - Array of tokens specifying the sentence to be parsed. No lowercasing will be performed by this method.
Returns:
a valid parse tree representing the most likely parse if this grammar can parse the given sentence; ParseTree.NULL_PARSE otherwise.

parseAll

public Collection<ParseTree> parseAll(String... sentences)
Attempts to parse each of the given sentences using parse(String sentence).

Specified by:
parseAll in interface GrammarInterface
Parameters:
sentences - the sentences to be parsed.
Returns:
one ParseTree per sentence

setLocale

public void setLocale(Locale locale)
Sets the locale. This will be used by parse(String sentence) and by parses(String sentence) when lowercasing the sentence.

Parameters:
locale - the locale to use when performing lowercasing
See Also:
String.toLowerCase(Locale)

main

public static void main(String[] args)
Example usage of this class with a small sample grammar and test sentences.

Parameters:
args - arguments are ignored