
RKc        !   @   sl  d  Z  d d k Z d d k l Z d d k Td d k l Z d d k l Z l	 Z	 l
 Z
 l Z l Z d e f d     YZ d	   Z d
 e	 e f d     YZ d e f d     YZ d e f d     YZ d e e f d     YZ d e f d     YZ d d k l Z d e e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d      YZ d!   Z d"   Z d#   Z d$   Z d%   Z  d&   Z! d d d d'  Z# e d(  d)    Z$ e i% d* e i&  Z' e i% d+ e i&  Z( e i% d, e i&  Z) e i% d- e i&  Z* e+ d.  Z, e+ d/  Z- e i% d0 e i&  Z. d1   Z/ e i% d2 e i&  Z0 e i% d3  Z1 d4   Z2 d5   Z3 d6   Z4 e  d7  Z5 e  d8  Z6 d9   Z7 d:   Z8 d;   Z9 d<   Z: d=   Z; e< d> j o e;   n d d? d d d d d d d d@ dA dB dC dD dE dF dG dH dI dJ dK dL dM dN dO dP dQ g Z= d S(R   s  
Basic data classes for representing context free grammars.  A
X{grammar} specifies which trees can represent the structure of a
given text.  Each of these trees is called a X{parse tree} for the
text (or simply a X{parse}).  In a X{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 C{NP}
and C{VP}.

The L{ContextFreeGrammar} class is used to encode context free grammars.  Each
C{ContextFreeGrammar} consists of a start symbol and a set of productions.
The X{start symbol} specifies the root node value for parse trees.  For example,
the start symbol for syntactic parsing is usually C{S}.  Start
symbols are encoded using the C{Nonterminal} class, which is discussed
below.

A Grammar's X{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 C{<S> -> <NP> <VP>} specifies that an C{S} node can
be the parent of an C{NP} node and a C{VP} node.

Grammar productions are implemented by the C{Production} class.
Each C{Production} consists of a left hand side and a right hand
side.  The X{left hand side} is a C{Nonterminal} that specifies the
node type for a potential parent; and the X{right hand side} is a list
that specifies allowable children for that parent.  This lists
consists of C{Nonterminals} and text types: each C{Nonterminal}
indicates that the corresponding child may be a C{TreeToken} with the
specified node type; and each text type indicates that the
corresponding child may be a C{Token} with the with that type.

The C{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 C{ContextFreeGrammar}, all node values are wrapped in the C{Nonterminal} class.
Note, however, that the trees that are specified by the grammar do
B{not} include these C{Nonterminal} wrappers.

Grammars can also be given a more procedural interpretation.  According to
this interpretation, a Grammar specifies any tree structure M{tree} that
can be produced by the following procedure:

    - Set M{tree} to the start symbol
    - Repeat until M{tree} contains no more nonterminal leaves:
      - Choose a production M{prod} with whose left hand side
        M{lhs} is a nonterminal leaf of M{tree}.
      - Replace the nonterminal leaf with a subtree, whose node
        value is the value wrapped by the nonterminal M{lhs}, and
        whose children are the right hand side of M{prod}.

The operation of replacing the left hand side (M{lhs}) of a production
with the right hand side (M{rhs}) in a tree (M{tree}) is known as
X{expanding} M{lhs} to M{rhs} in M{tree}.
iN(   t
   deprecated(   t   *(   t   ImmutableProbabilisticMixIn(   t
   FeatStructt   FeatDictt   FeatStructParsert   SLASHt   TYPEt   Nonterminalc           B   sh   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z RS(   s:  
    A non-terminal symbol for a context free grammar.  C{Nonterminal}
    is a wrapper class for node values; it is used by
    C{Production}s to distinguish node values from leaf values.
    The node value that is wrapped by a C{Nonterminal} is known as its
    X{symbol}.  Symbols are typically strings representing phrasal
    categories (such as C{"NP"} or C{"VP"}).  However, more complex
    symbol types are sometimes used (e.g., for lexicalized grammars).
    Since symbols are node values, they must be immutable and
    hashable.  Two C{Nonterminal}s are considered equal if their
    symbols are equal.

    @see: L{ContextFreeGrammar}
    @see: L{Production}
    @type _symbol: (any)
    @ivar _symbol: The node value corresponding to this
        C{Nonterminal}.  This value must be immutable and hashable. 
    c         C   s   | |  _  t |  |  _ d S(   s   
        Construct a new non-terminal from the given symbol.

        @type symbol: (any)
        @param symbol: The node value corresponding to this
            C{Nonterminal}.  This value must be immutable and
            hashable. 
        N(   t   _symbolt   hasht   _hash(   t   selft   symbol(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   __init__g   s    		c         C   s   |  i  S(   sf   
        @return: The node value corresponding to this C{Nonterminal}. 
        @rtype: (any)
        (   R	   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   s   s    c         C   sB   y' |  i  | i  j o t | |  i  SWn t j
 o t SXd S(   s  
        @return: True if this non-terminal is equal to C{other}.  In
            particular, return true iff C{other} is a C{Nonterminal}
            and this non-terminal's symbol is equal to C{other}'s
            symbol.
        @rtype: C{boolean}
        N(   R	   t
   isinstancet	   __class__t   AttributeErrort   False(   R   t   other(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   __eq__z   s
    c         C   s   |  | j S(   s  
        @return: True if this non-terminal is not equal to C{other}.  In
            particular, return true iff C{other} is not a C{Nonterminal}
            or this non-terminal's symbol is not equal to C{other}'s
            symbol.
        @rtype: C{boolean}
        (    (   R   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   __ne__   s    c         C   s&   y t  |  i | i  SWn d SXd  S(   Ni(   t   cmpR	   (   R   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   __cmp__   s    c         C   s   |  i  S(   N(   R   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   __hash__   s    c         C   s4   t  |  i t  o d |  i f Sd |  i f Sd S(   se   
        @return: A string representation for this C{Nonterminal}.
        @rtype: C{string}
        s   %ss   %rN(   R   R	   t
   basestring(   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   __repr__   s    c         C   s4   t  |  i t  o d |  i f Sd |  i f Sd S(   se   
        @return: A string representation for this C{Nonterminal}.
        @rtype: C{string}
        s   %ss   %rN(   R   R	   R   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   __str__   s    c         C   s   t  d |  i | i f  S(   sa  
        @return: A new nonterminal whose symbol is C{M{A}/M{B}}, where
            C{M{A}} is the symbol for this nonterminal, and C{M{B}}
            is the symbol for rhs.
        @rtype: L{Nonterminal}
        @param rhs: The nonterminal used to form the right hand side
            of the new nonterminal.
        @type rhs: L{Nonterminal}
        s   %s/%s(   R   R	   (   R   t   rhs(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   __div__   s    
c         C   s   t  |  i  S(   N(   t   lenR	   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   __len__   s    (   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R   R   R   R   (    (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   T   s   				
			
	
	c         C   sW   d |  j o |  i  d  } n |  i    } g  } | D] } | t | i    q7 ~ S(   s  
    Given a string containing a list of symbol names, return a list of
    C{Nonterminals} constructed from those symbols.  

    @param symbols: The symbol name string.  This string can be
        delimited by either spaces or commas.
    @type symbols: C{string}
    @return: A list of C{Nonterminals} constructed from the symbol
        names given in C{symbols}.  The C{Nonterminals} are sorted
        in the same order as the symbols names.
    @rtype: C{list} of L{Nonterminal}
    t   ,(   t   splitR   t   strip(   t   symbolst   symbol_listt   _[1]t   s(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   nonterminals   s     t   FeatStructNonterminalc           B   s    e  Z d  Z d   Z d   Z RS(   s|   A feature structure that's also a nonterminal.  It acts as its
    own symbol, and automatically freezes itself when hashed.c         C   s   |  i    t i |   S(   N(   t   freezeR   R   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR      s    
c         C   s   |  S(   N(    (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR      s    (   R    R!   R"   R   R   (    (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR+      s   	t
   Productionc           B   sz   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z RS(   s  
    A grammar production.  Each production maps a single symbol
    on the X{left-hand side} to a sequence of symbols on the
    X{right-hand side}.  (In the case of context-free productions,
    the left-hand side must be a C{Nonterminal}, and the right-hand
    side is a sequence of terminals and C{Nonterminals}.)
    X{terminals} can be any immutable hashable object that is
    not a C{Nonterminal}.  Typically, terminals are strings
    representing words, such as C{"dog"} or C{"under"}.

    @see: L{ContextFreeGrammar}
    @see: L{DependencyGrammar}
    @see: L{Nonterminal}
    @type _lhs: L{Nonterminal}
    @ivar _lhs: The left-hand side of the production.
    @type _rhs: C{tuple} of (C{Nonterminal} and (terminal))
    @ivar _rhs: The right-hand side of the production.
    c         C   s]   t  | t t f  o t d   n | |  _ t |  |  _ t |  i |  i f  |  _ d S(   s  
        Construct a new C{Production}.

        @param lhs: The left-hand side of the new C{Production}.
        @type lhs: L{Nonterminal}
        @param rhs: The right-hand side of the new C{Production}.
        @type rhs: sequence of (C{Nonterminal} and (terminal))
        s9   production right hand side should be a list, not a stringN(	   R   t   strt   unicodet	   TypeErrort   _lhst   tuplet   _rhsR
   R   (   R   t   lhsR   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR      s
    		c         C   s   |  i  S(   sc   
        @return: the left-hand side of this C{Production}.
        @rtype: L{Nonterminal}
        (   R1   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR4      s    c         C   s   |  i  S(   s   
        @return: the right-hand side of this C{Production}.
        @rtype: sequence of (C{Nonterminal} and (terminal))
        (   R3   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s    c         C   s   t  |  i  S(   sX   
        @return: the length of the right-hand side.
        @rtype: C{integer}
        (   R   R3   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s    c         C   s1   t  g  } |  i D] } | t | t  q ~  S(   sl   
        @return: True if the right-hand side only contains C{Nonterminal}s
        @rtype: C{bool}
        (   t   allR3   R   R   (   R   R(   t   n(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   is_nonlexical  s    c         C   s   |  i    S(   sm   
        @return: True if the right-hand contain at least one terminal token
        @rtype: C{bool}
        (   R7   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt
   is_lexical  s    c         C   s9   d |  i  f } x" |  i D] } | d | f 7} q W| S(   sv   
        @return: A verbose string representation of the
            C{Production}.
        @rtype: C{string}
        s   %r ->s    %r(   R1   R3   (   R   R.   t   elt(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   #  s
    
 c         C   s   d |  S(   sw   
        @return: A concise string representation of the
            C{Production}. 
        @rtype: C{string}
        s   %s(    (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   .  s    c         C   s6   t  | |  i  o# |  i | i j o |  i | i j S(   sf   
        @return: true if this C{Production} is equal to C{other}.
        @rtype: C{boolean}
        (   R   R   R1   R3   (   R   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   6  s    c         C   s   |  | j S(   N(    (   R   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   ?  s    c         C   s=   t  | |  i  p d St |  i |  i f | i | i f  S(   Ni(   R   R   R   R1   R3   (   R   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   B  s     c         C   s   |  i  S(   sU   
        @return: A hash value for the C{Production}.
        @rtype: C{int}
        (   R   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   F  s    (   R    R!   R"   R   R4   R   R   R7   R8   R   R   R   R   R   R   (    (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR-      s   												t   DependencyProductionc           B   s   e  Z d  Z d   Z RS(   s   
    A dependency grammar production.  Each production maps a single
    head word to an unordered list of one or more modifier words.
    c         C   s9   d |  i  f } x" |  i D] } | d | f 7} q W| S(   s   
        @return: A verbose string representation of the 
            C{DependencyProduction}.
        @rtype: C{string}
        s   '%s' ->s    '%s'(   R1   R3   (   R   R.   R9   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   S  s
    
 (   R    R!   R"   R   (    (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR:   N  s   t   WeightedProductionc           B   s;   e  Z d  Z d   Z d   Z d   Z d   Z d   Z RS(   s  
    A probabilistic context free grammar production.
    PCFG C{WeightedProduction}s are essentially just C{Production}s that
    have probabilities associated with them.  These probabilities are
    used to record how likely it is that a given production will
    be used.  In particular, the probability of a C{WeightedProduction}
    records the likelihood that its right-hand side is the correct
    instantiation for any given occurance of its left-hand side.

    @see: L{Production}
    c         K   s'   t  i |  |  t i |  | |  d S(   s{  
        Construct a new C{WeightedProduction}.

        @param lhs: The left-hand side of the new C{WeightedProduction}.
        @type lhs: L{Nonterminal}
        @param rhs: The right-hand side of the new C{WeightedProduction}.
        @type rhs: sequence of (C{Nonterminal} and (terminal))
        @param prob: Probability parameters of the new C{WeightedProduction}.
        N(   R   R   R-   (   R   R4   R   t   prob(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   k  s    
c         C   s   t  i |   d |  i   S(   Ns    [%s](   R-   R   R<   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   x  s    c         C   sO   t  | |  i  o< |  i | i j o) |  i | i j o |  i   | i   j S(   N(   R   R   R1   R3   R<   (   R   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   {  s    c         C   s   |  | j S(   N(    (   R   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s    c         C   s   t  |  i |  i |  i   f  S(   N(   R
   R1   R3   R<   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s    (   R    R!   R"   R   R   R   R   R   (    (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR;   _  s   				t   ContextFreeGrammarc           B   s   e  Z d  Z d   Z d   Z d   Z d d e d  Z d   Z	 d   Z
 d   Z e d  d	    Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s$  
    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.
    c         C   sC   | |  _  | |  _ t d   | D  |  _ |  i   |  i   d S(   sG  
        Create a new context-free grammar, from the given start state
        and set of C{Production}s.
        
        @param start: The start symbol
        @type start: L{Nonterminal}
        @param productions: The list of productions that defines the grammar
        @type productions: C{list} of L{Production}
        c         s   s   x |  ] } | i    Vq Wd  S(   N(   R4   (   t   .0t   prod(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pys	   <genexpr>  s   	 N(   t   _startt   _productionst   sett   _categoriest   _calculate_indexest   _calculate_grammar_forms(   R   t   startt   productions(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s
    
		
c      	   C   s  h  |  _  h  |  _ h  |  _ h  |  _ x |  i D] } | i } | |  i  j o g  |  i  | <n |  i  | i |  | i oF | i d } | |  i j o g  |  i | <n |  i | i |  n | |  i | i   <xD | i D]9 } t	 | t
  o# |  i i | t    i |  q q Wq. Wh  } xZ |  i D]O } t |  d j o6 | i   d } | i | i   t    i |  q0q0Wt d   |  i D  } |  _ t d   |  i D  } |  _ t }	 x |	 o t }	 x | D] }
 x | |
 D] } t	 | t  o0 | | |
 j o | |
 i |  t }	 qq| | j or | | i | |
  p | |
 i | |  t }	 n | |
 i | |  p | | i | |
  t }	 qqqWqWqWd  S(   Ni    c         s   s"   x |  ] } | t    f Vq Wd  S(   N(   RB   (   R>   t   cat(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pys	   <genexpr>  s   	 c         s   s(   x! |  ] } | t  | g  f Vq Wd  S(   N(   RB   (   R>   RH   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pys	   <genexpr>  s   	 (   t
   _lhs_indext
   _rhs_indext   _empty_indext   _lexical_indexRA   R1   t   appendR3   R4   R   R.   t
   setdefaultRB   t   addR   R   t   dictRC   t   _leftcornerst   _leftcorner_parentst   TrueR   R   t   issubsett   update(   R   R?   R4   t   rhs0t   tokent   immediate_leftcornerst   childt	   leftwordst   leftparentst
   once_againt   parent(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyRD     s^    				
 	

 +
 *     
c         C   s   |  i  S(   sY   
        @return: The start symbol of the grammar
        @rtype: L{Nonterminal}
        (   R@   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyRF     s    c         C   s  | o | o t  d   n | o( | o  | p |  i S|  i i   Sn | oJ | oB | p |  i i | g   S| |  i j o |  i | g Sg  Snr | o | o |  i i | g   Sg  } |  i i | g   D]- } | |  i i | g   j o | | q q ~ Sd S(   s  
        Return the grammar productions, filtered by the left-hand side
        or the first item in the right-hand side.
        
        @param lhs: Only return productions with the given left-hand side.
        @param rhs: Only return productions with the given first item 
        in the right-hand side.
        @param empty: Only return productions with an empty right-hand side.
        @return: A list of productions matching the given constraints.
        @rtype: C{list} of C{Production}
        sC   You cannot select empty and non-empty productions at the same time.N(   t
   ValueErrorRA   RK   t   valuesRI   t   getRJ   (   R   R4   R   t   emptyR(   R?   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyRG     s      c         C   s   |  i  i | t    S(   s   
        Return the set of all words that the given category can start with.
        Also called the I{first set} in compiler construction.
        (   RQ   R`   RB   (   R   RH   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   leftcorners  s    c         C   s   |  i  i | t    S(   si   
        Return the set of all categories for which the given category
        is a left corner.
        (   RR   R`   RB   (   R   RH   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   leftcorner_parents  s    c         C   sp   g  } | D]$ } |  i  i |  p | | q q ~ } | o- d i d   | D  } t d |   n d S(   sz   
        Check whether the grammar rules cover the given list of tokens.
        If not, then raise an exception.
        s   , c         s   s    x |  ] } d  | f Vq Wd S(   s   %rN(    (   R>   t   w(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pys	   <genexpr>  s   	 s3   Grammar does not cover some of the input words: %r.N(   RL   R`   t   joinR^   (   R   t   tokensR(   t   tokt   missing(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   check_coverage  s    's.   Use ContextFreeGrammar.check_coverage instead.c         C   s-   x& | D] } |  i  i |  o t Sq Wt S(   s   
        Check whether the grammar rules cover the given list of tokens.

        @param tokens: the given list of tokens.
        @type tokens: a C{list} of C{string} objects.
        @return: True/False
        (   RL   R`   R   RS   (   R   Rf   RW   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   covers$  s
    	 	c      	   C   s   |  i  } t g  } | D] } | | i   q ~  |  _ t g  } | D]* } t |  d j o | | i   qG qG ~  |  _ t d   | D  |  _ t	 d   | D  |  _
 t g  } | D]* } t |  d j o | | i   q q ~  |  _ d S(   s@   
        Pre-calculate of which form(s) the grammar is.
        i   c         s   s   x |  ] } t  |  Vq Wd  S(   N(   R   (   R>   t   p(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pys	   <genexpr>:  s   	 c         s   s   x |  ] } t  |  Vq Wd  S(   N(   R   (   R>   Rk   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pys	   <genexpr>;  s   	 N(   RA   R5   R8   t   _is_lexicalR   R7   t   _is_nonlexicalt   mint   _min_lent   maxt   _max_lent   _all_unary_are_lexical(   R   t   prodsR(   Rk   t   _[2]t   _[3](    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyRE   2  s    	03c         C   s   |  i  S(   s:   
        True if all productions are lexicalised.
        (   Rl   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR8   ?  s    c         C   s   |  i  S(   s  
        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.
        (   Rm   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR7   E  s    c         C   s   |  i  S(   sP   
        The right-hand side length of the shortest grammar production.
        (   Ro   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   min_lenR  s    c         C   s   |  i  S(   sO   
        The right-hand side length of the longest grammar production.
        (   Rq   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   max_lenX  s    c         C   s   |  i  d j S(   s9   
        True if there are no empty productions.
        i    (   Ro   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   is_nonempty^  s    c         C   s   |  i  d j S(   s   
        True if all productions are at most binary.
        Note that there can still be empty and unary productions.
        i   (   Rq   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   is_binarisedd  s    c         C   s$   |  i    o |  i   o
 |  i   S(   sa   
        True if all productions are of the forms
        A -> B C, A -> B, or A -> "s".
        (   Rx   R7   Ry   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   is_flexible_chomsky_normal_formk  s    c         C   s   |  i    o |  i S(   sx   
        A grammar is of Chomsky normal form if all productions
        are of the forms A -> B C, or A -> "s".
        (   Rz   Rr   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   is_chomsky_normal_formr  s    c         C   s   d t  |  i  S(   Ns   <Grammar with %d productions>(   R   RA   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   z  s    c         C   sJ   d t  |  i  } | d |  i 7} x |  i D] } | d | 7} q. W| S(   Ns   Grammar with %d productionss    (start state = %r)s   
    %s(   R   RA   R@   (   R   R.   t
   production(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   }  s    
 N(   R    R!   R"   R   RD   RF   t   NoneR   RG   Rb   Rc   Ri   R    Rj   RE   R8   R7   Rv   Rw   Rx   Ry   Rz   R{   R   R   (    (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR=     s(   		2		)													(   t
   Deprecatedt   Grammarc           B   s   e  Z d  Z RS(   s$   Use nltk.ContextFreeGrammar instead.(   R    R!   R"   (    (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s   t   FeatureGrammarc           B   sM   e  Z d  Z d   Z d   Z d d e d  Z d   Z d   Z	 d   Z
 RS(   s,  
    A feature-based grammar.  This is equivalent to a 
    L{ContextFreeGrammar} whose nonterminals are
    L{FeatStructNonterminal}s.

    A grammar consists of a start state and a set of 
    productions.  The set of terminals and nonterminals 
    is implicitly specified by the productions.
    c         C   s   t  i |  | |  d S(   sS  
        Create a new feature-based grammar, from the given start 
        state and set of C{Production}s.
        
        @param start: The start symbol
        @type start: L{FeatStructNonterminal}
        @param productions: The list of productions that defines the grammar
        @type productions: C{list} of L{Production}
        N(   R=   R   (   R   RF   RG   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s    
c         C   s<  h  |  _  h  |  _ h  |  _ h  |  _ x|  i D]} |  i | i  } | |  i  j o g  |  i  | <n |  i  | i |  | i oO |  i | i d  } | |  i j o g  |  i | <n |  i | i |  n | |  i |  i | i  <xD | i D]9 } t	 | t
  o# |  i i | t    i |  q q Wq. Wd  S(   Ni    (   RI   RJ   RK   RL   RA   t   _get_type_if_possibleR1   RM   R3   R   R.   RN   RB   RO   (   R   R?   R4   RV   RW   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyRD     s(    				
 

 c         C   sA  | o | o t  d   n | o( | o  | p |  i S|  i i   Sn | o\ | oT | p |  i i |  i |  g   S| |  i j o |  i |  i |  g Sg  Sn | o% | o |  i i |  i |  g   Sg  } |  i i |  i |  g   D]6 } | |  i i |  i |  g   j o | | q q ~ Sd S(   s  
        Return the grammar productions, filtered by the left-hand side
        or the first item in the right-hand side.
        
        @param lhs: Only return productions with the given left-hand side.
        @param rhs: Only return productions with the given first item 
        in the right-hand side.
        @param empty: Only return productions with an empty right-hand side.
        @return: A list of productions matching the given constraints.
        @rtype: C{list} of C{Production}
        sC   You cannot select empty and non-empty productions at the same time.N(   R^   RA   RK   R_   RI   R`   R   RJ   (   R   R4   R   Ra   R(   R?   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyRG     s     )c         C   s   t  d   d S(   s   
        Return the set of all words that the given category can start with.
        Also called the I{first set} in compiler construction.
        s   Not implemented yetN(   t   NotImplementedError(   R   RH   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyRb     s    c         C   s   t  d   d S(   si   
        Return the set of all categories for which the given category
        is a left corner.
        s   Not implemented yetN(   R   (   R   RH   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyRc     s    c         C   s4   t  | t  o t | j o t | t  S| Sd S(   s   
        Helper function which returns the C{TYPE} feature of the C{item}, 
        if it exists, otherwise it returns the C{item} itself
        N(   R   RP   R   t   FeatureValueType(   R   t   item(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s    N(   R    R!   R"   R   RD   R}   R   RG   Rb   Rc   R   (    (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s   			)		R   c           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s   
    A helper class for L{FeatureGrammar}s, designed to be different
    from ordinary strings.  This is to stop the C{FeatStruct}
    C{FOO[]} from being compare equal to the terminal "FOO".
    c         C   s   | |  _  t |  |  _ d  S(   N(   t   _valueR
   R   (   R   t   value(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s    	c         C   s   d |  i  S(   Ns   <%s>(   R   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s    c         C   s)   t  t t |   p t  |  i | i  S(   N(   R   R   t   typeR   (   R   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s    c         C   s   |  i  S(   N(   R   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   
  s    (   R    R!   R"   R   R   R   R   (    (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s
   			t   DependencyGrammarc           B   s;   e  Z d  Z d   Z d   Z d   Z d   Z d   Z RS(   s   
    A dependency grammar.  A DependencyGrammar consists of a set of
    productions.  Each production specifies a head/modifier relationship
    between a pair of words.
    c         C   s   | |  _  d S(   s   
        Create a new dependency grammar, from the set of C{Production}s.
        
        @param productions: The list of productions that defines the grammar
        @type productions: C{list} of L{Production}
        N(   RA   (   R   RG   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s    c         C   sN   xG |  i  D]< } x3 | i D]( } | i | j o | | j o t Sq Wq
 Wt S(   sB  
        @param head: A head word.
        @type head: C{string}.
        @param mod: A mod word, to test as a modifier of 'head'.
        @type mod: C{string}.

        @return: true if this C{DependencyGrammar} contains a 
            C{DependencyProduction} mapping 'head' to 'mod'.
        @rtype: C{boolean}.
        (   RA   R3   R1   RS   R   (   R   t   headt   modR|   t   possibleMod(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   contains  s    
 
 c         C   sN   xG |  i  D]< } x3 | i D]( } | i | j o | | j o t Sq Wq
 Wt S(   sB  
        @param head: A head word.
        @type head: C{string}.
        @param mod: A mod word, to test as a modifier of 'head'.
        @type mod: C{string}.

        @return: true if this C{DependencyGrammar} contains a 
            C{DependencyProduction} mapping 'head' to 'mod'.
        @rtype: C{boolean}.
        (   RA   R3   R1   RS   R   (   R   R   R   R|   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   __contains__-  s    
 
 c         C   s9   d t  |  i  } x |  i D] } | d | 7} q W| S(   s|   
        @return: A verbose string representation of the
            C{DependencyGrammar}
        @rtype: C{string}
        s&   Dependency grammar with %d productionss   
  %s(   R   RA   (   R   R.   R|   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   J  s
    
 c         C   s   d t  |  i  S(   sb   
        @return: A concise string representation of the
            C{DependencyGrammar}
        s&   Dependency grammar with %d productions(   R   RA   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   U  s    (   R    R!   R"   R   R   R   R   R   (    (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s   					t   StatisticalDependencyGrammarc           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s   

    c         C   s   | |  _  | |  _ | |  _ d  S(   N(   RA   t   _eventst   _tags(   R   RG   t   eventst   tags(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   b  s    		c         C   sN   xG |  i  D]< } x3 | i D]( } | i | j o | | j o t Sq Wq
 Wt S(   sB  
        @param head: A head word.
        @type head: C{string}.
        @param mod: A mod word, to test as a modifier of 'head'.
        @type mod: C{string}.

        @return: true if this C{DependencyGrammar} contains a 
            C{DependencyProduction} mapping 'head' to 'mod'.
        @rtype: C{boolean}.
        (   RA   R3   R1   RS   R   (   R   R   R   R|   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   g  s    
 
 c      
   C   s   d t  |  i  } x |  i D] } | d | 7} q W| d 7} x, |  i D]! } | d |  i | | f 7} qI W| d 7} x, |  i D]! } | d | |  i | f 7} q W| S(   s   
        @return: A verbose string representation of the
            C{StatisticalDependencyGrammar}
        @rtype: C{string}
        s2   Statistical dependency grammar with %d productionss   
  %ss   
Events:s   
  %d:%ss   
Tags:s
   
 %s:	(%s)(   R   RA   R   R   (   R   R.   R|   t   eventt   tag_word(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   x  s    
 

 

 c         C   s   d t  |  i  S(   sm   
        @return: A concise string representation of the
            C{StatisticalDependencyGrammar}
        s2   Statistical Dependency grammar with %d productions(   R   RA   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s    (   R    R!   R"   R   R   R   R   (    (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   ]  s
   			t   WeightedGrammarc           B   s   e  Z d  Z d Z d   Z RS(   s  
    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 C{WeightedProduction}s.
    C{WeightedGrammar}s 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.

    @type EPSILON: C{float}
    @cvar EPSILON: The acceptable margin of error for checking that
        productions with a given left-hand side have probabilities
        that sum to 1.
    g{Gz?c         C   s   t  i |  | |  h  } x: | D]2 } | i | i   d  | i   | | i   <q  WxZ | i   D]L \ } } d t i | j  o d t i j  n p t d |   qc qc Wd S(   s  
        Create a new context-free grammar, from the given start state
        and set of C{WeightedProduction}s.

        @param start: The start symbol
        @type start: L{Nonterminal}
        @param productions: The list of productions that defines the grammar
        @type productions: C{list} of C{Production}
        @raise ValueError: if the set of productions with any left-hand-side
            do not have probabilities that sum to a value within
            EPSILON of 1.
        i    i   s"   Productions for %r do not sum to 1N(	   R=   R   R`   R4   R<   t   itemsR   t   EPSILONR^   (   R   RF   RG   t   probsR|   R4   Rk   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s      (   R    R!   R"   R   R   (    (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s   c         C   s   h  } h  } xN | D]F } | i  | i   d  d | | i   <| i  | d  d | | <q Wg  } | D]@ } | t | i   | i   d t | |  | | i   qh ~ } t |  |  S(   s  
    Induce a PCFG grammar from a list of productions.

    The probability of a production A -> B C in a PCFG is:

    |                count(A -> B C)
    |  P(B, C | A) = ---------------       where * is any right hand side
    |                 count(A -> *)

    @param start: The start symbol
    @type start: L{Nonterminal}
    @param productions: The list of productions that defines the grammar
    @type productions: C{list} of L{Production}
    i    i   R<   (   R`   R4   R;   R   t   floatR   (   RF   RG   t   pcountt   lcountR?   R(   Rk   Rs   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   induce_pcfg  s     &Mc         C   s   t  |  t  S(   s<   
    @return: a C{list} of context-free L{Production}s.
    (   t   parse_productiont   standard_nonterm_parser(   t   input(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   parse_cfg_production  s    c         C   s"   t  |  t  \ } } t | |  S(   s   
    @return: a L{ContextFreeGrammar}.
    
    @param input: a grammar, either in the form of a string or else 
    as a list of strings.
    (   t   parse_grammarR   R=   (   R   RF   RG   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt	   parse_cfg  s    c         C   s   t  |  t d t S(   s<   
    @return: a C{list} of PCFG L{WeightedProduction}s.
    t   probabilistic(   R   R   RS   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   parse_pcfg_production  s    c         C   s(   t  |  t d t \ } } t | |  S(   s   
    @return: a probabilistic L{WeightedGrammar}.
    
    @param input: a grammar, either in the form of a string or else 
    as a list of strings.
    R   (   R   R   RS   R   (   R   RF   RG   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt
   parse_pcfg  s    c         C   s   t  |  |  S(   s=   
    @return: a C{list} of feature-based L{Production}s.
    (   R   (   R   t   fstruct_parser(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   parse_fcfg_production  s    c         C   s   | d j o t t f } n | d j o t | t d | } n | d j	 o t d   n t |  | i  \ } } t | |  S(   s  
    @return: a feature structure based L{FeatureGrammar}.
    
    @param input: a grammar, either in the form of a string or else 
    as a list of strings.
    @param features: a tuple of features (default: SLASH, TYPE)
    @param logic_parser: a parser for lambda-expressions 
                         (default: LogicParser())
    @param fstruct_parser: a feature structure parser 
                           (only if features and logic_parser is None)
    t   logic_parsers8   'logic_parser' and 'fstruct_parser' must not both be setN(	   R}   R   R   R   R+   t	   ExceptionR   t   partial_parseR   (   R   t   featuresR   R   RF   RG   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt
   parse_fcfg  s    s   Use nltk.parse_fcfg() instead.c         C   s
   t  |   S(   N(   R   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   parse_featcfg1  s    s
   \s* -> \s*s   ( \[ [\d\.]+ \] ) \s*s   ( "[^"]+" | \'[^\']+\' ) \s*s   \| \s*c         C   sH  d } | |  |  \ } } t  i |  |  } | p t d   n | i   } d g } g  g } xr| t |   j  o^t i |  |  } | oc | o\ | i   } t | i d  d d ! | d <| d d j o t d | d f   qqe |  | d j oZ t i |  |  } | p t d	   n | d i	 | i d  d d ! | i   } qe |  | d
 j o< t
 i |  |  } | i	 d  | i	 g   | i   } qe | |  |  \ } } | d i	 |  qe W| o> g  }	 t | |  D]" \ }
 } |	 t | |
 d | q~	 Sg  } | D] }
 | t | |
  q'~ Sd S(   sX   
    Parse a grammar rule, given as a string, and return
    a list of productions.
    i    s   Expected an arrowg        i   ig      ?s9   Production probability %f, should not be greater than 1.0s   '"s   Unterminated stringt   |R<   N(   t	   _ARROW_REt   matchR^   t   endR   t   _PROBABILITY_RER   t   groupt   _TERMINAL_RERM   t   _DISJUNCTION_REt   zipR;   R-   (   t   linet   nonterm_parserR   t   posR4   t   mt   probabilitiest   rhsidest   nontermR(   R   t   probabilityRt   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   <  sD     		   !7c      	   C   s  t  |  t  o |  i d  } n |  } d } g  } d } xTt |  D]F\ } } | | i   } | i d  p | d j o qH n | i d  o | d  i   d } qH n d } y | d d j ou | d	 i d d	  \ }	 }
 |	 d
 j o< | |
 d  \ } } | t	 |
  j o t
 d   q>qXt
 d   n | t | | |  7} WqH t
 j
 o& } t
 d | d	 | | f   qH XqH W| p t
 d  n | p | d i   } n | | f S(   s  
    @return: a pair of 
      - a starting category 
      - a list of C{Production}s
    
    @param input: a grammar, either in the form of a string or else 
        as a list of strings.
    @param nonterm_parser: a function for parsing nonterminals.
        It should take a C{(string,position)} as argument and 
        return a C{(nonterminal,position)} as result. 
    @param probabilistic: are the grammar rules probabilistic?
    s   
t    t   #s   \it    i    t   %i   RF   s   Bad argument to start directives   Bad directives   Unable to parse line %s: %s
%ss   No productions found!N(   R   R.   R$   R}   t	   enumerateR%   t
   startswitht   endswitht   rstripR   R^   R   R4   (   R   R   R   t   linesRF   RG   t   continue_linet   linenumR   t	   directivet   argsR   t   e(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR   s  s@       s   ( [\w/]+ ) \s*c         C   sP   t  i |  |  } | p t d |  |   n t | i d   | i   f S(   Ns   Expected a nonterminal, found: i   (   t   _STANDARD_NONTERM_RER   R^   R   R   R   (   t   stringR   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s
     sH  ^\s*                # leading whitespace
                              ('[^']+')\s*        # single-quoted lhs
                              (?:[-=]+>)\s*        # arrow
                              (?:(                 # rhs:
                                   "[^"]+"         # doubled-quoted terminal
                                 | '[^']+'         # single-quoted terminal
                                 | \|              # disjunction
                                 )
                                 \s*)              # trailing space
                                 *$s"   ('[^']'|[-=]+>|"[^"]+"|'[^']+'|\|)c         C   s   g  } x t  |  i d   D]y \ } } | i   } | i d  p | d j o q n y | t |  7} Wq t j
 o t d | | f  q Xq Wt |  d j o t d  n t |  S(   Ns   
R   R   s   Unable to parse line %s: %si    s   No productions found!(   R   R$   R%   R   t   parse_dependency_productionR^   R   R   (   R)   RG   R   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   parse_dependency_grammar  s       c   
      C   s   t  i |   p t d  n t i |   } g  } t |  D]( \ } } | d d j o | | q= q= ~ } | d i d  } g  g } xJ | d D]> } | d j o | i g   q | d i | i d   q Wg  } | D] }	 | t | |	  q ~ S(   Ns   Bad production stringi   i   i    s   '"R   i(	   t   _PARSE_DG_RER   R^   t   _SPLIT_DG_RER$   R   R%   RM   R:   (
   R)   t   piecesR(   t   iRk   t   lhsideR   t   pieceRt   t   rhside(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyR     s    B	 c          C   s  d d k  l }  l } l } |  d  \ } } } } |  d  \ } } }	 }
 | | } d G| | | | | | |	 |
 | | g	 GHd G| i   GHH| | | g  GH| d  } d G| GHd	 G| i   GHd
 G| i   i d d d d  GHHd GH| i d d g  GH| i d d g  GHd S(   sU   
    A demonstration showing how C{ContextFreeGrammar}s can be created and used.
    i(   R*   R-   R   s   S, NP, VP, PPs   N, V, P, Dets   Some nonterminals:s       S.symbol() =>s   
      S -> NP VP
      PP -> P NP
      NP -> Det N | NP PP
      VP -> V NP | VP PP
      Det -> 'a' | 'the'
      N -> 'dog' | 'cat'
      V -> 'chased' | 'sat'
      P -> 'on' | 'in'
    s
   A Grammar:s       grammar.start()       =>s       grammar.productions() =>R#   s   ,
R   i   s%   Coverage of input words by a grammar:t   at   dogt   toyN(	   t   nltkR*   R-   R   R   RF   RG   t   replaceRj   (   R*   R-   R   t   St   NPt   VPt   PPt   Nt   Vt   Pt   Dett   VP_slash_NPt   grammar(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   cfg_demo  s$    
(		
 s7  
    S -> NP VP [1.0]
    NP -> Det N [0.5] | NP PP [0.25] | 'John' [0.1] | 'I' [0.15]
    Det -> 'the' [0.8] | 'my' [0.2]
    N -> 'man' [0.5] | 'telescope' [0.5]
    VP -> VP PP [0.1] | V NP [0.7] | V [0.2]
    V -> 'ate' [0.35] | 'saw' [0.65]
    PP -> P NP [1.0]
    P -> 'with' [0.61] | 'under' [0.39]
    s  
    S    -> NP VP         [1.0]
    VP   -> V NP          [.59]
    VP   -> V             [.40]
    VP   -> VP PP         [.01]
    NP   -> Det N         [.41]
    NP   -> Name          [.28]
    NP   -> NP PP         [.31]
    PP   -> P NP          [1.0]
    V    -> 'saw'         [.21]
    V    -> 'ate'         [.51]
    V    -> 'ran'         [.28]
    N    -> 'boy'         [.11]
    N    -> 'cookie'      [.12]
    N    -> 'table'       [.13]
    N    -> 'telescope'   [.14]
    N    -> 'hill'        [.5]
    Name -> 'Jack'        [.52]
    Name -> 'Bob'         [.48]
    P    -> 'with'        [.61]
    P    -> 'under'       [.39]
    Det  -> 'the'         [.41]
    Det  -> 'a'           [.31]
    Det  -> 'my'          [.28]
    c       
   C   s  d d k  l }  d d k l } d d k l } d d k l } t i   } | d } d G| GHd G| i	   GHd	 G| i
   GHd
 G| i   GHHt } d G| GHd G| i   GHd G| i   i d d d d  GHHd GH| i d d g  GH| i d d g  GHd GHg  } x_ |  i d  D]P } xG |  i |  D]6 }	 |	 i d t  |	 i d d  | |	 i   7} q2WqWt d  }
 | |
 |  } | GHHd GH| i |  } | i d  |  i d  d i   } | GHx | i |  D] } | GHqWd S(   sR   
    A demonstration showing how C{WeightedGrammar}s can be created and used.
    i(   t   treebank(   t   treetransforms(   R   (   t   pcharti   s   A PCFG production:s       pcfg_prod.lhs()  =>s       pcfg_prod.rhs()  =>s       pcfg_prod.prob() =>s   A PCFG grammar:s       grammar.start()       =>s       grammar.productions() =>R#   s   ,
R   i   s%   Coverage of input words by a grammar:R   t   boyt   girls'   Induce PCFG grammar from treebank data:t   collapsePOSt
   horzMarkovR   s%   Parse sentence using induced grammar:i   s   wsj_0001.mrgi    N(   t   nltk.corpusR   R   R   R   t
   nltk.parseR   t	   toy_pcfg1RG   R4   R   R<   t	   toy_pcfg2RF   R   Rj   R   t   parsed_sentst   collapse_unaryR   t   chomsky_normal_formR   t   InsideChartParsert   tracet   leavest   nbest_parse(   R   R   R   R   t
   pcfg_prodst	   pcfg_prodR   RG   R   t   treeR   t   parsert   sentt   parse(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt	   pcfg_demo,  sR    


    c          C   s(   d d  k  }  |  i i d  } | GHHd  S(   Nis!   grammars/book_grammars/feat0.fcfg(   t	   nltk.datat   datat   load(   R   t   g(    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt	   fcfg_demoi  s    c          C   s   t  d  }  |  GHd S(   s]   
    A demonstration showing the creation and inspection of a 
    C{DependencyGrammar}.
    sP   
    'scratch' -> 'cats' | 'walls'
    'walls' -> 'the'
    'cats' -> 'the'
    N(   R   (   R   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   dg_demoo  s    	c          C   s'   t  d  }  |  i   } | i   GHd S(   sh   
    A demonstration of how to read a string representation of 
    a CoNLL format dependency tree.
    sg  
    1   Ze                ze                Pron  Pron  per|3|evofmv|nom                 2   su      _  _
    2   had               heb               V     V     trans|ovt|1of2of3|ev             0   ROOT    _  _
    3   met               met               Prep  Prep  voor                             8   mod     _  _
    4   haar              haar              Pron  Pron  bez|3|ev|neut|attr               5   det     _  _
    5   moeder            moeder            N     N     soort|ev|neut                    3   obj1    _  _
    6   kunnen            kan               V     V     hulp|ott|1of2of3|mv              2   vc      _  _
    7   gaan              ga                V     V     hulp|inf                         6   vc      _  _
    8   winkelen          winkel            V     V     intrans|inf                      11  cnj     _  _
    9   ,                 ,                 Punc  Punc  komma                            8   punct   _  _
    10  zwemmen           zwem              V     V     intrans|inf                      11  cnj     _  _
    11  of                of                Conj  Conj  neven                            7   vc      _  _
    12  terrassen         terras            N     N     soort|mv|neut                    11  cnj     _  _
    13  .                 .                 Punc  Punc  punt                             12  punct   _  _
    N(   t   DependencyGraphR   t   pprint(   t   dgR   (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   sdg_demo{  s    	c           C   s'   t    t   t   t   t   d  S(   N(   R   R   R  R  R
  (    (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   demo  s
    t   __main__R*   R   R   R   R   R   R   R   R   R   R   R   R  R   R   R  R
  R   R   (>   R"   t   ret   nltk.internalsR    t   nltk.compatt   nltk.probabilityR   t   nltk.featstructR   R   R   R   R   t   objectR   R*   R+   R-   R:   R;   R=   R~   R   R   R   R   R   R   R   R   R   R   R   R   R}   R   R   t   compilet   VERBOSER   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R  R  R
  R  R    t   __all__(    (    (    s)   /home/andreas/coglang/dop1/dop_grammar.pyt   <module>F   s   
(j	r,tP46	&					72					)				=							