
    g                     p   d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ e G d	 d
             Z G d de      Z G d de      Z G d d      Z G d d      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d e      Z G d! d"e      Z G d# d$e      Z G d% d&e      Z  G d' d(e      Z! G d) d*e      Z"d;d+Z# e        e        e        e       gZ$ e        e         e        e       gZ% e        e         e        e       gZ& e        e"        e!       gZ' G d, d-e      Z( G d. d/e(      Z) G d0 d1e(      Z* G d2 d3e(      Z+ G d4 d5e(      Z, G d6 d7e(      Z-d8 Z.	 	 	 	 	 	 	 d<d9Z/e0d:k(  r e/        yy)=a  
Data classes and parser implementations for "chart parsers", which
use dynamic programming to efficiently parse a text.  A chart
parser derives parse trees for a text by iteratively adding "edges"
to a "chart."  Each edge represents a hypothesis about the tree
structure for a subsequence of the text.  The 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 "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 ``Chart`` class, and edges are encoded with
the ``TreeEdge`` and ``LeafEdge`` classes.  The chart parser module
defines three chart parsers:

  - ``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.

  - ``SteppingChartParser`` is a subclass of ``ChartParser`` that can
    be used to step through the parsing process.
    N)total_ordering)PCFGis_nonterminalis_terminal)raise_unorderable_types)ParserI)Tree)OrderedDictc                   j    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d Zy)EdgeIa  
    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 span, indicating what part of the sentence is
      consistent with the hypothesized structure.
    - A left-hand side, specifying what kind of structure is
      hypothesized.
    - A right-hand side, specifying the contents of the
      hypothesized structure.
    - A dot position, indicating how much of the hypothesized
      structure is consistent with the sentence.

    Every edge is either complete or incomplete:

    - An edge is complete if its structure is fully consistent
      with the sentence.
    - An edge is 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:

    - A ``TreeEdge`` records which trees have been found to
      be (partially) consistent with the text.
    - A ``LeafEdge`` records the tokens occurring in the text.

    The ``EdgeI`` interface provides a common interface to both types
    of edge, allowing chart parsers to treat them in a uniform manner.
    c                 @    | j                   t        k(  rt        d      y )NzEdge is an abstract interface)	__class__r   	TypeErrorselfs    E/var/www/openai/venv/lib/python3.12/site-packages/nltk/parse/chart.py__init__zEdgeI.__init__X   s    >>U";<< #    c                     t               )z
        Return a tuple ``(s, e)``, where ``tokens[s:e]`` is the
        portion of the sentence that is consistent with this
        edge's structure.

        :rtype: tuple(int, int)
        NotImplementedErrorr   s    r   spanz
