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

type WeightedGrammar

source code

        object --+    
                 |    
ContextFreeGrammar --+
                     |
                    WeightedGrammar

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

PCFG productions should be WeightedProductions. WeightedGrammars impose the constraint that the set of productions with any given left-hand-side must have probabilities that sum to 1.

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 WeightedProductions.
source code

Inherited from ContextFreeGrammar: __repr__, __str__, check_coverage, is_binarised, is_chomsky_normal_form, is_flexible_chomsky_normal_form, is_leftcorner, is_lexical, is_nonempty, is_nonlexical, leftcorner_parents, leftcorners, max_len, min_len, productions, start

Class Variables [hide private]
float EPSILON = 0.01
The acceptable margin of error for checking that productions with a given left-hand side have probabilities that sum to 1.
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 WeightedProductions.

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.
Raises:
  • ValueError - if the set of productions with any left-hand-side do not have probabilities that sum to a value within EPSILON of 1.
Overrides: ContextFreeGrammar.__init__