Package nltk :: Module grammar :: Class ContextFreeGrammar
[hide private]
[frames] | no frames]

type ContextFreeGrammar

source code

object --+
         |
        ContextFreeGrammar
Known Subclasses:

A context-free grammar. A grammar consists of a start state and a set of productions. The set of terminals and nonterminals is implicitly specified by the productions.

If you need efficient key-based access to productions, you can use a subclass to implement it.

Instance Methods [hide private]
 
__init__(self, start, productions, calculate_leftcorners=True)
Create a new context-free grammar, from the given start state and set of Productions.
source code
 
_calculate_indexes(self) source code
 
_calculate_leftcorners(self) source code
Nonterminal
start(self)
Returns: The start symbol of the grammar
source code
list of Production
productions(self, lhs=None, rhs=None, empty=False)
Return the grammar productions, filtered by the left-hand side or the first item in the right-hand side.
source code
set of Nonterminal
leftcorners(self, cat)
Return the set of all nonterminals that the given nonterminal can start with, including itself.
source code
bool
is_leftcorner(self, cat, left)
True if left is a leftcorner of cat, where left can be a terminal or a nonterminal.
source code
set of Nonterminal
leftcorner_parents(self, cat)
Return the set of all nonterminals for which the given category is a left corner.
source code
 
check_coverage(self, tokens)
Check whether the grammar rules cover the given list of tokens.
source code
 
_calculate_grammar_forms(self)
Pre-calculate of which form(s) the grammar is.
source code
 
is_lexical(self)
True if all productions are lexicalised.
source code
 
is_nonlexical(self)
True if all lexical rules are "preterminals", that is, unary rules which can be separated in a preprocessing step.
source code
 
min_len(self)
The right-hand side length of the shortest grammar production.
source code
 
max_len(self)
The right-hand side length of the longest grammar production.
source code
 
is_nonempty(self)
True if there are no empty productions.
source code
 
is_binarised(self)
True if all productions are at most binary.
source code
 
is_flexible_chomsky_normal_form(self)
True if all productions are of the forms A -> B C, A -> B, or A -> "s".
source code
 
is_chomsky_normal_form(self)
A grammar is of Chomsky normal form if all productions are of the forms A -> B C, or A -> "s".
source code
 
__repr__(self) source code
 
__str__(self) source code
Method Details [hide private]

__init__(self, start, productions, calculate_leftcorners=True)
(Constructor)

source code 

Create a new context-free grammar, from the given start state and set of Productions.

Parameters:
  • start (Nonterminal) - The start symbol
  • productions (list of Production) - The list of productions that defines the grammar
  • calculate_leftcorners (bool) - False if we don't want to calculate the leftcorner relation. In that case, some optimized chart parsers won't work.
Overrides: object.__init__

start(self)

source code 
Returns: Nonterminal
The start symbol of the grammar

productions(self, lhs=None, rhs=None, empty=False)

source code 

Return the grammar productions, filtered by the left-hand side or the first item in the right-hand side.

Parameters:
  • lhs - Only return productions with the given left-hand side.
  • rhs - Only return productions with the given first item in the right-hand side.
  • empty - Only return productions with an empty right-hand side.
Returns: list of Production
A list of productions matching the given constraints.

leftcorners(self, cat)

source code 

Return the set of all nonterminals that the given nonterminal can start with, including itself.

This is the reflexive, transitive closure of the immediate leftcorner relation: (A > B) iff (A -> B beta)

Parameters:
  • cat (Nonterminal) - the parent of the leftcorners
Returns: set of Nonterminal
the set of all leftcorners

is_leftcorner(self, cat, left)

source code 

True if left is a leftcorner of cat, where left can be a terminal or a nonterminal.

Parameters:
  • cat (Nonterminal) - the parent of the leftcorner
  • left (Terminal or Nonterminal) - the suggested leftcorner
Returns: bool

leftcorner_parents(self, cat)

source code 

Return the set of all nonterminals for which the given category is a left corner. This is the inverse of the leftcorner relation.

Parameters:
  • cat (Nonterminal) - the suggested leftcorner
Returns: set of Nonterminal
the set of all parents to the leftcorner

check_coverage(self, tokens)

source code 

Check whether the grammar rules cover the given list of tokens. If not, then raise an exception.

Parameters:
  • tokens (list of str)

is_nonlexical(self)

source code 

True if all lexical rules are "preterminals", that is, unary rules which can be separated in a preprocessing step.

This means that all productions are of the forms A -> B1 ... Bn (n>=0), or A -> "s".

Note: is_lexical() and is_nonlexical() are not opposites. There are grammars which are neither, and grammars which are both.

is_binarised(self)

source code 

True if all productions are at most binary. Note that there can still be empty and unary productions.

__repr__(self)
(Representation operator)

source code 
Overrides: object.__repr__
(inherited documentation)

__str__(self)
(Informal representation operator)

source code 
Overrides: object.__str__
(inherited documentation)