EdgeI.span`        "##r   c                     t               )zR
        Return the start index of this edge's span.

        :rtype: int
        r   r   s    r   startzEdgeI.startj        "##r   c                     t               )zP
        Return the end index of this edge's span.

        :rtype: int
        r   r   s    r   endz	EdgeI.endr   r   r   c                     t               )zM
        Return the length of this edge's span.

        :rtype: int
        r   r   s    r   lengthzEdgeI.lengthz   r   r   c                     t               )z
        Return this edge's left-hand side, which specifies what kind
        of structure is hypothesized by this edge.

        :see: ``TreeEdge`` and ``LeafEdge`` for a description of
            the left-hand side values for each edge type.
        r   r   s    r   lhsz	EdgeI.lhs   r   r   c                     t               )a  
        Return this edge's right-hand side, which specifies
        the content of the structure hypothesized by this edge.

        :see: ``TreeEdge`` and ``LeafEdge`` for a description of
            the right-hand side values for each edge type.
        r   r   s    r   rhsz	EdgeI.rhs   r   r   c                     t               )a  
        Return this edge's dot position, which indicates how much of
        the hypothesized structure is consistent with the
        sentence.  In particular, ``self.rhs[:dot]`` is consistent
        with ``tokens[self.start():self.end()]``.

        :rtype: int
        r   r   s    r   dotz	EdgeI.dot        "##r   c                     t               )z
        Return the element of this edge's right-hand side that
        immediately follows its dot.

        :rtype: Nonterminal or terminal or None
        r   r   s    r   nextsymzEdgeI.nextsym        "##r   c                     t               )zw
        Return True if this edge's structure is fully consistent
        with the text.

        :rtype: bool
        r   r   s    r   is_completezEdgeI.is_complete   r*   r   c                     t               )z{
        Return True if this edge's structure is partially consistent
        with the text.

        :rtype: bool
        r   r   s    r   is_incompletezEdgeI.is_incomplete   r*   r   c                 h    | j                   |j                   u xr | j                  |j                  k(  S N)r   _comparison_keyr   others     r   __eq__zEdgeI.__eq__   s0    NNeoo- >$$(=(==	
r   c                     | |k(   S r0    r2   s     r   __ne__zEdgeI.__ne__   s    5=  r   c                     t        |t              st        d| |       | j                  |j                  u r| j                  |j                  k  S | j                  j
                  |j                  j
                  k  S )N<)
isinstancer   r   r   r1   __name__r2   s     r   __lt__zEdgeI.__lt__   s^    %'#Cu5>>U__,''%*?*???>>**U__-E-EEEr   c                     	 | j                   S # t        $ r) t        | j                        | _         | j                   cY S w xY wr0   )_hashAttributeErrorhashr1   r   s    r   __hash__zEdgeI.__hash__   s<    	:: 	d223DJ::	s    /A A N)r;   
__module____qualname____doc__r   r   r   r   r    r"   r$   r&   r)   r,   r.   r4   r7   r<   rA   r6   r   r   r   r   6   sT    @=$$$$$$	$$$$
!Fr   r   c                   v    e Zd ZdZddZe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y)TreeEdgea  
    An edge that records the fact that a tree is (partially)
    consistent with the sentence.  A tree edge consists of:

    - A span, indicating what part of the sentence is
      consistent with the hypothesized tree.
    - A left-hand side, specifying the hypothesized tree's node
      value.
    - A 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 dot position, indicating which children are consistent
      with part of the sentence.  In particular, if ``dot`` is the
      dot position, ``rhs`` is the right-hand size, ``(start,end)``
      is the span, and ``sentence`` is the list of tokens in the
      sentence, then ``tokens[start:end]`` can be spanned by the
      children specified by ``rhs[:dot]``.

    For more information about edges, see the ``EdgeI`` interface.
    c                 h    || _         || _        t        |      }|| _        || _        ||||f| _        y)a  
        Construct a new ``TreeEdge``.

        :type span: tuple(int, int)
        :param span: A tuple ``(s, e)``, where ``tokens[s:e]`` is the
            portion of the sentence that is consistent with the new
            edge's structure.
        :type lhs: Nonterminal
        :param lhs: The new edge's left-hand side, specifying the
            hypothesized tree's node value.
        :type rhs: list(Nonterminal and str)
        :param rhs: The new edge's right-hand side, specifying the
            hypothesized tree's children.
        :type dot: 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
            ``sentence`` is the list of tokens in the sentence, then
            ``okens[span[0]:span[1]]`` can be spanned by the
            children specified by ``rhs[:dot]``.
        N)_span_lhstuple_rhs_dotr1   )r   r   r"   r$   r&   s        r   r   zTreeEdge.__init__   s:    , 
	Cj		 $c34r   c                 \    t        ||f| j                         | j                         d      S )a  
        Return a new ``TreeEdge`` formed from the given production.
        The new edge's left-hand side and right-hand side will
        be taken from ``production``; its span will be
        ``(index,index)``; and its dot position will be ``0``.

        :rtype: TreeEdge
        r   r   r"   r$   r&   )rF   r"   r$   )
productionindexs     r   from_productionzTreeEdge.from_production  s-     Z^^%5:>>;KQR
 	
r   c                     t        | j                  d   |f| j                  | j                  | j                  dz         S )a  
        Return a new ``TreeEdge`` formed from this edge.
        The new edge's dot position is increased by ``1``,
        and its end index will be replaced by ``new_end``.

        :param new_end: The new end index.
        :type new_end: int
        :rtype: TreeEdge
        r      rN   )rF   rH   rI   rK   rL   )r   new_ends     r   move_dot_forwardzTreeEdge.move_dot_forward$  s:     **Q-)						A	
 	
r   c                     | j                   S r0   )rI   r   s    r   r"   zTreeEdge.lhs6      yyr   c                     | j                   S r0   rH   r   s    r   r   zTreeEdge.span9      zzr   c                      | j                   d   S Nr   rY   r   s    r   r   zTreeEdge.start<      zz!}r   c                      | j                   d   S NrS   rY   r   s    r   r   zTreeEdge.end?  r]   r   c                 @    | j                   d   | j                   d   z
  S )NrS   r   rY   r   s    r   r    zTreeEdge.lengthB  s    zz!}tzz!},,r   c                     | j                   S r0   )rK   r   s    r   r$   zTreeEdge.rhsE  rW   r   c                     | j                   S r0   )rL   r   s    r   r&   zTreeEdge.dotH  rW   r   c                 F    | j                   t        | j                        k(  S r0   rL   lenrK   r   s    r   r,   zTreeEdge.is_completeK      yyC		N**r   c                 F    | j                   t        | j                        k7  S r0   rd   r   s    r   r.   zTreeEdge.is_incompleteN  rf   r   c                 z    | j                   t        | j                        k\  ry | j                  | j                      S r0   rd   r   s    r   r)   zTreeEdge.nextsymQ  s,    99DII&99TYY''r   c                 j   d| j                   d    d| j                   d    d}|| j                  ddz  }t        t        | j                              D ]4  }|| j
                  k(  r|dz  }|d	t        | j                  |         z  z  }6 t        | j                        | j
                  k(  r|dz  }|S )
N[r   :rS   ] 2z ->z *z %s)rH   rI   rangere   rK   rL   repr)r   stris      r   __str__zTreeEdge.__str__X  s    $**Q-$**Q-3DII''s499~&ADII~t54		!---C ' tyy>TYY&4KC
r   c                     d| z  S Nz
[Edge: %s]r6   r   s    r   __repr__zTreeEdge.__repr__d  s    d""r   Nr   )r;   rB   rC   rD   r   staticmethodrQ   rU   r"   r   r   r   r    r$   r&   r,   r.   r)   rr   ru   r6   r   r   rF   rF      sa    .5: 
 

$-++(
#r   rF   c                   ^    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y)LeafEdgea  
    An edge that records the fact that a leaf value is consistent with
    a word in the sentence.  A leaf edge consists of:

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

    A leaf edge's left-hand side is its leaf value, and its right hand
    side is ``()``.  Its span is ``[index, index+1]``, and its dot
    position is ``0``.
    c                 2    || _         || _        ||f| _        y)a  
        Construct a new ``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)_leaf_indexr1   )r   leafrP   s      r   r   zLeafEdge.__init__u  s     
 $e}r   c                     | j                   S r0   )r{   r   s    r   r"   zLeafEdge.lhs  rZ   r   c                 8    | j                   | j                   dz   fS r_   r|   r   s    r   r   zLeafEdge.span  s    T[[1_--r   c                     | j                   S r0   r   r   s    r   r   zLeafEdge.start  s    {{r   c                      | j                   dz   S r_   r   r   s    r   r   zLeafEdge.end  s    {{Qr   c                      yr_   r6   r   s    r   r    zLeafEdge.length      r   c                      yNr6   r6   r   s    r   r$   zLeafEdge.rhs  s    r   c                      yr\   r6   r   s    r   r&   zLeafEdge.dot  r   r   c                      y)NTr6   r   s    r   r,   zLeafEdge.is_complete      r   c                      yNFr6   r   s    r   r.   zLeafEdge.is_incomplete  s    r   c                      y r0   r6   r   s    r   r)   zLeafEdge.nextsym  r   r   c                 l    d| j                    d| j                   dz    dt        | j                         S )Nrj   rk   rS   rl   )r|   ro   r{   r   s    r   rr   zLeafEdge.__str__  s2    4;;-qq 1D4D3EFFr   c                     d| z  S rt   r6   r   s    r   ru   zLeafEdge.__repr__  s    t$$r   N)r;   rB   rC   rD   r   r"   r   r   r   r    r$   r&   r,   r.   r)   rr   ru   r6   r   r   ry   ry   h  sI    
-.G%r   ry   c                       e Zd 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fdZedfdZd Zd ZddZddZddZd Zy)Charta  
    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 ``select`` method can be used to select a specific collection
    of edges.  For example ``chart.select(is_complete=True, start=0)``
    yields all complete edges whose start indices are 0.  To ensure
    the efficiency of these selection operations, ``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 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                 x    t        |      | _        t        | j                        | _        | j	                          y)z
        Construct a new chart. The chart is initialized with the
        leaf edges corresponding to the terminal leaves.

        :type tokens: list
        :param tokens: The sentence that this chart will be used to parse.
        N)rJ   _tokensre   _num_leaves
initializer   tokenss     r   r   zChart.__init__  s,     V}t||, 	r   c                 .    g | _         i | _        i | _        y)z"
        Clear the chart.
        N)_edges_edge_to_cpls_indexesr   s    r   r   zChart.initialize  s    
    r   c                     | j                   S )z[
        Return the number of words in this chart's sentence.

        :rtype: int
        )r   r   s    r   
num_leaveszChart.num_leaves  s     r   c                      | j                   |   S )z\
        Return the leaf value of the word at the given index.

        :rtype: str
        r   )r   rP   s     r   r}   z
Chart.leaf  s     ||E""r   c                     | j                   S )z{
        Return a list of the leaf values of each word in the
        chart's sentence.

        :rtype: list(str)
        r   r   s    r   leaveszChart.leaves  s     ||r   c                      | j                   dd S )z
        Return a list of all edges in this chart.  New edges
        that are added to the chart after the call to edges()
        will *not* be contained in this list.

        :rtype: list(EdgeI)
        :see: ``iteredges``, ``select``
        N)r   r   s    r   edgeszChart.edges	  s     {{1~r   c                 ,    t        | j                        S )a  
        Return an iterator over the edges in this chart.  It is
        not guaranteed that new edges which are added to the
        chart before the iterator is exhausted will also be generated.

        :rtype: iter(EdgeI)
        :see: ``edges``, ``select``
        )iterr   r   s    r   	iteredgeszChart.iteredges  s     DKK  r   c                 ,    t        | j                        S )zZ
        Return the number of edges contained in this chart.

        :rtype: int
        )re   r   r   s    r   	num_edgeszChart.num_edges"  s     4%%&&r   c                 6   i k(  rt        | j                        S t        j                               }t	        |      }|| j
                  vr| j                  |       t	        fd|D              }t        | j
                  |   j                  |g             S )a`  
        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.  ``restrictions``
        can be used to restrict the set of edges that will be
        generated.

        :param span: Only generate edges ``e`` where ``e.span()==span``
        :param start: Only generate edges ``e`` where ``e.start()==start``
        :param end: Only generate edges ``e`` where ``e.end()==end``
        :param length: Only generate edges ``e`` where ``e.length()==length``
        :param lhs: Only generate edges ``e`` where ``e.lhs()==lhs``
        :param rhs: Only generate edges ``e`` where ``e.rhs()==rhs``
        :param nextsym: Only generate edges ``e`` where
            ``e.nextsym()==nextsym``
        :param dot: Only generate edges ``e`` where ``e.dot()==dot``
        :param is_complete: Only generate edges ``e`` where
            ``e.is_complete()==is_complete``
        :param is_incomplete: Only generate edges ``e`` where
            ``e.is_incomplete()==is_incomplete``
        :rtype: iter(EdgeI)
        c              3   (   K   | ]	  }|     y wr0   r6   ).0keyrestrictionss     r   	<genexpr>zChart.select.<locals>.<genexpr>M  s     =*3\#&*s   )r   r   sortedkeysrJ   r   
