
B_Kc           @   sj  d  Z  d d k l Z d d k l Z l Z l Z d d k l Z d d k	 Td d k
 Z
 d d k Z 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 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 e f d     YZ d e f d     YZ d e e f d      YZ d! e f d"     YZ 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/ d0  Z" e   e   e   e   g Z# e   e   e   e   g Z$ e   e   e   e   g Z% e   e!   e    g Z& d1 e' f d2     YZ( d3 e( f d4     YZ) d5 e( f d6     YZ* d7 e( f d8     YZ+ d9 e( f d:     YZ, d; e( f d<     YZ- d=   Z. e/ e0 e1 e0 d> d? d@ dA  Z2 e3 dB j o e2   n d S(C   s  
Data classes and parser implementations for "chart parsers", which
use dynamic programming to efficiently parse a text.  A X{chart
parser} derives parse trees for a text by iteratively adding "edges"
to a "chart."  Each X{edge} represents a hypothesis about the tree
structure for a subsequence of the text.  The X{chart} is a
"blackboard" for composing and combining these hypotheses.

When a chart parser begins parsing a text, it creates a new (empty)
chart, spanning the text.  It then incrementally adds new edges to the
chart.  A set of X{chart rules} specifies the conditions under which
new edges should be added to the chart.  Once the chart reaches a
stage where none of the chart rules adds any new edges, parsing is
complete.

Charts are encoded with the L{Chart} class, and edges are encoded with
the L{TreeEdge} and L{LeafEdge} classes.  The chart parser module
defines three chart parsers:

  - C{ChartParser} is a simple and flexible chart parser.  Given a
    set of chart rules, it will apply those rules to the chart until
    no more edges are added.

  - C{SteppingChartParser} is a subclass of C{ChartParser} that can
    be used to step through the parsing process.
