
4Ic           @   s  d  d k  l Z l Z l Z d  d k Td  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 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 e   e   e   e   g Z e   e   e   g Z e   e   e   g Z e   e   e   e   g Z d* e f d+     YZ d, e f d-     YZ  e! e" d. d/  Z# e$ d0 j o e#   n d S(1   i(   t   Treet   Nonterminalt   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__X   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   span`   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   starti   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   endp   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   lengthw   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   3   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 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   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   s4  d |  i  d |  i  d f } t |  i i   t  o | d |  i f 7} n | d |  i f 7} x t t |  i   D] } | |  i j o | d 7} n t |  i | t	  o9 t |  i | i   t  o | d |  i | f 7} qy | d |  i | f 7} qy Wt |  i  |  i j o | d 7} n | S(	   Ns   [%s:%s] i    i   s   %-2s ->s   %-2r ->s    *s    %ss    %r(
   R   t
   isinstanceR   t   symbolt
   basestringt   rangeR#   R   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__.  s    (   R   R   R   R   R"   t   staticmethodR   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   =  s    		c         C   s   |  i  S(   N(   R1   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   J  s    c         C   s   |  i  |  i  d f S(   Ni   (   R2   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR
   K  s    c         C   s   |  i  S(   N(   R2   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   L  s    c         C   s   |  i  d S(   Ni   (   R2   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   M  s    c         C   s   d S(   Ni   (    (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   N  s    c         C   s   d S(   N(    (    (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   O  s    c         C   s   d S(   Ni    (    (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   P  s    c         C   s   t  S(   N(   t   True(   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   Q  s    c         C   s   t  S(   N(   t   False(   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   R  s    c         C   s   d  S(   N(   R$   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   S  s    c         C   s=   t  | t  p d Sn t |  i |  i f | i | i f  S(   Ni(   R'   R0   R%   R2   R1   (   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   V  s    c         C   s   t  |  i |  i f  S(   N(   R&   R2   R1   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   Y  s    c         C   s   d |  i  |  i  d |  i f S(   Ns
   [%s:%s] %ri   (   R2   R1   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR-   ]  s    c         C   s   d |  S(   Ns
   [Edge: %s](    (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR.   ^  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.pyR0   1  s    														t   Chartc           B   s   e  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 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  |  _ g  |  _ h  |  _ h  |  _ d S(   s   
        Construct a new empty chart.

        @type tokens: L{list}
        @param tokens: The sentence that this chart will be used to parse.
        N(   t   listt   _tokensR#   t   _num_leavest   _edgest   _edge_to_cplst   _indexes(   R   t   tokens(    (    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}
        (   R9   (   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}
        (   R8   (   R   R!   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR3     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}
        (   R8   (   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(   s  
        @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.
        @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         K   s   | h  j o t  |  i  Sn | i   } | i   t |  } | |  i j o |  i |  n g  } | D] } | | | ql ~ } t  |  i | i t |  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}
        (   RA   R:   t   keyst   sortR   R<   t
   _add_indext   get(   R   t   restrictionst
   restr_keyst   _[1]t   kt   vals(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   select  s    
%c      	   C   s   x/ | D]' } t  t |  p t d |  q q Wh  |  i | <xj |  i D]_ } g  } | D] } | t | |    qZ ~ } |  i | } | i t |  g   i |  qI 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: %sN(	   t   hasattrR   t
   ValueErrorR<   R:   t   getattrt
   setdefaultR   t   append(   R   RI   RK   t   edgeRJ   RL   R!   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyRF     s     
 -c         G   s   | |  i  j o |  i i |  xm |  i i   D]X \ } } g  } | D] } | t | |    qG ~ } | i t |  g   i |  q0 Wn |  i  i | h   } t }	 x; | D]3 }
 t |
  }
 |
 | j o t	 | |
 <t	 }	 q q 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;   R:   RR   R<   t   itemsRP   RQ   R   R5   R4   (   R   RS   t   child_pointer_listsRI   R!   RJ   RK   RL   t   cplst   chart_was_modifiedt   child_pointer_list(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   insert  s     -' 
c         C   sU   g  } xH |  i  d d |  i f 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   t
   tree_classt   complete(   RM   R9   t   treesR4   (   R   t   rootRZ   R\   RS   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   parses7  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   memoRZ   (   t   _trees(   R   RS   RZ   R[   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR\   A  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 ~ }
 t |
  d j o* t |
 d  t d  j o |
 g }
 n x? |  i	 |
  D]. } | i
   i   } | i | | |   qWq W| i    o\ g  } | i   | i   D] } | | | g   qq~ } x | D] } | i |  q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.
        i    t    (   R   R'   R0   R8   R   RU   R`   R#   t   typet   _choose_childrenR   R(   RR   R   R   t   extend(   R   RS   R[   R_   RZ   R\   R3   t   cplRJ   t   cpt   child_choicest   childrenR   t   _[2]t   eltt
   unexpandedt   tree(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR`   R  s8    

 ,0 3 
c         C   sS   g  g } xC | D]; } g  } | D]! } | D] } | | | g q+ 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.
        (    (   R   Rg   t   children_listst   child_choiceRJ   t   childt
   child_list(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyRc     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;   RG   RD   (   R   RS   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyRU     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
   R9   (   R   RS   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   |.Rr   Rq   Ra   (   R$   R>   R8   t   center(   R   Rz   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{   (   t   .0RS   (   Rz   R   (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pys	   <genexpr>  s    (   R$   R>   R   R   RE   R   t   join(   R   Rz   RJ   t   eR@   Ri   t   _(    (   R   Rz   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*   RC   R>   R:   R   R   R3   t	   enumerate(   R   t   st   yt   xRS   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   dot_digraph  sZ    


  + 
 
 +


 %

  $+ (
(   R   R   R   R   R>   R3   R?   R@   RB   t   __iter__RC   RM   RF   RY   R    R^   R5   R\   R`   Rc   RU   R$   R{   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR6   e  s*   					
			%		)
	=		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   grammarR@   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   apply=  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   R@   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt
   apply_iterK  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_everywhereZ  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_iterd  s    	(   R   R   R   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   (  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   R@   (    (    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   t   new_edget   e1t   e2t   e3(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s:              c         G   s   t  |  i | | |   S(   N(   R7   R   (   R   R   R   R@   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    c         C   s   t  |  i | |   S(   N(   R7   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   o  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:
    
        - [AS{->}S{alpha}*BS{beta}][i:j]
        - [BS{->}S{gamma}*][j:k]
    licenses the edge:
        - [AS{->}S{alpha}B*S{beta}][i:j]
    i   c   
   
   c   s   | i    | i   j o0 | i   | i   j o | i   o
 | i   p d  Sn t d | i   | i    f d | i   d | i   d | i   d  } | i	 |  } g  } | D] } | | | f q ~ }	 | i
 | |	  o	 | Vn d  S(   NR
   R   R   R   i   (   R   R   R   R   R   R   R   R   R   RU   RY   (
   R   R   R   t	   left_edget
   right_edgeR   RV   RJ   Re   t   new_cpls(    (    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 e   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:
        - [AS{->}S{alpha}*BS{beta}][i:j]
        - [BS{->}S{gamma}*][j:k]
    licenses the edge:
        - [AS{->}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 is left
        unspecified.
    i   c         c   s   |  i  } | i   oa x | i d | i   d t d | i    D]. } x% | i | | | |  D] } | Vq` WqA Wn^ xZ | i d | i   d t d | i	    D]. } x% | i | | | |  D] } | Vq Wq Wd  S(   NR   R   R   R   R   (
   t   _fundamental_ruleR   RM   R   R4   R   R   R   R5   R   (   R   R   R   t   edge1t   frt   edge2R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    	    c         C   s   d S(   Ns   Fundamental Rule(    (   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   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:
        - [SS{->}*S{alpha}][0:i]
    is licensed for each grammar production C{SS{->}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   prodR   (    (    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   TopDownExpandRulec           B   s   e  Z d  Z d Z d   Z RS(   sG  
    A rule licensing edges corresponding to the grammar productions
    for the nonterminal following an incomplete edge's dot.  In
    particular, this rule specifies that:
        - [AS{->}S{alpha}*BS{beta}][i:j]
    licenses the edge:
        - [BS{->}*S{gamma}][j:j]
    for each grammar production C{BS{->}S{gamma}}.
    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   RS   R   R   (    (    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   TopDownMatchRulec           B   s   e  Z d  Z d Z d   Z RS(   s  
    A rule licensing an edge corresponding to a terminal following an
    incomplete edge's dot.  In particular, this rule specifies that:
        - [AS{->}S{alpha}*w{beta}][i:j]
    licenses the leaf edge:
        - [wS{->}*][j:j+1]
    if the C{j}th word in the text is C{w}.
    i   c         c   s   | i    p | i   | i   j o d  Sn | i   } | i |  } | i   | j o/ t | |  } | i | d  o	 | Vq n d  S(   N(    (   R   R   R>   R3   R   R0   RY   (   R   R   R   RS   R!   R3   R   (    (    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   CachedTopDownInitRulec           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s   
    A cached version of L{TopDownInitRule}.  After the first time this
    rule is applied, it will not generate any more edges.

    If C{chart} or C{grammar} are changed, then the cache is flushed.
    c         C   s   t  i |   d |  _ d  S(   N(   NN(   R   R   R$   t   _done(   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   /  s    c         c   sh   |  i  d | j o |  i  d | j o d  Sn x" t i |  | |  D] } | VqF W| | f |  _  d  S(   Ni    i   (   R   R   R   (   R   R   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   3  s
    0 	c         C   s   d S(   Ns   Top Down Init Rule(    (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR-   ?  s    (   R   R   R   R   R   R-   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   (  s   		t   CachedTopDownExpandRulec           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s-  
    A cached version of L{TopDownExpandRule}.  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   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   J  s    c         c   s   |  i  i | i   | i   f d  } | d | j o | d | j o d  Sn x% t i |  | | |  D] } | Vqj W| | f |  i  | i   | i   f <d  S(   Ni    i   (   NN(   R   RG   R   R   R$   R   R   (   R   R   R   RS   t   doneR   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   N  s    '* 	c         C   s   d S(   Ns   Top Down Expand Rule(    (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR-   \  s    (   R   R   R   R   R   R-   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   A  s   		t   BottomUpInitRulec           B   s   e  Z d  Z d Z d   Z RS(   s   
    A rule licensing any edges corresponding to terminals in the
    text.  In particular, this rule licenses the leaf edge:
        - [wS{->}*][i:i+1]
    for C{w} is a word in the text, where C{i} is C{w}'s index.
    i    c         c   sU   xN t  | i    D]: } t | i |  |  } | i | d  o	 | Vq q Wd  S(   N(    (   R*   R>   R0   R3   RY   (   R   R   R   R!   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   j  s
     (   R   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   b  s   t   BottomUpPredictRulec           B   s   e  Z d  Z d Z d   Z RS(   s8  
    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:
        - [AS{->}S{alpha}*]
    licenses the edge:
        - [BS{->}*AS{beta}]
    for each grammar production C{BS{->}AS{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   RS   R   R   (    (    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   p  s   	t   BottomUpPredictCombineRulec           B   s   e  Z d  Z d   Z RS(   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   RS   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   CompleterRulec           B   s   e  Z d    Z RS(   c         C   s   d S(   Ns   Completer Rule(    (   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   ScannerRulec           B   s   e  Z d  Z d Z d   Z RS(   s|  
    A rule licensing a leaf edge corresponding to a part-of-speech
    terminal following an incomplete edge's dot.  In particular, this
    rule specifies that:
        - [AS{->}S{alpha}*PS{beta}][i:j]
    licenses the edges:
        - [PS{->}w*][j:j+1]
        - [wS{->}*][j:j+1]
    if the C{j}th word in the text is C{w}; and C{P} is a valid part
    of speech for C{w}.
    i   c   
      c   s  | i    p | i   | i   j o d  Sn | i   } | i |  } | i   g  } | i d |  D] } | | i   qi ~ j ov t | |  } | i | d  o	 | Vn t	 | | d f | i   | g d  }	 | i |	 | f  o	 |	 Vq n d  S(   NR   i   (    (
   R   R   R>   R3   R   R   R   R0   RY   R   (
   R   R   R   RS   R!   R3   RJ   R   t   new_leaf_edget   new_pos_edge(    (    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   PredictorRulec           B   s   e  Z d    Z d   Z RS(   c         c   s   |  i  i | i   | i   f d  } | d | j o | d | j o d  Sn | i   o d  Sn x | i d | i    D]t } t | i    d j o! t	 | i   d t
  o q n t i | | i    } | i | d  o	 | Vq q W| | f |  i  | i   | i   f <d  S(   Ni    i   R   (   NN(    (   R   RG   R   R   R$   R   R   R#   R   R'   R+   R   R"   RY   (   R   R   R   RS   R   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s    '* :c         C   s   d S(   Ns   Predictor Rule(    (   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   ChartParserc           B   sA   e  Z d  Z e d e d  Z d   Z 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    c         C   s   | |  _  | |  _ | |  _ | |  _ g  |  _ g  |  _ x_ | D]W } | i d j o |  i i |  q= | i d j o |  i i |  q= t |  _ q= 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 use_agenda: C{bool}
        @param use_agenda: Use an optimized agenda-based algorithm, 
            if possible.  
        i    i   N(	   t   _grammart	   _strategyt   _tracet   _use_agendat   _axiomst   _inference_rulesR   RR   R5   (   R   R   t   strategyt   tracet
   use_agendat   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      s    c   
         s  t  |  }  i i |  t t  |     i } g  } d  i   d    i d j o  i    GHn     f d   }  i o x:  i D]/ } | i	  |  } | | 7} | | |  q Wx | g  j oP | i
   } x=  i D]2 } | i	  | |  } | | 7} | | |  q Wq Wne d }	 x[ |	 d j oM d }	 x@  i D]5 } | i  |  } | | |  |	 t |  7}	 qZWq=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}
        i2   i   i    c            sd     i  d j oP | g  j oC   i  d j o d |  GHn x# | D] }  i |   GHqA Wn d  S(   Ni    i   s   %s:(   R   R{   (   R   t	   new_edgesR   (   R   R   t   w(    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   _trace_new_edges  s     (   R7   R   t   check_coverageR6   R>   R   R   R   R   R   t   popR   R   R   R#   (
   R   R=   R   t   agendaR   t   axiomR   RS   R   t   edges_added(    (   R   R   R   s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   chart_parse  s<    		"

 

 

 c         C   s5   |  i  |  } |  i } | i | i   d | |  S(   NRZ   (   R   R   R^   R   (   R   R=   t   nRZ   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   nbest_parse4  s    	(   R   R   R   t   BU_STRATEGYR4   R   R   R   R$   R    R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     s
   #		1t   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_chartruleR5   t   _restartR   R   (   R   R   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR   N  s    			c         C   s"   t  t |   |  _ t |  _ d S(   s   Begin parsing the given tokens.N(   R6   R7   R   R4   R   (   R   R=   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt
   initializeX  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$   RO   R5   R   R>   t   _parseR   R   R{   (   R   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   stepa  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   R^   R   R   (   R   RZ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR^     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   R4   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   R4   R   (   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   set_grammar  s    	c         C   s.   | |  i  j o d Sn | |  _  t |  _ d S(   s)   Load a given chart into the chart parser.N(   R   R4   R   (   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt	   set_chart  s    	c         C   sf   t  |  } |  i i |  |  i |  x& |  i   D] } | d  j o Pq6 q6 W|  i d |  |  S(   NRZ   (   R7   R   R   R   R   R$   R^   (   R   R=   R   RZ   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyR     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.pyR   >  s   
											
i   c   %      C   sH  d d k  } d d k } d d k l } l } l } | d  \ } }	 }
 } | d  \ } } } } } | | |
 |	 g  | | | |
 g  | |
 | | g  | |
 |
 | g  | |	 |	 | g  | |	 | |
 g  | |	 | g  | |
 d g  | |
 d g  | | d g  | | d	 g  | | d
 g  | | d g  | | d g  | | d g  | | d g  | | d g  | | d g  g } | | |  } d } d GH| GH| i   } | GHH|  d j o< d GHd GHd GHd GHd GHd GHd G| i i	   i
   }  Hn t |   }  |  d5 j o d  GHd Sn h  } h  d! t f d <d" t f d <d# t f d <d$ t f d <} g  } | i |   o |  g } n |  d j o d d d d g } n x | D] } d% | | d& GHHt | | | d' d( | } | i   } | i |  } | i   | | | | d& <t |  d) j p
 t d*  x | D] } | GHqRWHqW|  d6 j oH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 Pq0q0WqW| i   | | d/ <t | i    d) j p
 t d*  x | i   D] }  |  GHqWHn | o | p d Sn d0 GHHt d1   | i   D  }! d2 |! d3 }" | i   }# |# i d4    x# |# D] \ }$ } |" |$ | f GHq%Wd S(7   s/   
    A demonstration of the chart parsers.
    iN(   t   nonterminalst
   Productiont   ContextFreeGrammars   S, VP, NP, PPs   V, N, P, Name, Dett   Johnt   It   thet   myt   at   dogt   cookiet   atet   sawt   witht   unders$   I saw John with a dog with my cookies   * Sentence:s     1: Top-down chart parsers     2: Bottom-up chart parsers#     3: Bottom-up combine chart parsers     4: Earley parsers=     5: Stepping chart parser (alternating top-down & bottom-up)s     6: All parserss   
Which parser (1-6)? t   1t   2t   3t   4t   5t   6s   Bad parser numbers   Top-downs	   Bottom-ups   Bottom-up combinet   Earleys   * Strategy: i    i   R   i   s   Not all parses founds,   * Strategy: Stepping (top-down vs bottom-up)s   *** SWITCH TO TOP DOWNi   s   *** SWITCH TO BOTTOM UPt   Steppings   * Parsing timesc         s   s   x |  ] } t  |  Vq Wd  S(   N(   R#   (   R   t   key(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pys	   <genexpr>A  s    t   %s   s parser: %6.3fsecc         S   s   t  |  d  | d   S(   i   (   R%   (   R   t   b(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   <lambda>D  s    (   R   R   R   R   R   R   (   R   R   (    t   syst   timet   nltkR   R   R   t   splitR$   t   stdint   readlinet   stripR+   t   TD_STRATEGYR   t   BUC_STRATEGYt   EARLEY_STRATEGYt   has_keyR   R   R#   R	   R   R   R*   R   R   R   R^   t   maxRD   RT   RE   (%   t   choicet   should_print_timesR   R   R   R   R   R   t   St   VPt   NPt   PPt   Vt   Nt   Pt   Namet   DetR   R   t   sentR=   t   timest
   strategiest   choicesR   Rf   t   tR^   Rl   R,   t   jR   t   parset   maxlent   formatt   times_itemst   parser(    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pyt   demo  s    $$$$#    # '#  t   __main__(%   R   R    R   R   t   apiR   t   objectR   R   R0   R6   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   ParserIR   R   R$   R4   R  R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/parse/chart.pys   <module>   sB   
m4 G>(!mn