_add_indexget)r   r   
restr_keysvalss    `  r   selectzChart.select*  s    0 2$$ L--/0
:&
 T]]*OOJ'=*==DMM*-11$;<<r   c                     |D ]   }t        t        |      rt        d|z         i x}| j                  |<   | j                  D ]7  t        fd|D              }|j                  |g       j                         9 y)z
        A helper function for ``select``, which creates a new index for
        a given set of attributes (aka restriction keys).
        zBad restriction: %sc              3   @   K   | ]  } t        |               y wr0   getattrr   r   edges     r   r   z#Chart._add_index.<locals>.<genexpr>_       D#+s+-   N)hasattrr   
ValueErrorr   r   rJ   
setdefaultappend)r   r   r   rP   r   r   s        @r   r   zChart._add_indexP  sz     C5#& !6!<== 
 -/.j) KKDDDDDT2&--d3  r   c                     | j                   j                         D ]:  \  }}t        fd|D              }|j                  |g       j	                         < y)zs
        A helper function for ``insert``, which registers the new
        edge with all existing indexes.
        c              3   @   K   | ]  } t        |               y wr0   r   r   s     r   r   z/Chart._register_with_indexes.<locals>.<genexpr>h  r   r   N)r   itemsrJ   r   r   )r   r   r   rP   r   s    `   r   _register_with_indexeszChart._register_with_indexesb  sL    
 "&!4!4!6JDDDDT2&--d3 "7r   c                 z    | j                  |      }|D cg c]  }||fz   
 }} | j                  |g| S c c}w )zT
        Add a new edge to the chart, using a pointer to the previous edge.
        )child_pointer_listsinsert)r   new_edgeprevious_edge
child_edgecplscplnew_cplss          r   insert_with_backpointerzChart.insert_with_backpointero  sJ     ''63784CC:-'48t{{8/h// 9s   8c                     || j                   vr"| j                  |       | j                  |       | j                   j                  |t	                     }d}|D ]  }t        |      }||vsd||<   d} |S )a  
        Add a new edge to the chart, and return True if this operation
        modified the chart.  In particular, return true iff the chart
        did not already contain ``edge``, or if it did not already associate
        ``child_pointer_lists`` with ``edge``.

        :type edge: EdgeI
        :param edge: The new edge
        :type child_pointer_lists: sequence of tuple(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 ``edge``.
        :rtype: bool
        FT)r   _append_edger   r   r
   rJ   )r   r   r   r   chart_was_modifiedchild_pointer_lists         r   r   zChart.insertw  s      t)))d#''- !!,,T;=A""5!&'9!:!-+/'(%)" #6 "!r   c                 :    | j                   j                  |       y r0   )r   r   r   r   s     r   r   zChart._append_edge  s    4 r   c              #      K   | j                  d| j                  |      D ]  }| j                  ||d      E d{      y7 w)z
        Return an iterator of the complete tree structures that span
        the entire chart, and whose root node is ``root``.
        r   )r   r   r"   T)
tree_classcompleteN)r   r   trees)r   rootr   r   s       r   parseszChart.parses  sC     
 KKaT-=-=4KHDzz$:zMMM IMs   9AAAFc                 >    t        | j                  ||i |            S )a,  
        Return an iterator of the tree structures that are associated
        with ``edge``.

        If ``edge`` is incomplete, then the unexpanded children will be
        encoded as childless subtrees, whose node value is the
        corresponding terminal or nonterminal.

        :rtype: list(Tree)
        :note: If two trees share a common subtree, then the same
            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.
        )memor   )r   _trees)r   r   r   r   s       r   r   zChart.trees  s      DKKhRJKOPPr   c           
         ||v r||   S |r|j                         rg S t        |t              r&| j                  |j	                            }|g||<   |gS g ||<   g }|j                         j                         }| j                  |      D ]R  }|D 	cg c]  }	| j                  |	|||       }
}	t        j                  |
 D ]  }|j                   |||              T |j                         rM|j                         |j                         d D cg c]  } ||g        }}|D ]  }|j                  |        |||<   |S c c}	w c c}w )z
        A helper function for ``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.
        N)r.   r:   ry   r   r   r"   symbolr   r   	itertoolsproductr   r$   r&   extend)r   r   r   r   r   r}   r   r"   r   cpchild_choiceschildrenelt