i(   t   Tree(   t   WeightedGrammart   is_nonterminalt   is_terminal(   t   defaultdict(   t   *Nt   EdgeIc           B   s   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 d   Z RS(   sM  
    A hypothesis about the structure of part of a sentence.
    Each edge records the fact that a structure is (partially)
    consistent with the sentence.  An edge contains:

        - A X{span}, indicating what part of the sentence is
          consistent with the hypothesized structure.
          
        - A X{left-hand side}, specifying what kind of structure is
          hypothesized.

        - A X{right-hand side}, specifying the contents of the
          hypothesized structure.

        - A X{dot position}, indicating how much of the hypothesized
          structure is consistent with the sentence.

    Every edge is either X{complete} or X{incomplete}:

      - An edge is X{complete} if its structure is fully consistent
        with the sentence.

      - An edge is X{incomplete} if its structure is partially
        consistent with the sentence.  For every incomplete edge, the
        span specifies a possible prefix for the edge's structure.
    
    There are two kinds of edge:

        - C{TreeEdges<TreeEdge>} record which trees have been found to
          be (partially) consistent with the text.
          
        - C{LeafEdges<leafEdge>} record the tokens occur in the text.

    The C{EdgeI} interface provides a common interface to both types
    of edge, allowing chart parsers to treat them in a uniform manner.
    c         C   s$   |  i  t j o t d   n d  S(   Ns   Edge is an abstract interface(   t	   __class__R   t	   TypeError(   t   self(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   __init__[   s    c         C   s   t  d   d S(   s   
        @return: A tuple C{(s,e)}, where C{subtokens[s:e]} is the
            portion of the sentence that is consistent with this
            edge's structure.
        @rtype: C{(int, int)}
        s   EdgeI is an abstract interfaceN(   t   AssertionError(   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   spanc   s    c         C   s   t  d   d S(   sV   
        @return: The start index of this edge's span.
        @rtype: C{int}
        s   EdgeI is an abstract interfaceN(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   startl   s    c         C   s   t  d   d S(   sT   
        @return: The end index of this edge's span.
        @rtype: C{int}
        s   EdgeI is an abstract interfaceN(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   ends   s    c         C   s   t  d   d S(   sQ   
        @return: The length of this edge's span.
        @rtype: C{int}
        s   EdgeI is an abstract interfaceN(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   lengthz   s    c         C   s   t  d   d S(   s   
        @return: This edge's left-hand side, which specifies what kind
            of structure is hypothesized by this edge.
        @see: L{TreeEdge} and L{LeafEdge} for a description of
            the left-hand side values for each edge type.
        s   EdgeI is an abstract interfaceN(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   lhs   s    c         C   s   t  d   d S(   s  
        @return: This edge's right-hand side, which specifies
            the content of the structure hypothesized by this
            edge.
        @see: L{TreeEdge} and L{LeafEdge} for a description of
            the right-hand side values for each edge type.
        s   EdgeI is an abstract interfaceN(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   rhs   s    c         C   s   t  d   d S(   s!  
        @return: This edge's dot position, which indicates how much of
            the hypothesized structure is consistent with the
            sentence.  In particular, C{self.rhs[:dot]} is consistent
            with C{subtoks[self.start():self.end()]}.
        @rtype: C{int}
        s   EdgeI is an abstract interfaceN(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   dot   s    c         C   s   t  d   d S(   s   
        @return: The element of this edge's right-hand side that
            immediately follows its dot.
        @rtype: C{Nonterminal} or X{terminal} or C{None}
        s   EdgeI is an abstract interfaceN(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   next   s    c         C   s   t  d   d S(   s   
        @return: True if this edge's structure is fully consistent
            with the text.
        @rtype: C{boolean}
        s   EdgeI is an abstract interfaceN(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   is_complete   s    c         C   s   t  d   d S(   s   
        @return: True if this edge's structure is partially consistent
            with the text.
        @rtype: C{boolean}
        s   EdgeI is an abstract interfaceN(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   is_incomplete   s    c         C   s   t  d   d  S(   Ns   EdgeI is an abstract interface(   R   (   R	   t   other(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   __cmp__   s    c         C   s   t  d   d  S(   Ns   EdgeI is an abstract interface(   R   (   R	   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   __hash__   s    (   t   __name__t
   __module__t   __doc__R
   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   6   s   $								
	
				t   TreeEdgec           B   s   e  Z d  Z d d  Z d   Z e 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 d   Z d   Z RS(   se  
    An edge that records the fact that a tree is (partially)
    consistent with the sentence.  A tree edge consists of:

        - A X{span}, indicating what part of the sentence is
          consistent with the hypothesized tree.
          
        - A X{left-hand side}, specifying the hypothesized tree's node
          value.

        - A X{right-hand side}, specifying the hypothesized tree's
          children.  Each element of the right-hand side is either a
          terminal, specifying a token with that terminal as its leaf
          value; or a nonterminal, specifying a subtree with that
          nonterminal's symbol as its node value.

        - A X{dot position}, indicating which children are consistent
          with part of the sentence.  In particular, if C{dot} is the
          dot position, C{rhs} is the right-hand size, C{(start,end)}
          is the span, and C{sentence} is the list of subtokens in the
          sentence, then C{subtokens[start:end]} can be spanned by the
          children specified by C{rhs[:dot]}.

    For more information about edges, see the L{EdgeI} interface.
    i    c         C   s.   | |  _  t |  |  _ | |  _ | |  _ d S(   s  
        Construct a new C{TreeEdge}.
        
        @type span: C{(int, int)}
        @param span: A tuple C{(s,e)}, where C{subtokens[s:e]} is the
            portion of the sentence that is consistent with the new
            edge's structure.
        @type lhs: L{Nonterminal}
        @param lhs: The new edge's left-hand side, specifying the
            hypothesized tree's node value.
        @type rhs: C{list} of (L{Nonterminal} and C{string})
        @param rhs: The new edge's right-hand side, specifying the
            hypothesized tree's children.
        @type dot: C{int}
        @param dot: The position of the new edge's dot.  This position
            specifies what prefix of the production's right hand side
            is consistent with the text.  In particular, if
            C{sentence} is the list of subtokens in the sentence, then
            C{subtokens[span[0]:span[1]]} can be spanned by the
            children specified by C{rhs[:dot]}.
        N(   t   _lhst   tuplet   _rhst   _spant   _dot(   R	   R   R   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR
      s    		c      	   C   s1   t  d | | f d |  i   d |  i   d d  S(   s)  
        @return: A new C{TreeEdge} formed from the given production.
            The new edge's left-hand side and right-hand side will
            be taken from C{production}; its span will be 
            C{(index,index)}; and its dot position will be C{0}.
        @rtype: L{TreeEdge}
        R   R   R   R   i    (   R   R   R   (   t
   productiont   index(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   from_production   s    c      
   C   s9   t  d |  i d | f d |  i d |  i d |  i d  S(   s%  
        @return: A new C{TreeEdge} formed from this edge.
            The new edge's dot position is increased by C{1}, 
            and its end index will be replaced by C{new_end}.
        @rtype: L{TreeEdge}
        @param new_end: The new end index.
        @type new_end: C{int}
        R   i    R   R   R   i   (   R   R    R   R   R!   (   R	   t   new_end(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   move_dot_forward	  s    	c         C   s   |  i  S(   N(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s   |  i  S(   N(   R    (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s   |  i  d S(   Ni    (   R    (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s   |  i  d S(   Ni   (   R    (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s   |  i  d |  i  d S(   Ni   i    (   R    (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s   |  i  S(   N(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s   |  i  S(   N(   R!   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s   |  i  t |  i  j S(   N(   R!   t   lenR   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s   |  i  t |  i  j S(   N(   R!   R'   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s3   |  i  t |  i  j o d  Sn |  i |  i  Sd  S(   N(   R!   R'   R   t   None(   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR      s    !c         C   sd   |  i  | i  j o d Sn t |  i |  i   |  i   |  i f | i | i   | i   | i f  S(   Ni(   R   t   cmpR    R   R   R!   (   R	   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   %  s    $c         C   s(   t  |  i   |  i   |  i |  i f  S(   N(   t   hashR   R   R    R!   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   )  s    c         C   s   d |  i  d |  i  d f } | d |  i f 7} xS t t |  i   D]< } | |  i j o | d 7} n | d |  i | f 7} qH Wt |  i  |  i j o | d 7} n | S(   Ns   [%s:%s] i    i   s   %-2r ->s    *s    %r(   R    R   t   rangeR'   R   R!   (   R	   t   strt   i(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   __str__-  s     'c         C   s   d |  S(   Ns
   [Edge: %s](    (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   __repr__7  s    (   R   R   R   R
   R$   t   staticmethodR&   R   R   R   R   R   R   R   R   R   R   R   R   R.   R/   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR      s&   	
														
t   LeafEdgec           B   s   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 d   Z d   Z d   Z RS(   s  
    An edge that records the fact that a leaf value is consistent with
    a word in the sentence.  A leaf edge consists of:

      - An X{index}, indicating the position of the word.
      - A X{leaf}, specifying the word's content.

    A leaf edge's left-hand side is its leaf value, and its right hand
    side is C{()}.  Its span is C{[index, index+1]}, and its dot
    position is C{0}.
    c         C   s   | |  _  | |  _ d S(   s  
        Construct a new C{LeafEdge}.

        @param leaf: The new edge's leaf value, specifying the word
            that is recorded by this edge.
        @param index: The new edge's index, specifying the position of
            the word that is recorded by this edge.
        N(   t   _leaft   _index(   R	   t   leafR#   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR
   F  s    		c         C   s   |  i  S(   N(   R2   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   S  s    c         C   s   |  i  |  i  d f S(   Ni   (   R3   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   T  s    c         C   s   |  i  S(   N(   R3   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   U  s    c         C   s   |  i  d S(   Ni   (   R3   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   V  s    c         C   s   d S(   Ni   (    (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   W  s    c         C   s   d S(   N(    (    (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   X  s    c         C   s   d S(   Ni    (    (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   Y  s    c         C   s   t  S(   N(   t   True(   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   Z  s    c         C   s   t  S(   N(   t   False(   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   [  s    c         C   s   d  S(   N(   R(   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   \  s    c         C   s=   t  | t  p d Sn t |  i |  i f | i | i f  S(   Ni(   t
   isinstanceR1   R)   R3   R2   (   R	   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   _  s    c         C   s   t  |  i |  i f  S(   N(   R*   R3   R2   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   b  s    c         C   s   d |  i  |  i  d |  i f S(   Ns
   [%s:%s] %ri   (   R3   R2   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR.   f  s    c         C   s   d |  S(   Ns
   [Edge: %s](    (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR/   h  s    (   R   R   R   R
   R   R   R   R   R   R   R   R   R   R   R   R   R.   R/   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR1   :  s    														t   Chartc           B   s   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 e	 Z
 d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z e d  Z e e d  Z d   Z d   Z d   Z e d  Z e d  Z e d  Z d   Z RS(   s  
    A blackboard for hypotheses about the syntactic constituents of a
    sentence.  A chart contains a set of edges, and each edge encodes
    a single hypothesis about the structure of some portion of the
    sentence.

    The L{select} method can be used to select a specific collection
    of edges.  For example C{chart.select(is_complete=True, start=0)}
    yields all complete edges whose start indices are 0.  To ensure
    the efficiency of these selection operations, C{Chart} dynamically
    creates and maintains an index for each set of attributes that
    have been selected on.

    In order to reconstruct the trees that are represented by an edge,
    the chart associates each edge with a set of child pointer lists.
    A X{child pointer list} is a list of the edges that license an
    edge's right-hand side.

    @ivar _tokens: The sentence that the chart covers.
    @ivar _num_leaves: The number of tokens.
    @ivar _edges: A list of the edges in the chart
    @ivar _edge_to_cpls: A dictionary mapping each edge to a set
        of child pointer lists that are associated with that edge.
    @ivar _indexes: A dictionary mapping tuples of edge attributes
        to indices, where each index maps the corresponding edge
        attribute values to lists of edges.
    c         C   s/   t  |  |  _ t |  i  |  _ |  i   d S(   s   
        Construct a new chart. The chart is initialized with the 
        leaf edges corresponding to the terminal leaves.

        @type tokens: L{list}
        @param tokens: The sentence that this chart will be used to parse.
        N(   R   t   _tokensR'   t   _num_leavest
   initialize(   R	   t   tokens(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR
     s    	c         C   s   g  |  _  h  |  _ h  |  _ d S(   s"   
        Clear the chart.
        N(   t   _edgest   _edge_to_cplst   _indexes(   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR;     s    		c         C   s   |  i  S(   s_   
        @return: The number of words in this chart's sentence.
        @rtype: C{int}
        (   R:   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt
   num_leaves  s    c         C   s   |  i  | S(   sc   
        @return: The leaf value of the word at the given index.
        @rtype: C{string}
        (   R9   (   R	   R#   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR4     s    c         C   s   |  i  S(   s   
        @return: A list of the leaf values of each word in the
            chart's sentence.
        @rtype: C{list} of C{string}
        (   R9   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   leaves  s    c         C   s   |  i  S(   s  
        @return: A list of all edges in this chart.  New edges
            that are added to the chart after the call to edges()
            will I{not} be contained in this list.
        @rtype: C{list} of L{EdgeI}
        @see: L{iteredges}, L{select}
        (   R=   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   edges  s    c         C   s   t  |  i  S(   s0  
        @return: An iterator over the edges in this chart.  It is 
            I{not} guaranteed that new edges which are added to the 
            chart before the iterator is exhausted will also be  
            generated.
        @rtype: C{iter} of L{EdgeI}
        @see: L{edges}, L{select}
        (   t   iterR=   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt	   iteredges  s    	c         C   s   t  |  i  S(   s^   
        @return: The number of edges contained in this chart.
        @rtype: C{int}
        (   R'   R>   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt	   num_edges  s    c            s     h  j o t  |  i  Sn   i   } | i   t |  } | |  i j o |  i |  n t   f d   | D  } t  |  i | i | g    S(   sP  
        @return: An iterator over the edges in this chart.  Any
            new edges that are added to the chart before the iterator
            is exahusted will also be generated.  C{restrictions}
            can be used to restrict the set of edges that will be
            generated.
        @rtype: C{iter} of L{EdgeI}

        @kwarg span: Only generate edges C{e} where C{e.span()==span}
        @kwarg start: Only generate edges C{e} where C{e.start()==start}
        @kwarg end: Only generate edges C{e} where C{e.end()==end}
        @kwarg length: Only generate edges C{e} where C{e.length()==length}
        @kwarg lhs: Only generate edges C{e} where C{e.lhs()==lhs}
        @kwarg rhs: Only generate edges C{e} where C{e.rhs()==rhs}
        @kwarg next: Only generate edges C{e} where C{e.next()==next}
        @kwarg dot: Only generate edges C{e} where C{e.dot()==dot}
        @kwarg is_complete: Only generate edges C{e} where
            C{e.is_complete()==is_complete}
        @kwarg is_incomplete: Only generate edges C{e} where
            C{e.is_incomplete()==is_incomplete}
        c         3   s   x |  ] }   | Vq Wd  S(   N(    (   t   .0t   key(   t   restrictions(    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pys	   <genexpr>  s    (   RC   R=   t   keyst   sortR   R?   t
   _add_indext   get(   R	   RH   t
   restr_keyst   vals(    (   RH   s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   select  s    
c            s   x/ | D]' } t  t |  p t d |  q q Wh  } |  i | <xF |  i D];   t   f d   | D  } | i | g   i    qM Wd S(   s   
        A helper function for L{select}, which creates a new index for
        a given set of attributes (aka restriction keys).
        s   Bad restriction: %sc         3   s%   x |  ] } t    |    Vq Wd  S(   N(   t   getattr(   RF   RG   (   t   edge(    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pys	   <genexpr>  s    N(   t   hasattrR   t
   ValueErrorR?   R=   R   t
   setdefaultt   append(   R	   RM   RG   R#   RN   (    (   RQ   s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyRK   
  s     
 c            sY   xR |  i  i   D]A \ } } t   f d   | D  } | i | g   i    q Wd S(   sr   
        A helper function for L{insert}, which registers the new
        edge with all existing indexes.
        c         3   s%   x |  ] } t    |    Vq Wd  S(   N(   RP   (   RF   RG   (   RQ   (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pys	   <genexpr>"  s    N(   R?   t   itemsR   RT   RU   (   R	   RQ   RM   R#   RN   (    (   RQ   s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   _register_with_indexes  s     c         C   sG   |  i  |  } g  } | D] } | | | f q ~ } |  i | |  S(   sT   
        Add a new edge to the chart, using a pointer to the previous edge.
        (   t   child_pointer_listst   insert(   R	   t   new_edget   previous_edget
   child_edget   cplst   _[1]t   cplt   new_cpls(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   insert_with_backpointer)  s    (c         G   s   | |  i  j o |  i |  |  i |  n |  i  i | h   } t } x; | D]3 } t |  } | | j o t | | <t } qP qP W| S(   s  
        Add a new edge to the chart.

        @type edge: L{EdgeI}
        @param edge: The new edge
        @type child_pointer_lists: C(sequence} of C{tuple} of L{EdgeI} 
        @param child_pointer_lists: A sequence of lists of the edges that 
            were used to form this edge.  This list is used to reconstruct 
            the trees (or partial trees) that are associated with C{edge}.
        @rtype: C{bool}
        @return: True if this operation modified the chart.  In
            particular, return true iff the chart did not already
            contain C{edge}, or if it did not already associate
            C{child_pointer_lists} with C{edge}.
        (   R>   t   _append_edgeRW   RT   R6   R   R5   (   R	   RQ   RX   R]   t   chart_was_modifiedt   child_pointer_list(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyRY   1  s     
c         C   s   |  i  i |  d  S(   N(   R=   RU   (   R	   RQ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyRb   S  s    c         C   sU   g  } xH |  i  d d d |  i d |  D]% } | |  i | d | d t 7} q( W| S(   s   
        @return: A list of the complete tree structures that span
        the entire chart, and whose root node is C{root}.
        R   i    R   R   t
   tree_classt   complete(   RO   R:   t   treesR5   (   R	   t   rootRe   Rg   RQ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   parsesZ  s
    " #c         C   s   |  i  | | d h  d | S(   s>  
        @return: A list of the tree structures that are associated
        with C{edge}.

        If C{edge} is incomplete, then the unexpanded children will be
        encoded as childless subtrees, whose node value is the
        corresponding terminal or nonterminal.
            
        @rtype: C{list} of L{Tree}
        @note: If two trees share a common subtree, then the same
            C{Tree} may be used to encode that subtree in
            both trees.  If you need to eliminate this subtree
            sharing, then create a deep copy of each tree.
        t   memoRe   (   t   _trees(   R	   RQ   Re   Rf   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyRg   d  s    c      
   C   s  | | j o | | Sn g  } | o | i    o | Sn g  | | <t | t  o( |  i | i   } | | | <| g Sn x |  i |  D]{ } g  } | D] }	 | |  i |	 | | |  q ~ }
 x? |  i |
  D]. } | i   i	   } | i
 | | |   q Wq W| i    o\ g  } | i   | i   D] } | | | g   q4~ } x | D] } | i |  qZWn | | | <| S(   s   
        A helper function for L{trees}.
        @param memo: A dictionary used to record the trees that we've
            generated for each edge, so that when we see an edge more
            than once, we can reuse the same trees.
        (   R   R7   R1   R9   R   RX   Rk   t   _choose_childrenR   t   symbolRU   R   R   t   extend(   R	   RQ   Rf   Rj   Re   Rg   R4   R_   R^   t   cpt   child_choicest   childrenR   t   _[2]t   eltt
   unexpandedt   tree(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyRk   u  s4    

 , 3 
c         C   s   g  g } x | D] } t  | d  oJ t | t  o9 g  } | D]! } | D] } | | | g qL qB ~ } q g  } | D] } | | | g q{ ~ } q W| S(   sF  
        A helper function for L{_trees} that finds the possible sets
        of subtrees for a new tree.
        
        @param child_choices: A list that specifies the options for
        each child.  In particular, C{child_choices[i]} is a list of
        tokens and subtrees that can be used as the C{i}th child.
        t   __iter__(   RR   R7   t
   basestring(   R	   Rp   t   children_listst   child_choiceR^   t   childt
   child_listRr   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyRl     s    		 
(%c         C   s   |  i  i | h   i   S(   s   
        @rtype: C{list} of C{list} of C{EdgeI}
        @return: The set of child pointer lists for the given edge.
            Each child pointer list is a list of edges that have
            been used to form this edge.
        (   R>   RL   RI   (   R	   RQ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyRX     s    c         C   s  | t  j o d |  i   d } n | i   | i   } } d d d | d | } | | j o) | i   o | d 7} qV| d 7} n | i   oN | i   d |  i f j o2 | d	 d
 | | | d d
 | d d 7} nn | i   o2 | d	 d | | | d d | d d 7} n/ | d	 d | | | d d | d d 7} | d | d d |  i | 7} | d | S(   s   
        @return: A pretty-printed string representation of a given edge
            in this chart.
        @rtype: C{string}
        @param width: The number of characters allotted to each
            index in the sentence.
        i2   i   t   |t   .t    t   #t   >i    t   [t   =t   ]t   -s   | %s(   R(   R@   R   R   R   R   R:   (   R	   RQ   t   widthR   R   R,   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   pp_edge  s    %)22.!c         C   s   | t  j o d |  i   d } n |  i t  j	 oX | d j oK d } x4 |  i D]) } | | | d  i | d  d 7} qR W| d 7} n d } | S(   s   
        @return: A pretty-printed string representation of this
            chart's leaves.  This string can be used as a header
            for calls to L{pp_edge}.
        i2   i   s   |.R}   R|   t    (   R(   R@   R9   t   center(   R	   R   t   headert   tok(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt	   pp_leaves  s    %
 'c      	      s    t  j o d   i   d  n g  }   D]" } | | i   | i   | f q0 ~ } | i   g  } | D] \ } } } | | qp ~ }   i   d d i    f d   | D  S(   s   
        @return: A pretty-printed string representation of this chart.
        @rtype: C{string}
        @param width: The number of characters allotted to each
            index in the sentence.
        i2   i   s   
c         3   s%   x |  ] }  i  |    Vq Wd  S(   N(   R   (   RF   RQ   (   R   R	   (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pys	   <genexpr>  s    (   R(   R@   R   R   RJ   R   t   join(   R	   R   R^   t   eRB   Rr   t   _(    (   R	   R   s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   pp  s    %6
*c         C   s   d } | d 7} | d 7} | d 7} x t  |  i   d d  D] } | d j o | d 7} n x t  |  i   d  D]g } | d j p< | |  i | d i   j p | |  i | d i   j o | d	 | | f 7} qu qu Wq= W| d
 7} x t  |  i   d  D] } | d 7} x t  |  i   d  D]g } | d j p< | |  i | d i   j p | |  i | d i   j o | d | | f 7} q,q,W| d 7} qW| d 7} | d 7} | d 7} x; t  |  i    D]' } | d |  i |  | d f 7} qW| d 7} | d 7} x t |   D] \ } } x@ t  | i    D], } | d | | d | d | d f 7} qAW| d | i   | d | i   | d | f 7} xI t  | i   |  i    D], } | d | | d | d | d f 7} qWq"W| d 7} | S(   Ns   digraph nltk_chart {
s     rankdir=LR;
s     node [height=0.1,width=0.1];
s*     node [style=filled, color="lightgray"];
ii    s&     node [style=filled, color="black"];
i   s     %04d.%04d [label=""];
s/     x [style=invis]; x->0000.0000 [style=invis];
s     {rank=same;s
    %04d.%04ds   }
s"     edge [style=invis, weight=100];
s     node [shape=plaintext]
s     0000.0000s   ->%s->%04d.0000s   ;

s      edge [style=solid, weight=1];
s*     %04d.%04d -> %04d.%04d [style="invis"];
s'     %04d.%04d -> %04d.%04d [label="%s"];
(   R+   RE   R@   R=   R   R   R4   t	   enumerate(   R	   t   st   yt   xRQ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   dot_digraph  sZ    


  + 
 
 +


 %

  $+ (
(   R   R   R   R
   R;   R@   R4   RA   RB   RD   Rv   RE   RO   RK   RW   Ra   RY   Rb   R    Ri   R6   Rg   Rk   Rl   RX   R(   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR8   o  s2   						
			%				"	
	9		t
   ChartRuleIc           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s  
    A rule that specifies what new edges are licensed by any given set
    of existing edges.  Each chart rule expects a fixed number of
    edges, as indicated by the class variable L{NUM_EDGES}.  In
    particular:
    
      - A chart rule with C{NUM_EDGES=0} specifies what new edges are
        licensed, regardless of existing edges.

      - A chart rule with C{NUM_EDGES=1} specifies what new edges are
        licensed by a single existing edge.

      - A chart rule with C{NUM_EDGES=2} specifies what new edges are
        licensed by a pair of existing edges.
      
    @type NUM_EDGES: C{int}
    @cvar NUM_EDGES: The number of existing edges that this rule uses
        to license new edges.  Typically, this number ranges from zero
        to two.
    c         G   s   t  d  d S(   s  
        Add the edges licensed by this rule and the given edges to the
        chart.

        @type edges: C{list} of L{EdgeI}
        @param edges: A set of existing edges.  The number of edges
            that should be passed to C{apply} is specified by the
            L{NUM_EDGES} class variable.
        @rtype: C{list} of L{EdgeI}
        @return: A list of the edges that were added.
        s#   ChartRuleI is an abstract interfaceN(   R   (   R	   t   chartt   grammarRB   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   applye  s    c         G   s   t  d  d S(   s  
        @return: A generator that will add edges licensed by this rule
            and the given edges to the chart, one at a time.  Each
            time the generator is resumed, it will either add a new
            edge and yield that edge; or return.
        @rtype: C{iter} of L{EdgeI}
        
        @type edges: C{list} of L{EdgeI}
        @param edges: A set of existing edges.  The number of edges
            that should be passed to C{apply} is specified by the
            L{NUM_EDGES} class variable.
        s#   ChartRuleI is an abstract interfaceN(   R   (   R	   R   R   RB   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt
   apply_iters  s    c         C   s   t  d  d S(   s   
        Add all the edges licensed by this rule and the edges in the
        chart to the chart.
        
        @rtype: C{list} of L{EdgeI}
        @return: A list of the edges that were added.
        s#   ChartRuleI is an abstract interfaceN(   R   (   R	   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   apply_everywhere  s    c         C   s   t  d  d S(   sL  
        @return: A generator that will add all edges licensed by
            this rule, given the edges that are currently in the
            chart, one at a time.  Each time the generator is resumed,
            it will either add a new edge and yield that edge; or
            return.
        @rtype: C{iter} of L{EdgeI}
        s#   ChartRuleI is an abstract interfaceN(   R   (   R	   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   apply_everywhere_iter  s    	(   R   R   R   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   P  s
   			
t   AbstractChartRulec           B   s;   e  Z d  Z d   Z d   Z d   Z d   Z d   Z RS(   s  
    An abstract base class for chart rules.  C{AbstractChartRule}
    provides:
      - A default implementation for C{apply}, based on C{apply_iter}.
      - A default implementation for C{apply_everywhere_iter},
        based on C{apply_iter}.
      - A default implementation for C{apply_everywhere}, based on
        C{apply_everywhere_iter}.  Currently, this implementation
        assumes that C{NUM_EDGES}<=3.
      - A default implementation for C{__str__}, which returns a
        name basd on the rule's class name.
    c         G   s   t  d  d  S(   Ns&   AbstractChartRule is an abstract class(   R   (   R	   R   R   RB   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c      	   c   s]  |  i  d j o& xF|  i | |  D] } | Vq# Wn$|  i  d j o: x| D]+ } x" |  i | | |  D] } | Vqi WqM Wn |  i  d j oN x | D]? } x6 | D]. } x% |  i | | | |  D] } | Vq Wq Wq Wn| |  i  d j ob xh | D]S } xJ | D]B } x9 | D]1 } x( |  i | | | | |  D] } | Vq1WqWqWq Wn
 t d  d  S(   Ni    i   i   i   s&   NUM_EDGES>3 is not currently supported(   t	   NUM_EDGESR   R   (   R	   R   R   RZ   t   e1t   e2t   e3(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s:              c         G   s   t  |  i | | |   S(   N(   t   listR   (   R	   R   R   RB   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s   t  |  i | |   S(   N(   R   R   (   R	   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s   t  i d d |  i i  S(   Ns   ([a-z])([A-Z])s   \1 \2(   t   ret   subR   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR.     s    (   R   R   R   R   R   R   R   R.   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s   				t   FundamentalRulec           B   s   e  Z d  Z d Z d   Z RS(   s*  
    A rule that joins two adjacent edges to form a single combined
    edge.  In particular, this rule specifies that any pair of edges:
    
        - [A S{->} S{alpha} * B S{beta}][i:j]
        - [B S{->} S{gamma} *][j:k]
    licenses the edge:
        - [A S{->} S{alpha} B * S{beta}][i:j]
    i   c         c   s   | i    o< | i   o/ | i   | i   j o | i   | i   j p d  Sn | i | i    } | i | | |  o	 | Vn d  S(   N(   R   R   R   R   R   R   R&   Ra   (   R	   R   R   t	   left_edget
   right_edgeRZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    (   R   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s   	t   SingleEdgeFundamentalRulec           B   s/   e  Z d  Z d Z d   Z d   Z d   Z RS(   s  
    A rule that joins a given edge with adjacent edges in the chart,
    to form combined edges.  In particular, this rule specifies that
    either of the edges:
        - [A S{->} S{alpha} * B S{beta}][i:j]
        - [B S{->} S{gamma} *][j:k]
    licenses the edge:
        - [A S{->} S{alpha} B * S{beta}][i:j]
    if the other edge is already in the chart.
    
    @note: This is basically L{FundamentalRule}, with one edge left
        unspecified.
    i   c         c   sY   | i    o& xE |  i | |  D] } | Vq  Wn# x |  i | |  D] } | VqF Wd  S(   N(   R   t   _apply_incompletet   _apply_complete(   R	   R   R   RQ   RZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR      s      c         c   sm   xf | i  d | i   d t d | i    D]: } | i | i    } | i | | |  o	 | Vq+ q+ Wd  S(   NR   R   R   (   RO   R   R6   R   R&   R   Ra   (   R	   R   R   R   RZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s     c         c   sm   xf | i  d | i   d t d | i    D]: } | i | i    } | i | | |  o	 | Vq+ q+ Wd  S(   NR   R   R   (   RO   R   R5   R   R&   Ra   (   R	   R   R   R   RZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s     (   R   R   R   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s
   		t   LeafInitRulec           B   s   e  Z d  Z d   Z RS(   i    c         c   sU   xN t  | i    D]: } t | i |  |  } | i | d  o	 | Vq q Wd  S(   N(    (   R+   R@   R1   R4   RY   (   R	   R   R   R#   RZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s
     (   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s   t   TopDownInitRulec           B   s   e  Z d  Z d Z d   Z RS(   s*  
    A rule licensing edges corresponding to the grammar productions for
    the grammar's start symbol.  In particular, this rule specifies that:
        - [S S{->} * S{alpha}][0:i]
    is licensed for each grammar production C{S S{->} S{alpha}}, where
    C{S} is the grammar's start symbol.
    i    c         c   sU   xN | i  d | i    D]4 } t i | d  } | i | d  o	 | Vq q Wd  S(   NR   i    (    (   t   productionsR   R   R$   RY   (   R	   R   R   t   prodRZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   1  s
     (   R   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   (  s   t   CachedTopDownInitRulec           B   s   e  Z d  Z RS(   s   Use L{TopDownInitRule} instead.(   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   7  s   t   TopDownPredictRulec           B   s   e  Z d  Z d Z d   Z RS(   s  
    A rule licensing edges corresponding to the grammar productions
    for the nonterminal following an incomplete edge's dot.  In
    particular, this rule specifies that:
        - [A S{->} S{alpha} * B S{beta}][i:j]
    licenses the edge:
        - [B S{->} * S{gamma}][j:j]
    for each grammar production C{B S{->} S{gamma}}.
    
    @note: This rule corresponds to the Predictor Rule in Earley parsing.
    i   c         c   sp   | i    o d  Sn xT | i d | i    D]: } t i | | i    } | i | d  o	 | Vq. q. Wd  S(   NR   (    (   R   R   R   R   R$   R   RY   (   R	   R   R   RQ   R   RZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   G  s     (   R   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   :  s   t   TopDownExpandRulec           B   s   e  Z d  Z RS(   s   Use TopDownPredictRule instead(   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   N  s   t   CachedTopDownPredictRulec           B   s    e  Z d  Z d   Z d   Z RS(   s.  
    A cached version of L{TopDownPredictRule}.  After the first time
    this rule is applied to an edge with a given C{end} and C{next},
    it will not generate any more edges for edges with that C{end} and
    C{next}.
    
    If C{chart} or C{grammar} are changed, then the cache is flushed.
    c         C   s   t  i |   h  |  _ d  S(   N(   R   R
   t   _done(   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR
   Z  s    c   
      c   sT  | i    o d  Sn | i   | i   } } t | i    p d  Sn |  i i | | f d  } | d | j o | d | j o d  Sn x | i d |  D] } | i   o | i   d } t	 |  o4 | | i
   j p | | i |  j o q qn t i | |  }	 | i |	 d  o	 |	 Vq q W| | f |  i | | f <d  S(   Ni    i   R   (   NN(    (   R   R   R   R   R   RL   R(   R   R   R   R@   R4   R   R$   RY   (
   R	   R   R   RQ   R   R#   t   doneR   t   firstRZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   ^  s    * 4(   R   R   R   R
   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   Q  s   	t   CachedTopDownExpandRulec           B   s   e  Z d  Z RS(   s(   Use L{CachedTopDownPredictRule} instead.(   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   w  s   t   BottomUpPredictRulec           B   s   e  Z d  Z d Z d   Z RS(   sC  
    A rule licensing any edge corresponding to a production whose
    right-hand side begins with a complete edge's left-hand side.  In
    particular, this rule specifies that:
        - [A S{->} S{alpha} *]
    licenses the edge:
        - [B S{->} * A S{beta}]
    for each grammar production C{B S{->} A S{beta}}.
    i   c         c   sp   | i    o d  Sn xT | i d | i    D]: } t i | | i    } | i | d  o	 | Vq. q. Wd  S(   NR   (    (   R   R   R   R   R$   R   RY   (   R	   R   R   RQ   R   RZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s     (   R   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   ~  s   	t   BottomUpPredictCombineRulec           B   s   e  Z d  Z d Z d   Z RS(   s  
    A rule licensing any edge corresponding to a production whose
    right-hand side begins with a complete edge's left-hand side.  In
    particular, this rule specifies that:
        - [A S{->} S{alpha} *]
    licenses the edge:
        - [B S{->} A * S{beta}]
    for each grammar production C{B S{->} A S{beta}}.
    
    @note: This is like L{BottomUpPredictRule}, but it also applies
    the L{FundamentalRule} to the resulting edge.
    i   c         c   s   | i    o d  Sn xf | i d | i    D]L } t | i   | i   | i   d  } | i | | f  o	 | Vq. q. Wd  S(   NR   i   (   R   R   R   R   R   R   RY   (   R	   R   R   RQ   R   RZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s     '(   R   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s   t   EmptyPredictRulec           B   s   e  Z d  Z d Z d   Z RS(   si   
    A rule that inserts all empty productions as passive edges,
    in every position in the chart.
    i    c         c   sp   xi | i  d t  D]U } xL t | i   d  D]4 } t i | |  } | i | d  o	 | Vq0 q0 Wq Wd  S(   Nt   emptyi   (    (   R   R5   t   xrangeR@   R   R$   RY   (   R	   R   R   R   R#   RZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s      (   R   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s   t!   FilteredSingleEdgeFundamentalRulec           B   s#   e  Z d    Z d   Z d   Z RS(   c         c   se   | i    o, xQ |  i | | i |  D] } | Vq& Wn) x% |  i | | i |  D] } | VqR Wd  S(   N(   R   R   t   leftcornersR   (   R	   R   R   RQ   RZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s      c         c   s   | i    } | | i   j  o | i |  } n d  } x | i d | i   d t d | i    D]` } t | | | i	   | i
    o8 | i | i     } | i | | |  o	 | Vq qc qc Wd  S(   NR   R   R   (   R   R@   R4   R(   RO   R   R6   R   t   _bottomup_filterR   R   R&   Ra   (   R	   R   R   R   R   t	   nexttokenR   RZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s     "c         c   s   x | i  d | i   d t d | i    D] } | i   } | | i   j  o | i |  } n d  } t | | | i   | i	    o8 | i
 | i    } | i | | |  o	 | Vq q+ q+ Wd  S(   NR   R   R   (   RO   R   R5   R   R@   R4   R(   R   R   R   R&   Ra   (   R	   R   R   R   R   R   R   RZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s     "(   R   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s   		t"   FilteredBottomUpPredictCombineRulec           B   s   e  Z d    Z RS(   c   	      c   s   | i    o d  Sn | i } | i   } | | i   j  o | i |  } n d  } x | i d | i    D]i } t | | | i	    oJ t
 | i   | i   | i	   d  } | i | | f  o	 | Vq qo qo Wd  S(   NR   i   (   R   R   R   R@   R4   R(   R   R   R   R   R   R   RY   (	   R	   R   R   RQ   R   R   R   R   RZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    	 '(   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s   i    c         C   s\   t  |  | d j o t Sn | | d } t |  o | | j Sn | |  |  j Sd  S(   Ni   (   R'   R5   R   (   R   R   R   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    t   ChartParserc           B   sS   e  Z d  Z e d d e e d  Z d   Z d   Z e	 d  Z
 e	 e d  Z RS(   s  
    A generic chart parser.  A X{strategy}, or list of
    L{ChartRules<ChartRuleI>}, is used to decide what edges to add to
    the chart.  In particular, C{ChartParser} uses the following
    algorithm to parse texts:

        - Until no new edges are added:
          - For each I{rule} in I{strategy}:
            - Apply I{rule} to any applicable edges in the chart.
        - Return any complete parses in the chart
    i    i2   c         C   s   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ g  |  _ g  |  _ x_ | D]W } | i d j o |  i i	 |  qO | i d j o |  i i	 |  qO t
 |  _ qO Wd S(   s>  
        Create a new chart parser, that uses C{grammar} to parse
        texts.

        @type grammar: L{ContextFreeGrammar}
        @param grammar: The grammar used to parse texts.
        @type strategy: C{list} of L{ChartRuleI}
        @param strategy: A list of rules that should be used to decide
            what edges to add to the chart (top-down strategy by default).
        @type trace: C{int}
        @param trace: The level of tracing that should be used when
            parsing a text.  C{0} will generate no tracing output;
            and higher numbers will produce more verbose tracing
            output.
        @type trace_chart_width: C{int}
        @param trace_chart_width: The default total width reserved for 
            the chart in trace output.  The remainder of each line will 
            be used to display edges. 
        @type use_agenda: C{bool}
        @param use_agenda: Use an optimized agenda-based algorithm, 
            if possible. 
        @param chart_class: The class that should be used to create
            the parse charts.
        i    i   N(   t   _grammart	   _strategyt   _tracet   _trace_chart_widtht   _use_agendat   _chart_classt   _axiomst   _inference_rulesR   RU   R6   (   R	   R   t   strategyt   tracet   trace_chart_widtht
   use_agendat   chart_classt   rule(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR
     s    								 c         C   s   |  i  S(   N(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   G  s    c         C   s[   | p d  Sn | d j } x9 | D]1 } | o d | GHt  } n | i | |  GHq" Wd  S(   Ni   s   %s:(   R6   R   (   R	   R   R   t	   new_edgesR   t
   edge_widtht   should_print_rule_headerRQ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   _trace_new_edgesJ  s     	
c      	   C   s  | t  j o |  i } n |  i } t |  } |  i i |  |  i |  } |  i } |  i | i   d } | o | i	 |  GHn |  i
 o x9 |  i D]. } | i | |  } | | | | | |  q W|  i }	 | i   }
 |
 i   x |
 om |
 i   } xZ |	 D]R } | i | | |  } | o& t |  } | | | | | |  n |
 | 7}
 q	Wq Wnd t } xZ | oR t } xE |  i D]: } | i | |  } t |  } | | | | | |  qWqqW| S(   s   
        @return: The final parse L{Chart}, 
        from which all possible parse trees can be extracted.
        
        @param tokens: The sentence to be parsed
        @type tokens: L{list} of L{string}
        @rtype: L{Chart}
        i   (   R(   R   R   R   R   t   check_coverageR   R   R@   R   R   R   R   R   RB   t   reverset   popR   R5   R6   R   R   R'   (   R	   R<   R   t   trace_new_edgesR   R   t   trace_edge_widtht   axiomR   t   inference_rulest   agendaRQ   R   t   edges_added(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   chart_parseS  sD    			

 	

 

 c         C   s/   |  i  |  } | i |  i i   d | |  S(   NRe   (   R   Ri   R   R   (   R	   R<   t   nRe   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   nbest_parse  s    (   R   R   R   t   BU_LC_STRATEGYR5   R8   R
   R   R   R(   R   R    R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s   ,			6t   TopDownChartParserc           B   s   e  Z d  Z d   Z RS(   sj   
    A L{ChartParser} using a top-down parsing strategy.
    See L{ChartParser} for more information.
    c         K   s   t  i |  | t |  d  S(   N(   R   R
   t   TD_STRATEGY(   R	   R   t   parser_args(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR
     s    (   R   R   R   R
   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s   t   BottomUpChartParserc           B   s   e  Z d  Z d   Z RS(   sk   
    A L{ChartParser} using a bottom-up parsing strategy.
    See L{ChartParser} for more information.
    c         K   sA   t  | t  o t i d d t n t i |  | t |  d  S(   Nsc   BottomUpChartParser only works for ContextFreeGrammar, use BottomUpProbabilisticChartParser insteadt   category(   R7   R   t   warningst   warnt   DeprecationWarningR   R
   t   BU_STRATEGY(   R	   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR
     s    (   R   R   R   R
   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s   t   BottomUpLeftCornerChartParserc           B   s   e  Z d  Z d   Z RS(   s   
    A L{ChartParser} using a bottom-up left-corner parsing strategy.
    This strategy is often more efficient than standard bottom-up.
    See L{ChartParser} for more information.
    c         K   s   t  i |  | t |  d  S(   N(   R   R
   R   (   R	   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR
     s    (   R   R   R   R
   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s   t   LeftCornerChartParserc           B   s   e  Z d    Z RS(   c         K   s7   | i    p t d   n t i |  | t |  d  S(   NsC   LeftCornerParser only works for grammars without empty productions.(   t   is_nonemptyRS   R   R
   t   LC_STRATEGY(   R	   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR
     s    (   R   R   R
   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s   t   SteppingChartParserc           B   s   e  Z d  Z g  d d  Z d   Z d   Z d   Z d   Z d   Z d   Z	 d	   Z
 e d
  Z d   Z d   Z d   Z e e d  Z RS(   s  
    A C{ChartParser} that allows you to step through the parsing
    process, adding a single edge at a time.  It also allows you to
    change the parser's strategy or grammar midway through parsing a
    text.

    The C{initialize} method is used to start parsing a text.  C{step}
    adds a single edge to the chart.  C{set_strategy} changes the
    strategy used by the chart parser.  C{parses} returns the set of
    parses that has been found by the chart parser.

    @ivar _restart: Records whether the parser's strategy, grammar,
        or chart has been changed.  If so, then L{step} must restart
        the parsing algorithm.
    i    c         C   s5   d  |  _ d  |  _ t |  _ t i |  | | |  d  S(   N(   R(   t   _chartt   _current_chartruleR6   t   _restartR   R
   (   R	   R   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR
     s    			c         C   s"   t  t |   |  _ t |  _ d S(   s   Begin parsing the given tokens.N(   R8   R   R   R5   R   (   R	   R<   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR;     s    c         c   s   |  i  d j o t d  n x t |  _ d |  i  i   d } xq |  i   D]^ } |  i d j o |  i GHn |  i d j o |  i  i	 | |  GHn | V|  i o PqM qM Wd Vq  d S(   s  
        @return: A generator that adds edges to the chart, one at a
        time.  Each time the generator is resumed, it adds a single
        edge and yields that edge.  If no more edges can be added,
        then it yields C{None}.

        If the parser's strategy, grammar, or chart is changed, then
        the generator will continue adding edges using the new
        strategy, grammar, or chart.

        Note that this generator never terminates, since the grammar
        or strategy might be changed to values that would add new
        edges.  Instead, it yields C{None} when no more edges can be
        added with the current strategy and grammar.
        s    Parser must be initialized firsti2   i   i    N(
   R   R(   RS   R6   R   R@   t   _parseR   R   R   (   R	   t   wR   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   step  s    	 (c         c   s   |  i  } |  i } d } xa | d j oS d } xF |  i D]; } | |  _ x) | i | |  D] } | d 7} | VqZ Wq8 Wq Wd S(   s   
        A generator that implements the actual parsing algorithm.
        L{step} iterates through this generator, and restarts it
        whenever the parser's strategy, grammar, or chart is modified.
        i   i    N(   R   R   R   R   R   (   R	   R   R   R   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    		
 	 
c         C   s   |  i  S(   s*   @return: The strategy used by this parser.(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s   |  i  S(   s)   @return: The grammar used by this parser.(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s   |  i  S(   s/   @return: The chart that is used by this parser.(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s   |  i  S(   s>   @return: The chart rule used to generate the most recent edge.(   R   (   R	   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   current_chartrule  s    c         C   s   |  i  i |  i i   |  S(   s:   @return: The parse trees currently contained in the chart.(   R   Ri   R   R   (   R	   Re   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyRi     s    c         C   s/   | |  i  j o d Sn | |  _  t |  _ d S(   s  
        Change the startegy that the parser uses to decide which edges
        to add to the chart.
        @type strategy: C{list} of L{ChartRuleI}
        @param strategy: A list of rules that should be used to decide
            what edges to add to the chart.
        N(   R   R5   R   (   R	   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   set_strategy$  s    
c         C   s.   | |  i  j o d Sn | |  _  t |  _ d S(   s&   Change the grammar used by the parser.N(   R   R5   R   (   R	   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   set_grammar0  s    	c         C   s.   | |  i  j o d Sn | |  _  t |  _ d S(   s)   Load a given chart into the chart parser.N(   R   R5   R   (   R	   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt	   set_chart6  s    	c         C   sf   t  |  } |  i i |  |  i |  x& |  i   D] } | d  j o Pq6 q6 W|  i d |  |  S(   NRe   (   R   R   R   R;   R   R(   Ri   (   R	   R<   R   Re   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   @  s     (   R   R   R   R
   R;   R   R   R   R   R   R   R    Ri   R   R   R   R(   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s   
											
c          C   s   d d k  l }  |  d  S(   Ni(   t	   parse_cfgs   
S  -> NP VP
PP -> "with" NP
NP -> NP PP
VP -> VP PP
VP -> Verb NP
VP -> Verb
NP -> Det Noun
NP -> "John"
NP -> "I"
Det -> "the"
Det -> "my"
Det -> "a"
Noun -> "dog"
Noun -> "cookie"
Verb -> "ate"
Verb -> "saw"
Prep -> "with"
Prep -> "under"
(   t   nltk.grammarR   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   demo_grammarR  s    i   s$   I saw John with a dog with my cookiei   c         C   s1  d d k  } d d k } d d k l }	 l }
 l } t   } | o d GH| GHn d GH| GH| i   } | GHH|  d j o7 d GHd GHd GHd	 GHd
 GHd G| i	 i
   i   }  Hn t |   }  |  d& j o d GHd Sn h  } h  d t f d <d t f d <d t f d <} g  } | i |   o |  g } n |  d j o d d d g } n x | D] } d | | d GHHt | | | d d | } | i   } | i |  } | i   | | | | d <| o! t |  | j p
 t d  n | o x& | D] } | GHqWn d Gt |  GHHq[W|  d' j osd GHH| i   } t | d | } | i |  x t d  D] } d GH| i t  x? t | i    D]+ \ } } | d j p | d j o PqqWd GH| i t  x? t | i    D]+ \ } } | d j p | d j o PqqWqoW| i   | | d  <| o' t | i    | j p
 t d  n | o  x2 | i   D] } | GHqwWn d Gt | i    GHHn | o | p d Sn d! GHHt d"   | i   D  } d# | d$ } | i   } | i d%    x# | D] \ } } | | | f GHqWd S((   s/   
    A demonstration of the chart parsers.
    iN(   t   nonterminalst
   Productiont   ContextFreeGrammars	   * Grammars   * Sentence:s     1: Top-down chart parsers     2: Bottom-up chart parsers'     3: Bottom-up left-corner chart parsers=     4: Stepping chart parser (alternating top-down & bottom-up)s     5: All parserss   
Which parser (1-5)? t   1t   2t   3t   4t   5s   Bad parser numbers   Top-downs	   Bottom-ups   Bottom-up left-corners   * Strategy: i    i   R   s   Not all parses founds	   Nr trees:s,   * Strategy: Stepping (top-down vs bottom-up)i   s   *** SWITCH TO TOP DOWNi   s   *** SWITCH TO BOTTOM UPt   Steppings   * Parsing timesc         s   s   x |  ] } t  |  Vq Wd  S(   N(   R'   (   RF   RG   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pys	   <genexpr>  s    t   %s   s parser: %6.3fsecc         S   s   t  |  d  | d   S(   i   (   R)   (   t   at   b(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   <lambda>  s    (   R   R   R   R  R  (   R  R  (    t   syst   timet   nltkR   R   R   R   t   splitR(   t   stdint   readlinet   stripR,   R   R   R   t   has_keyR   R   R'   R   R   R;   R+   R   R   R   Ri   t   maxRI   RV   RJ   (   t   choicet   should_print_timest   should_print_grammart   should_print_treesR   t   sentt	   numparsesR  R	  R   R   R   R   R<   t   timest
   strategiest   choicesR   Ro   t   tRi   Ru   R-   t   jR   t   maxlent   formatt   times_itemst   parser(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   demoi  s    		  !   # ''  t   __main__(4   R   t	   nltk.treeR    R   R   R   R   t   nltk.compatR   t   apiR   R   t   objectR   R   R1   R8   R   R   R   R   R   R   t
   DeprecatedR   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   ParserIR   R   R   R   R   R   R   R(   R5   R6   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pys   <module>'   sn   
s5 G?,&(		c