Basic data classes for representing context free grammars. A "grammar" specifies which trees can represent the structure of a given text. Each of these trees is called a "parse tree" for the text (or simply a "parse"). In a "context free" grammar, the set of parse trees for any piece of a text can depend only on that piece, and not on the rest of the text (i.e., the piece's context). Context free grammars are often used to find possible syntactic structures for sentences. In this context, the leaves of a parse tree are word tokens; and the node values are phrasal categories, such as NP and VP.
The CFG class is used to encode context free grammars. Each CFG consists of a start symbol and a set of productions. The "start symbol" specifies the root node value for parse trees. For example, the start symbol for syntactic parsing is usually S. Start symbols are encoded using the Nonterminal class, which is discussed below.
A Grammar's "productions" specify what parent-child relationships a parse tree can contain. Each production specifies that a particular node can be the parent of a particular set of children. For example, the production <S> -> <NP> <VP> specifies that an S node can be the parent of an NP node and a VP node.
Grammar productions are implemented by the Production class. Each Production consists of a left hand side and a right hand side. The "left hand side" is a Nonterminal that specifies the node type for a potential parent; and the "right hand side" is a list that specifies allowable children for that parent. This lists consists of Nonterminals and text types: each Nonterminal indicates that the corresponding child may be a TreeToken with the specified node type; and each text type indicates that the corresponding child may be a Token with the with that type.
The Nonterminal class is used to distinguish node values from leaf values. This prevents the grammar from accidentally using a leaf value (such as the English word "A") as the node of a subtree. Within a CFG, all node values are wrapped in the Nonterminal class. Note, however, that the trees that are specified by the grammar do not include these Nonterminal wrappers.
Grammars can also be given a more procedural interpretation. According to this interpretation, a Grammar specifies any tree structure tree that can be produced by the following procedure:
The operation of replacing the left hand side (lhs) of a production with the right hand side (rhs) in a tree (tree) is known as "expanding" lhs to rhs in tree.
Class | CFG |
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. |
Class |
|
A dependency grammar. A DependencyGrammar consists of a set of productions. Each production specifies a head/modifier relationship between a pair of words. |
Class |
|
A dependency grammar production. Each production maps a single head word to an unordered list of one or more modifier words. |
Class |
|
A feature structure that's also a nonterminal. It acts as its own symbol, and automatically freezes itself when hashed. |
Class |
|
A feature-based grammar. This is equivalent to a CFG whose nonterminals are all FeatStructNonterminal. |
Class |
|
A helper class for FeatureGrammars, designed to be different from ordinary strings. This is to stop the FeatStruct FOO[] from being compare equal to the terminal "FOO". |
Class |
|
A non-terminal symbol for a context free grammar. Nonterminal is a wrapper class for node values; it is used by Production objects to distinguish node values from leaf values. The node value that is wrapped by a ... |
Class | PCFG |
A probabilistic context-free grammar. A PCFG consists of a start state and a set of productions with probabilities. The set of terminals and nonterminals is implicitly specified by the productions. |
Class |
|
No summary |
Class |
|
A probabilistic context free grammar production. A PCFG ProbabilisticProduction is essentially just a Production that has an associated probability, which represents how likely it is that this production will be used... |
Class |
|
A grammar production. Each production maps a single symbol on the "left-hand side" to a sequence of symbols on the "right-hand side". (In the case of context-free productions, the left-hand side must be a ... |
Function | cfg |
A demonstration showing how CFGs can be created and used. |
Function | demo |
Undocumented |
Function | dg |
A demonstration showing the creation and inspection of a DependencyGrammar. |
Function | fcfg |
Undocumented |
Function | induce |
Induce a PCFG grammar from a list of productions. |
Function | is |
No summary |
Function | is |
Return True if the item is a terminal, which currently is if it is hashable and not a Nonterminal. |
Function | nonterminals |
Given a string containing a list of symbol names, return a list of Nonterminals constructed from those symbols. |
Function | pcfg |
A demonstration showing how a PCFG can be created and used. |
Function | read |
Return a pair consisting of a starting category and a list of Productions. |
Function | sdg |
A demonstration of how to read a string representation of a CoNLL format dependency tree. |
Function | standard |
Undocumented |
Variable | toy |
Undocumented |
Variable | toy |
Undocumented |
Function | _read |
Return a list of context-free Productions. |
Function | _read |
Undocumented |
Function | _read |
Return a list of feature-based Productions. |
Function | _read |
Return a list of PCFG ProbabilisticProductions. |
Function | _read |
Parse a grammar rule, given as a string, and return a list of productions. |
Constant | _ARROW |
Undocumented |
Constant | _DISJUNCTION |
Undocumented |
Constant | _PROBABILITY |
Undocumented |
Constant | _READ |
Undocumented |
Constant | _SPLIT |
Undocumented |
Constant | _STANDARD |
Undocumented |
Constant | _TERMINAL |
Undocumented |
Induce a PCFG grammar from a list of productions.
The probability of a production A -> B C in a PCFG is:
Parameters | |
start:Nonterminal | The start symbol |
productions:list(Production) | The list of productions that defines the grammar |
Return True if the item is a terminal, which currently is if it is hashable and not a Nonterminal.
Returns | |
bool | Undocumented |
Given a string containing a list of symbol names, return a list of Nonterminals constructed from those symbols.
Parameters | |
symbols:str | The symbol name string. This string can be delimited by either spaces or commas. |
Returns | |
list(Nonterminal) | A list of Nonterminals constructed from the symbol names given in symbols. The Nonterminals are sorted in the same order as the symbols names. |
Return a pair consisting of a starting category and a list of Productions.
Parameters | |
input | a grammar, either in the form of a string or else as a list of strings. |
nonterm | a function for parsing nonterminals. It should take a (string, position) as argument and return a (nonterminal, position) as result. |
probabilistic:bool | are the grammar rules probabilistic? |
encoding:str | the encoding of the grammar, if it is a binary string |
Undocumented
Value |
|