unexpandedtrees                  r   r   zChart._trees  s\    4<: **,I dH%<<

-DDJ6M T
hhj! ++D1C TWWSVRT[[XtZHSVMW &--}=ZX67 > 2 9=DHHJL9QR9Q#*S"-9QJRJ'  T
 ! X Ss   EEc                 V    | j                   j                  |i       j                         S )z
        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.

        :rtype: list(list(EdgeI))
        )r   r   r   r   s     r   r   zChart.child_pointer_lists  s&     !!%%dB/4466r   Nc                 d   |d| j                         dz   z  }|j                         |j                         }}ddd|dz
  z  z   |z  z   }||k(  r|j                         r|dz  }n|dz  }n|j                         r@|j	                         d| j
                  fk(  r!|d	d
|z  ||z
  dz
  z  z   d
|dz
  z  z   dz   z  }nQ|j                         r!|d	d|z  ||z
  dz
  z  z   d|dz
  z  z   dz   z  }n |d	d|z  ||z
  dz
  z  z   d|dz
  z  z   dz   z  }|d|dz
  z  dz   | j
                  |z
  z  z  }|d|z  z   S )z
        Return a pretty-printed string representation of a given edge
        in this chart.

        :rtype: str
        :param width: The number of characters allotted to each
            index in the sentence.
        2   rS   |. #>r   rj   =]-z| %s)r   r   r   r,   r   r   )r   r   widthr   r   rp   s         r   pretty_format_edgezChart.pretty_format_edge  s}    =4??,q01E

dhhjS3%!),,55 C<!s
s
 DIIKAt7G7G3H$H3#+#+/::SEAI=NNQTTTC3#+#+/::SEAI=NNQTTTC3#+#+/::SEAI=NNQTTTCuqy!C'D,<,<s,BCCVd]""r   c                     |d| j                         dz   z  }| j                  ?|dkD  r:d}| j                  D ]"  }||d|dz
   j                  |dz
        dz   z  }$ |dz  }|S d}|S )z
        Return a pretty-printed string representation of this
        chart's leaves.  This string can be used as a header
        for calls to ``pretty_format_edge``.
        Nr   rS   z|.r   r    )r   r   center)r   r   headertoks       r   pretty_format_leaveszChart.pretty_format_leaves  s     =4??,q01E<<#	F||#k	*11%!)<sBB $cMF  Fr   c                      d j                         dz   z  t        d  D              }|D cg c]  \  }}}|
 }}} j                        dz   dj                   fd|D              z   S c c}}w )z
        Return a pretty-printed string representation of this chart.

        :param width: The number of characters allotted to each
            index in the sentence.
        :rtype: str
        r   rS   c              3   ^   K   | ]%  }|j                         |j                         |f ' y wr0   )r    r   )r   es     r   r   z&Chart.pretty_format.<locals>.<genexpr>=  s$     @4a
AGGIq14s   +-
c              3   B   K   | ]  }j                  |        y wr0   )r   )r   r   r   r   s     r   r   z&Chart.pretty_format.<locals>.<genexpr>C  s     O//e<s   )r   r   r   join)r   r   r   _r   s   ``   r   pretty_formatzChart.pretty_format1  s     =4??,q01E @4@@$)*Ey1aE* %%e,iiOOOP	
 +s   A2c           	         d}|dz  }|dz  }|dz  }t        | j                         dd      D ]  }|dk(  r|dz  }t        | j                         dz         D ]X  }|dk(  sG|| j                  |dz
     j	                         k  s$|| j                  |dz
     j                         k\  sO|d	||fz  z  }Z  |d
z  }t        | j                         dz         D ]  }|dz  }t        | j                         dz         D ]X  }|dk(  sG|| j                  |dz
     j	                         k  s$|| j                  |dz
     j                         k\  sO|d||fz  z  }Z |dz  } |dz  }|dz  }|dz  }t        | j                               D ]  }|d| j                  |      |dz   fz  z  }  |dz  }|dz  }t        |       D ]  \  }}t        |j	                               D ]  }|d||dz   |dz   |dz   fz  z  } |d|j	                         |dz   |j                         |dz   |fz  z  }t        |j                         | j                               D ]  }|d||dz   |dz   |dz   fz  z  }  |dz  }|S )Nzdigraph nltk_chart {
z  rankdir=LR;
z  node [height=0.1,width=0.1];
z*  node [style=filled, color="lightgray"];
r   z&  node [style=filled, color="black"];
rS   z  %04d.%04d [label=""];
z/  x [style=invis]; x->0000.0000 [style=invis];
z  {rank=same;z
 %04d.%04dz}
z"  edge [style=invis, weight=100];
z  node [shape=plaintext]
z  0000.0000z->%s->%04d.0000z;

z   edge [style=solid, weight=1];
z*  %04d.%04d -> %04d.%04d [style="invis"];
z'  %04d.%04d -> %04d.%04d [label="%s"];
)rn   r   r   r   r   r   r}   	enumerate)r   syxr   s        r   dot_digraphzChart.dot_digraphJ  s   $		//	:: t~~'R0AAv>>4??,q016QU+1133qDKKA<N<R<R<T7T41v==A	 2 1 	
?? t(1,-A A4>>+a/06QU+1133qDKKA<N<R<R<T7TA..A	 1
 JA . 	
22	))	]t()A"diilAE%:::A *	W 	
00 GAt4::<(BEEE	F   ) ;

A
A?  A 488:t'89BEEE	F   : ', 	
U
r   r0   )r;   rB   rC   rD   r   r   r   r}   r   r   r   __iter__r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r  r
  r6   r   r   r   r     s    8$ #		! H'$=L4$40"B! '+ N &*E Q"4l	7 #D&
2@r   r   c                       e Zd ZdZd Zd Zy)
ChartRuleIa  
    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 ``NUM_EDGES``.  In
    particular:

    - A chart rule with ``NUM_EDGES=0`` specifies what new edges are
      licensed, regardless of existing edges.
    - A chart rule with ``NUM_EDGES=1`` specifies what new edges are
      licensed by a single existing edge.
    - A chart rule with ``NUM_EDGES=2`` specifies what new edges are
      licensed by a pair of existing edges.

    :type NUM_EDGES: 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                     t               )a  
        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.

        :type edges: list(EdgeI)
        :param edges: A set of existing edges.  The number of edges
            that should be passed to ``apply()`` is specified by the
            ``NUM_EDGES`` class variable.
        :rtype: iter(EdgeI)
        r   r   chartgrammarr   s       r   applyzChartRuleI.apply  s     "##r   c                     t               )a+  
        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: iter(EdgeI)
        r   )r   r  r  s      r   apply_everywherezChartRuleI.apply_everywhere  r'   r   N)r;   rB   rC   rD   r  r  r6   r   r   r  r    s    &$	$r   r  c                   "    e Zd ZdZd Zd Zd Zy)AbstractChartRuleaq  
    An abstract base class for chart rules.  ``AbstractChartRule``
    provides:

    - A default implementation for ``apply``.
    - A default implementation for ``apply_everywhere``,
      (Currently, this implementation assumes that ``NUM_EDGES <= 3``.)
    - A default implementation for ``__str__``, which returns a
      name based on the rule's class name.
    c                     t               r0   r   r  s       r   r  zAbstractChartRule.apply  s    !##r   c           
   #     K   | j                   dk(  r| j                  ||      E d {    y | j                   dk(  r#|D ]  }| j                  |||      E d {     y | j                   dk(  r+|D ]%  }|D ]  }| j                  ||||      E d {      ' y | j                   dk(  r3|D ]-  }|D ]&  }|D ]  }| j                  |||||      E d {    ! ( / y t        d      7 7 7 Z7 w)Nr   rS         z&NUM_EDGES>3 is not currently supported)	NUM_EDGESr  AssertionError)r   r  r  e1e2e3s         r   r  z"AbstractChartRule.apply_everywhere  s     >>Qzz%111^^q ::eWb999  ^^q B#zz%"bAAA    ^^q B##'::eWb"b#III $    !!IJJ% 2 :
 B JsE   %C.C&/C.C(7C.C*?C.C,
C.(C.*C.,C.c                 X    t        j                  dd| j                  j                        S )Nz([a-z])([A-Z])z\1 \2)resubr   r;   r   s    r   rr   zAbstractChartRule.__str__  s     vv&$..2I2IJJr   N)r;   rB   rC   rD   r  r  rr   r6   r   r   r  r    s    	$
K.Kr   r  c                       e Zd ZdZdZd Zy)FundamentalRulea  
    A rule that joins two adjacent edges to form a single combined
    edge.  In particular, this rule specifies that any pair of edges

    - ``[A -> alpha \* B beta][i:j]``
    - ``[B -> gamma \*][j:k]``

    licenses the edge:

    - ``[A -> alpha B * beta][i:j]``
    r  c              #   @  K   |j                         rR|j                         rB|j                         |j                         k(  r!|j	                         |j                         k(  sy |j                  |j                               }|j                  |||      r| y y wr0   )r.   r,   r   r   r)   r"   rU   r   r   r  r  	left_edge
right_edger   s         r   r  zFundamentalRule.apply  s      ##%&&(:#3#3#55!!#z~~'77 --jnn.>? ((9jIN Js   BBNr;   rB   rC   rD   r  r  r6   r   r   r$  r$    s    
 Ir   r$  c                   &    e Zd ZdZdZd Zd Zd Zy)SingleEdgeFundamentalRulea  
    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 -> alpha \* B beta][i:j]``
    - ``[B -> gamma \*][j:k]``

    licenses the edge:

    - ``[A -> alpha B * beta][i:j]``

    if the other edge is already in the chart.

    :note: This is basically ``FundamentalRule``, with one edge left
        unspecified.
    rS   c              #      K   |j                         r| j                  |||      E d {    y | j                  |||      E d {    y 7 !7 wr0   )r.   _apply_incomplete_apply_complete)r   r  r  r   s       r   r  zSingleEdgeFundamentalRule.apply*  sL     --eWdCCC++E7DAAA DAs!   'AAAAAAc              #      K   |j                  |j                         d|j                               D ]9  }|j                  |j	                               }|j                  |||      s6| ; y wNF)r   r,   r)   )r   r   r"   rU   r   r   )r   r  r  r(  r'  r   s         r   r.  z)SingleEdgeFundamentalRule._apply_complete0  se       "z~~?O & 
I !11*..2BCH,,Xy*M
   A&A0)A0c              #      K   |j                  |j                         d|j                               D ]9  }|j                  |j                               }|j	                  |||      s6| ; y wNT)r   r,   r"   )r   r   r)   rU   r   r&  s         r   r-  z+SingleEdgeFundamentalRule._apply_incomplete8  sd     ,,--/t9J9J9L ' 
J !11*..2BCH,,Xy*M
r1  N)r;   rB   rC   rD   r  r  r.  r-  r6   r   r   r+  r+    s    $ IBr   r+  c                       e Zd ZdZd Zy)LeafInitRuler   c              #      K   t        |j                               D ]4  }t        |j                  |      |      }|j	                  |d      s1| 6 y wr   )rn   r   ry   r}   r   )r   r  r  rP   r   s        r   r  zLeafInitRule.applyI  sF     5++-.E

5 159H||Hb) /s   A
AAN)r;   rB   rC   r  r  r6   r   r   r5  r5  F  s    Ir   r5  c                       e Zd ZdZdZd Zy)TopDownInitRulea  
    A rule licensing edges corresponding to the grammar productions for
    the grammar's start symbol.  In particular, this rule specifies that
    ``[S -> \* alpha][0:i]`` is licensed for each grammar production
    ``S -> alpha``, where ``S`` is the grammar's start symbol.
    r   c              #      K   |j                  |j                               D ]/  }t        j                  |d      }|j	                  |d      s,| 1 y w)Nr"   r   r6   )productionsr   rF   rQ   r   )r   r  r  prodr   s        r   r  zTopDownInitRule.apply_  sI     ''GMMO'<D//a8H||Hb) =s   AAANr)  r6   r   r   r8  r8  U  s     Ir   r8  c                       e Zd ZdZdZd Zy)TopDownPredictRulea|  
    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 -> alpha \* B beta][i:j]`` licenses the edge
    ``[B -> \* gamma][j:j]`` for each grammar production ``B -> gamma``.

    :note: This rule corresponds to the Predictor Rule in Earley parsing.
    rS   c              #      K   |j                         ry |j                  |j                               D ]=  }t        j	                  ||j                               }|j                  |d      s:| ? y w)Nr:  r6   )r,   r;  r)   rF   rQ   r   r   r   r  r  r   r<  r   s         r   r  zTopDownPredictRule.applys  s^     ''DLLN';D//dhhjAH||Hb) <   A+A5.A5Nr)  r6   r   r   r>  r>  f  s     Ir   r>  c                       e Zd ZdZd Zd Zy)CachedTopDownPredictRulea1  
    A cached version of ``TopDownPredictRule``.  After the first time
    this rule is applied to an edge with a given ``end`` and ``next``,
    it will not generate any more edges for edges with that ``end`` and
    ``next``.

    If ``chart`` or ``grammar`` are changed, then the cache is flushed.
    c                 <    t         j                  |        i | _        y r0   )r>  r   _doner   s    r   r   z!CachedTopDownPredictRule.__init__  s    ##D)
r   c              #   :  K   |j                         ry |j                         |j                         }}t        |      sy | j                  j                  ||fd      }|d   |u r|d   |u ry |j                  |      D ]  }|j                         rF|j                         d   }t        |      r(||j                         k\  s||j                  |      k7  rYt        j                  ||      }	|j                  |	d      s|	  ||f| j                  ||f<   y w)N)NNr   rS   r:  r6   )r,   r)   r   r   rE  r   r;  r$   r   r   r}   rF   rQ   r   )
r   r  r  r   r)   rP   doner<  firstr   s
             r   r  zCachedTopDownPredictRule.apply  s    g&
 zz~~w.=7eQ7 2 ''G'4D xxz
1u% 0 0 22euzz%?P6P //e<H||Hb) 5 ',W%5

7E>"s   C>DDN)r;   rB   rC   rD   r   r  r6   r   r   rC  rC  |  s    6r   rC  c                       e Zd ZdZdZd Zy)BottomUpPredictRulea"  
    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 -> alpha \*]`` licenses
    the edge ``[B -> \* A beta]`` for each grammar production ``B -> A beta``.
    rS   c              #      K   |j                         ry |j                  |j                               D ]=  }t        j	                  ||j                               }|j                  |d      s:| ? y w)Nr$   r6   )r.   r;  r"   rF   rQ   r   r   r@  s         r   r  zBottomUpPredictRule.apply  s^     ''DHHJ'7D//djjlCH||Hb) 8rA  Nr)  r6   r   r   rJ  rJ    s     Ir   rJ  c                       e Zd ZdZdZd Zy)BottomUpPredictCombineRulea  
    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 -> alpha \*]``
    licenses the edge ``[B -> A \* beta]`` for each grammar
    production ``B -> A beta``.

    :note: This is like ``BottomUpPredictRule``, but it also applies
        the ``FundamentalRule`` to the resulting edge.
    rS   c              #     K   |j                         ry |j                  |j                               D ]R  }t        |j	                         |j                         |j                         d      }|j                  ||f      sO| T y wNrL  rS   )r.   r;  r"   rF   r   r$   r   r@  s         r   r  z BottomUpPredictCombineRule.apply  sj     ''DHHJ'7D		TXXZQGH||Htg. 8s   B B
B
Nr)  r6   r   r   rN  rN    s    	 Ir   rN  c                       e Zd ZdZdZd Zy)EmptyPredictRulezi
    A rule that inserts all empty productions as passive edges,
    in every position in the chart.
    r   c              #      K   |j                  d      D ]P  }t        |j                         dz         D ]/  }t        j	                  ||      }|j                  |d      s,| 1 R y w)NT)emptyrS   r6   )r;  rn   r   rF   rQ   r   )r   r  r  r<  rP   r   s         r   r  zEmptyPredictRule.apply  s`     ''d'3Du//1A56#33D%@<<"-"N 7 4s   AA) 	A)Nr)  r6   r   r   rR  rR    s    
 I#r   rR  c                       e Zd Zd Zd Zy)!FilteredSingleEdgeFundamentalRulec              #     K   |j                         }||j                         k  xr |j                  |      }|j                  |j	                         d|j                               D ]d  }t        |||j                         |j                               s.|j                  |j                               }|j                  |||      sa| f y wr0  )r   r   r}   r   r   r"   _bottomup_filterr$   r&   rU   r   )r   r  r  r(  r   	nexttokenr'  r   s           r   r.  z1FilteredSingleEdgeFundamentalRule._apply_complete  s     nn%**,,@C	  "z~~?O & 
I  IMMOY]]_U$55jnn6FG009jQ"N
   BC2C
Cc              #     K   |j                  |j                         d|j                               D ]  }|j                         }||j                         k  xr |j	                  |      }t        |||j                         |j                               sd|j                  |j                               }|j                  |||      s|  y wr3  )
r   r   r)   r   r}   rX  r$   r&   rU   r   )r   r  r  r'  r(  r   rY  r   s           r   r-  z3FilteredSingleEdgeFundamentalRule._apply_incomplete  s     ,,--/t9J9J9L ' 
J .."Ce..00DUZZ_IIMMOY]]_U$55jnn6FG009jQ"N
rZ  N)r;   rB   rC   r.  r-  r6   r   r   rV  rV    s    	#	#r   rV  c                       e Zd Zd Zy)"FilteredBottomUpPredictCombineRulec              #     K   |j                         ry |j                         }||j                         k  xr |j                  |      }|j	                  |j                               D ]n  }t        |||j                               st        |j                         |j                         |j                         d      }|j                  ||f      sk| p y wrP  )r.   r   r   r}   r;  r"   rX  r$   rF   r   r   )r   r  r  r   r   rY  r<  r   s           r   r  z(FilteredBottomUpPredictCombineRule.apply  s     hhj%**,,@C	''DHHJ'7DDHHJ?#DIIKTXXZK<<4'2"N	 8s   BC	ACCN)r;   rB   rC   r  r6   r   r   r]  r]    s    
#r   r]  c                 z    t        |      |dz   k  ry||dz      }t        |      r||k(  S | j                  ||      S )NrS   T)re   r   is_leftcorner)r  rY  r$   r&   _nexts        r   rX  rX    sH    
3x37aLE5E!!$$UI66r   c                   @    e Zd ZdZedddefdZd Zd Zdd	Z	e
fd
Zy)ChartParsera  
    A generic chart parser.  A "strategy", or list of
    ``ChartRuleI`` instances, is used to decide what edges to add to
    the chart.  In particular, ``ChartParser`` uses the following
    algorithm to parse texts:

    | Until no new edges are added:
    |   For each *rule* in *strategy*:
    |     Apply *rule* to any applicable edges in the chart.
    | Return any complete parses in the chart
    r   r   Tc                 <   || _         || _        || _        || _        || _        || _        g | _        g | _        |D ]_  }|j                  dk(  r| j                  j                  |       .|j                  dk(  r| j                  j                  |       Yd| _        a y)a  
        Create a new chart parser, that uses ``grammar`` to parse
        texts.

        :type grammar: CFG
        :param grammar: The grammar used to parse texts.
        :type strategy: list(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: int
        :param trace: The level of tracing that should be used when
            parsing a text.  ``0`` will generate no tracing output;
            and higher numbers will produce more verbose tracing
            output.
        :type trace_chart_width: 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: 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.
        r   rS   FN)
_grammar	_strategy_trace_trace_chart_width_use_agenda_chart_class_axioms_inference_rulesr  r   )r   r  strategytracetrace_chart_width
use_agendachart_classrules           r   r   zChartParser.__init__I  s    B  !"3 &' "D~~"##D)1$%%,,T2#(  r   c                     | j                   S r0   re  r   s    r   r  zChartParser.grammar}  s    }}r   c                 |    |sy |dkD  }|D ]/  }|rt        d|z         d}t        |j                  ||             1 y )NrS   z%s:F)printr   )r   r  rr  	new_edgesrn  
edge_widthprint_rule_headerr   s           r   _trace_new_edgeszChartParser._trace_new_edges  sF    !AID edl#$)!%**4<=	 r   Nc                 J   || j                   }| j                  }t        |      }| j                  j	                  |       | j                  |      }| j                  }| j                  |j                         dz   z  }|rt        |j                  |             | j                  r| j                  D ])  }t        |j                  ||            } ||||||       + | j                  }	|j                         }
|
j                          |
rI|
j!                         }|	D ]1  }t        |j                  |||            }|r ||||||       |
|z  }
3 |
rI|S d}|rHd}| j"                  D ]4  }t        |j%                  ||            }t'        |      } ||||||       6 |rH|S )z
        Return the final parse ``Chart`` from which all possible
        parse trees can be extracted.

        :param tokens: The sentence to be parsed
        :type tokens: list(str)
        :rtype: Chart
        rS   TF)rg  rz  listre  check_coveragerj  rh  r   rv  r   ri  rk  r  rl  r   reversepoprf  r  re   )r   r   rn  trace_new_edgesr  r  trace_edge_widthaxiomrw  inference_rulesagendar   rr  edges_addeds                 r   chart_parsezChartParser.chart_parse  s    =KKE//f$$V,!!&)--  22u7G7G7IA7MN%,,-=>? UG!<=	ui@PQ & #33O[[]F NNzz|+D $TZZw%E FI'tYGWXi'F	 , &  K# NND $T%:%:5'%J KI"%i.K#E4ECST +  r   c                     | j                  |      }t        |j                  | j                  j	                         |            S N)r   )r  r   r   re  r   )r   r   r   r  s       r   parsezChartParser.parse  s7      (ELL!4!4!6:LNOOr   r0   )r;   rB   rC   rD   BU_LC_STRATEGYr   r   r  rz  r  r	   r  r6   r   r   rc  rc  <  s9    
  2)h>5n (, Pr   rc  c                       e Zd ZdZd Zy)TopDownChartParserzl
    A ``ChartParser`` using a top-down parsing strategy.
    See ``ChartParser`` for more information.
    c                 <    t        j                  | |t        fi | y r0   )rc  r   TD_STRATEGYr   r  parser_argss      r   r   zTopDownChartParser.__init__  s    T7KG;Gr   Nr;   rB   rC   rD   r   r6   r   r   r  r    s    
Hr   r  c                       e Zd ZdZd Zy)BottomUpChartParserzm
    A ``ChartParser`` using a bottom-up parsing strategy.
    See ``ChartParser`` for more information.
    c                     t        |t              rt        j                  dt               t        j                  | |t        fi | y )NzTBottomUpChartParser only works for CFG, use BottomUpProbabilisticChartParser instead)category)r:   r   warningswarnDeprecationWarningrc  r   BU_STRATEGYr  s      r   r   zBottomUpChartParser.__init__  s9    gt$MM?+
 	T7KG;Gr   Nr  r6   r   r   r  r    s    
Hr   r  c                       e Zd ZdZd Zy)BottomUpLeftCornerChartParserz
    A ``ChartParser`` using a bottom-up left-corner parsing strategy.
    This strategy is often more efficient than standard bottom-up.
    See ``ChartParser`` for more information.
    c                 <    t        j                  | |t        fi | y r0   )rc  r   r  r  s      r   r   z&BottomUpLeftCornerChartParser.__init__  s    T7NJkJr   Nr  r6   r   r   r  r    s    Kr   r  c                       e Zd Zd Zy)LeftCornerChartParserc                 r    |j                         st        d      t        j                  | |t        fi | y )NzCLeftCornerParser only works for grammars without empty productions.)is_nonemptyr   rc  r   LC_STRATEGYr  s      r   r   zLeftCornerChartParser.__init__  s7    ""$X  	T7KG;Gr   N)r;   rB   rC   r   r6   r   r   r  r    s    Hr   r  c                   l    e Zd ZdZg dfdZd Zd Zd Zd Zd Z	d	 Z
d
 ZefdZd Zd Zd ZefdZy)SteppingChartParsera  
    A ``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 ``initialize`` method is used to start parsing a text.  ``step``
    adds a single edge to the chart.  ``set_strategy`` changes the
    strategy used by the chart parser.  ``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 ``step`` must restart
        the parsing algorithm.
    r   c                 ^    d | _         d | _        d| _        t        j	                  | |||       y r   )_chart_current_chartrule_restartrc  r   )r   r  rm  rn  s       r   r   zSteppingChartParser.__init__
  s,    "&T7He<r   c                 D    t        t        |            | _        d| _        y)zBegin parsing the given tokens.TN)r   r|  r  r  r   s     r   r   zSteppingChartParser.initialize  s    DL)r   c              #     K   | j                   t        d      	 d| _        d| j                   j                         dz   z  }| j	                         D ]l  }| j
                  dkD  rt        | j                         | j
                  dkD  r%t        | j                   j                  ||             | | j                  sl n d w)a  
        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 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 None when no more edges can be
        added with the current strategy and grammar.
        Nz Parser must be initialized firstFr   rS   r   )	r  r   r  r   _parserg  rv  r  r   )r   wr   s      r   stepzSteppingChartParser.step  s       ;;?@@!DMt{{--/!34A[[];;?$112;;?$++88A>?== # 
 s   B;C>Cc              #      K   | j                   }| j                  }d}|dkD  rAd}| j                  D ])  }|| _        |j	                  ||      D ]  }|dz  }|  + |dkD  r@yyw)z
        A generator that implements the actual parsing algorithm.
        ``step`` iterates through this generator, and restarts it
        whenever the parser's strategy, grammar, or chart is modified.
        rS   r   N)r  re  rf  r  r  )r   r  r  r  rr  r   s         r   r  zSteppingChartParser._parse>  sp      --AoK*.'..ug>A1$KG ? ' Aos   AA$"A$c                     | j                   S )z(Return the strategy used by this parser.)rf  r   s    r   rm  zSteppingChartParser.strategyS  s    ~~r   c                     | j                   S )z'Return the grammar used by this parser.rt  r   s    r   r  zSteppingChartParser.grammarW  s    }}r   c                     | j                   S )z-Return the chart that is used by this parser.)r  r   s    r   r  zSteppingChartParser.chart[  s    {{r   c                     | j                   S )z<Return the chart rule used to generate the most recent edge.)r  r   s    r   current_chartrulez%SteppingChartParser.current_chartrule_  s    &&&r   c                 j    | j                   j                  | j                  j                         |      S )z8Return the parse trees currently contained in the chart.)r  r   re  r   )r   r   s     r   r   zSteppingChartParser.parsesc  s&    {{!!$--"5"5"7DDr   c                 F    || j                   k(  ry|dd | _         d| _        y)a
  
        Change the strategy that the parser uses to decide which edges
        to add to the chart.

        :type strategy: list(ChartRuleI)
        :param strategy: A list of rules that should be used to decide
            what edges to add to the chart.
        NT)rf  r  )r   rm  s     r   set_strategyz SteppingChartParser.set_strategyk  s%     t~~%!!r   c                 >    || j                   u ry|| _         d| _        y)z&Change the grammar used by the parser.NT)re  r  )r   r  s     r   set_grammarzSteppingChartParser.set_grammary  s    dmm#r   c                 >    || j                   u ry|| _         d| _        y)z)Load a given chart into the chart parser.NT)r  r  )r   r  s     r   	set_chartzSteppingChartParser.set_chart  s    DKKr   c                     t        |      }| j                  j                  |       | j                  |       | j	                         D ]  }| n | j                  |      S r  )r|  re  r}  r   r  r   )r   r   r   r   s       r   r  zSteppingChartParser.parse  sX    f$$V, 	 Ay 
 {{j{11r   N)r;   rB   rC   rD   r   r   r  r  rm  r  r  r  r	   r   r  r  r  r  r6   r   r   r  r    s[      *,1 =B*' !% E (, 2r   r  c                  0    ddl m}  | j                  d      S )Nr   CFGz
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"
)nltk.grammarr  
fromstringr  s    r   demo_grammarr    s     >>	 r   c           	         ddl }ddl}ddlm}	m}
m} t               }|rt        d       t        |       t        d       t        |       |j                         }t        |       t                | t        d       t        d       t        d       t        d	       t        d
       t        d       t        dd       |j                  j                         j                         } t                t        |       } | dvrt        d       yi }dt        fdt        fdt        fdt         fd}g }| |v r| g}| dk(  rd}|D ]  }t        d||   d   z          t                t#        |||   d   |      }|j                         }|j%                  |      }t'        |j)                  |j+                                     }|j                         |z
  |||   d   <   t        dt-        |j/                                      |rt-        |      |k(  sJ d       |r|D ]  }t        |        nt        dt-        |             t                 | dv rt        d       t                |j                         }t1        ||      }|j3                  |       t5        d       D ]  }t        d!       |j7                  t               t9        |j;                               D ]  \  }}|d"kD  s| n t        d#       |j7                  t               t9        |j;                               D ]  \  }}|d"kD  s|   |j                         |z
  |d$<   t        dt-        |j=                         j/                                      |r,t-        t'        |j)                                     |k(  sJ d       |r!|j)                         D ]  }t        |        n,t        dt-        t'        |j)                                            t                |r|syt        d%       t                t?        d& |D              }d'tA        |      z   d(z   }|jC                         }tE        |d) *      D ]  \  }}t        |||fz          y)+z/
    A demonstration of the chart parsers.
    r   N)r  
Productionnonterminalsz	* Grammarz* Sentence:z  1: Top-down chart parserz  2: Bottom-up chart parserz'  3: Bottom-up left-corner chart parserz3  4: Left-corner chart parser with bottom-up filterz=  5: Stepping chart parser (alternating top-down & bottom-up)z  6: All parsersz
Which parser (1-6)? r   )r   123456zBad parser numberzTop-downz	Bottom-upzBottom-up left-cornerzFiltered left-corner)1rm   3461234z* Strategy: rS   )rn  zNr edges in chart:zNot all parses foundz	Nr trees:56z,* Strategy: Stepping (top-down vs bottom-up)   z*** SWITCH TO TOP DOWN   z*** SWITCH TO BOTTOM UPSteppingz* Parsing timesc              3   2   K   | ]  }t        |        y wr0   )re   )r   r   s     r   r   zdemo.<locals>.<genexpr>0  s     +UcSUs   %zs parser: %6.3fsecc                     | d   S r_   r6   )as    r   <lambda>zdemo.<locals>.<lambda>3  s    qtr   )r   )#systimenltkr  r  r  r  rv  splitstdinreadlinestriprp   r  r  r  r  rc  r  r|  r   r   re   r   r  r   rn   r  r  r  r  maxro   r   r   )choiceprint_timesprint_grammarprint_treesrn  sent	numparsesr  r  r  r  r  r  r   times
strategieschoicesrm  r   tr  r   r   rq   jr   maxlenformattimes_itemsparsers                                 r   demor    s    22 nGkg 
-	$KZZ\F	&M	G ~*++,78CDMN !&C0##%++-[FX!" E +&;'%~6$k2	J G(} nz(3A667*X"6q"9GIIKv&ell7==?34)-qj"1%&"C$67v;)+C-CC+d  +s6{+# ( ~<=IIK 6
fqA*+OOK(!"''),1r6QY - +,OOK(!"''),1r6QY -  !IIK!Oj"C
(8(8(:$;<tBIIK()Y6N8NN6		d $ +s4		#456 E	
	G+U++F4<"66F++-KK^<	f{"# =r   __main__rv   )NTFTr  z$I saw John with a dog with my cookier  )1rD   r   r!  r  	functoolsr   r  r   r   r   nltk.internalsr   nltk.parse.apir   	nltk.treer	   	nltk.utilr
   r   rF   ry   r   r  r  r$  r+  r5  r8  r>  rC  rJ  rN  rR  rV  r]  rX  r  r  r  r  rc  r  r  r  r  r  r  r  r;   r6   r   r   <module>r     s)  6  	  $ : : 2 "  ! g g gTD#u D#N>%u >%L\ \H,$ ,$^+K
 +Kf' B) )b$ ' "* ,+61 +6f+ &!4 .#( #*#(A #0#)C #7 N	 N	 N 	 N&(%'GP' GPTH HH+ H KK KHK H_2+ _2N8 
	/y$x zF r   