ó
ù`]c           @  sñ  d  Z  d d l m Z m Z d d l m Z d d l m Z m Z m	 Z	 m
 Z
 m Z m Z m Z m Z m Z m Z m Z m Z m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z d e f d „  ƒ  YZ d e e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ  d e f d „  ƒ  YZ! d e f d „  ƒ  YZ" d e f d „  ƒ  YZ# d e f d „  ƒ  YZ$ d e  f d „  ƒ  YZ% d e! f d „  ƒ  YZ& d e f d „  ƒ  YZ' e ƒ  e ƒ  e  ƒ  e! ƒ  e" ƒ  g Z( e ƒ  e ƒ  e ƒ  e ƒ  g Z) e ƒ  e ƒ  e ƒ  e ƒ  g Z* e ƒ  e ƒ  e ƒ  e ƒ  g Z+ e ƒ  e ƒ  e# ƒ  g Z, d e f d „  ƒ  YZ- d e- f d „  ƒ  YZ. d  e- f d! „  ƒ  YZ/ d" e- f d# „  ƒ  YZ0 d$ e- f d% „  ƒ  YZ1 d& e- f d' „  ƒ  YZ2 e ƒ  e ƒ  e% ƒ  e& ƒ  e' ƒ  g Z3 e ƒ  e ƒ  e ƒ  e$ ƒ  g Z4 e ƒ  e ƒ  e ƒ  e$ ƒ  g Z5 e ƒ  e ƒ  e ƒ  e$ ƒ  g Z6 d( e- e f d) „  ƒ  YZ7 d* e7 f d+ „  ƒ  YZ8 d, e7 f d- „  ƒ  YZ9 d. e7 f d/ „  ƒ  YZ: d0 e7 f d1 „  ƒ  YZ; e< e= e< d2 d3 d4 d5 „ Z> e? d6 k ríe> ƒ  n  d7 S(8   sÕ  
Data classes and parser implementations for *incremental* 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.

A parser is "incremental", if it guarantees that for all i, j where i < j,
all edges ending at i are built before any edges ending at j.
This is appealing for, say, speech recognizer hypothesis filtering.

The main parser class is ``EarleyChartParser``, which is a top-down
algorithm, originally formulated by Jay Earley (1970).
iÿÿÿÿ(   t   print_functiont   division(   t   range(   t   Chartt   ChartParsert   EdgeIt   LeafEdget   LeafInitRulet   BottomUpPredictRulet   BottomUpPredictCombineRulet   TopDownInitRulet   SingleEdgeFundamentalRulet   EmptyPredictRulet   CachedTopDownPredictRulet!   FilteredSingleEdgeFundamentalRulet"   FilteredBottomUpPredictCombineRule(   t   FeatureChartt   FeatureChartParsert   FeatureTopDownInitRulet   FeatureTopDownPredictRulet   FeatureEmptyPredictRulet   FeatureBottomUpPredictRulet!   FeatureBottomUpPredictCombineRulet    FeatureSingleEdgeFundamentalRulet   IncrementalChartc           B  sP   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   c         C  s5   t  d „  |  j ƒ  Dƒ ƒ |  _ i  |  _ i  |  _ d  S(   Nc         s  s   |  ] } g  Vq d  S(   N(    (   t   .0t   x(    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pys	   <genexpr>B   s    (   t   tuplet
   _positionst
   _edgelistst   _edge_to_cplst   _indexes(   t   self(    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyt
   initialize@   s    	c         C  s   t  |  j ƒ  ƒ S(   N(   t   listt	   iteredges(   R    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyt   edgesK   s    c         C  s   d „  |  j  Dƒ S(   Nc         s  s"   |  ] } | D] } | Vq q d  S(   N(    (   R   t   edgelistt   edge(    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pys	   <genexpr>O   s    (   R   (   R    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR#   N   s    c           s   |  j  | } ˆ  i  k r# t | ƒ St ˆ  j ƒ  ƒ } t | ƒ } | |  j k r` |  j | ƒ n  t ‡  f d †  | Dƒ ƒ } t |  j | | j | g  ƒ ƒ S(   Nc         3  s   |  ] } ˆ  | Vq d  S(   N(    (   R   t   key(   t   restrictions(    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pys	   <genexpr>`   s    (   R   t   itert   sortedt   keysR   R   t
   _add_indext   get(   R    t   endR(   R%   t
   restr_keyst   vals(    (   R(   s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyt   selectQ   s    
c           sÎ   x0 | D]( } t  t | ƒ s t d | ƒ ‚ q q Wt d „  |  j ƒ  Dƒ ƒ } |  j | <xm t |  j ƒ D]\ \ } } | | } xC | D]; ‰  t ‡  f d †  | Dƒ ƒ } | j | g  ƒ j	 ˆ  ƒ q‡ Wqj Wd  S(   Ns   Bad restriction: %sc         s  s   |  ] } i  Vq d  S(   N(    (   R   R   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pys	   <genexpr>j   s    c         3  s!   |  ] } t  ˆ  | ƒ ƒ  Vq d  S(   N(   t   getattr(   R   R'   (   R&   (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pys	   <genexpr>p   s    (
   t   hasattrR   t
   ValueErrorR   R   R   t	   enumerateR   t
   setdefaultt   append(   R    R/   R'   t   indexR.   R%   t
   this_indexR0   (    (   R&   s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR,   c   s    '
c           si   ˆ  j  ƒ  } xV |  j j ƒ  D]E \ } } t ‡  f d †  | Dƒ ƒ } | | j | g  ƒ j ˆ  ƒ q Wd  S(   Nc         3  s!   |  ] } t  ˆ  | ƒ ƒ  Vq d  S(   N(   R2   (   R   R'   (   R&   (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pys	   <genexpr>v   s    (   R.   R   t   itemsR   R6   R7   (   R    R&   R.   R/   R8   R0   (    (   R&   s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyt   _register_with_indexess   s    c         C  s   |  j  | j ƒ  j | ƒ d  S(   N(   R   R.   R7   (   R    R&   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyt   _append_edgey   s    c         C  s   t  |  j ƒ  d ƒ S(   Ni   (   R   t
   num_leaves(   R    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR   |   s    (
   t   __name__t
   __module__R!   R$   R#   R1   R,   R;   R<   R   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR   ?   s   							t   FeatureIncrementalChartc           B  s#   e  Z d  „  Z d „  Z d „  Z RS(   c           s    ˆ j  | } ˆ  i  k r# t | ƒ St ˆ  j ƒ  ƒ } t | ƒ } | ˆ j k r` ˆ j | ƒ n  t ‡  ‡ f d †  | Dƒ ƒ } t ˆ j | | j | g  ƒ ƒ S(   Nc         3  s"   |  ] } ˆ j  ˆ  | ƒ Vq d  S(   N(   t   _get_type_if_possible(   R   R'   (   R(   R    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pys	   <genexpr>‘   s    (   R   R)   R*   R+   R   R   R,   R-   (   R    R.   R(   R%   R/   R0   (    (   R(   R    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR1      s    
c           sÑ   x0 | D]( } t  t | ƒ s t d | ƒ ‚ q q Wt d „  ˆ j ƒ  Dƒ ƒ } ˆ j | <xp t ˆ j ƒ D]_ \ } } | | } xF | D]> ‰  t ‡  ‡ f d †  | Dƒ ƒ } | j | g  ƒ j	 ˆ  ƒ q‡ Wqj Wd  S(   Ns   Bad restriction: %sc         s  s   |  ] } i  Vq d  S(   N(    (   R   R   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pys	   <genexpr>œ   s    c         3  s*   |  ]  } ˆ j  t ˆ  | ƒ ƒ  ƒ Vq d  S(   N(   RA   R2   (   R   R'   (   R&   R    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pys	   <genexpr>£   s   (
   R3   R   R4   R   R   R   R5   R   R6   R7   (   R    R/   R'   R8   R.   R%   R9   R0   (    (   R&   R    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR,   •   s    '
c           sl   ˆ  j  ƒ  } xY ˆ j j ƒ  D]H \ } } t ‡  ‡ f d †  | Dƒ ƒ } | | j | g  ƒ j ˆ  ƒ q Wd  S(   Nc         3  s*   |  ]  } ˆ j  t ˆ  | ƒ ƒ  ƒ Vq d  S(   N(   RA   R2   (   R   R'   (   R&   R    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pys	   <genexpr>¬   s    (   R.   R   R:   R   R6   R7   (   R    R&   R.   R/   R8   R0   (    (   R&   R    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR;   ¨   s
    (   R>   R?   R1   R,   R;   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR@   €   s   		t   CompleteFundamentalRulec           B  s   e  Z d  „  Z RS(   c      	   c  sw   | j  ƒ  } xd | j d | d | d t d | j ƒ  ƒ D]8 } | j | j  ƒ  ƒ } | j | | | ƒ r7 | Vq7 q7 Wd  S(   Nt   startR.   t   is_completet   lhs(   R.   R1   t   Truet   nextsymt   move_dot_forwardt   insert_with_backpointer(   R    t   chartt   grammart	   left_edgeR.   t
   right_edget   new_edge(    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyt   _apply_incomplete·   s    %(   R>   R?   RO   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRB   ¶   s   t   CompleterRulec           B  s   e  Z e ƒ  Z d  „  Z RS(   c         c  s>   t  | t ƒ s: x( |  j j | | | ƒ D] } | Vq( Wn  d  S(   N(   t
   isinstanceR   t   _fundamental_rulet   apply(   R    RJ   RK   R&   RN   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRS   Æ   s    (   R>   R?   RB   RR   RS   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRP   Ã   s   	t   ScannerRulec           B  s   e  Z e ƒ  Z d  „  Z RS(   c         c  s>   t  | t ƒ r: x( |  j j | | | ƒ D] } | Vq( Wn  d  S(   N(   RQ   R   RR   RS   (   R    RJ   RK   R&   RN   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRS   Ï   s    (   R>   R?   RB   RR   RS   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRT   Ì   s   	t   PredictorRulec           B  s   e  Z RS(    (   R>   R?   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRU   Õ   s   t   FilteredCompleteFundamentalRulec           B  s   e  Z d  „  Z RS(   c         c  s8   | j  ƒ  r4 x% |  j | | | ƒ D] } | Vq" Wn  d  S(   N(   RD   t   _apply_complete(   R    RJ   RK   R&   RN   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRS   Ú   s    (   R>   R?   RS   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRV   Ù   s   t   FeatureCompleteFundamentalRulec           B  s   e  Z d  „  Z RS(   c      	   c  sv   |  j  } | j ƒ  } xZ | j d | d | d t d | j ƒ  ƒ D]. } x% | j | | | | ƒ D] } | Vq_ Wq@ Wd  S(   NRC   R.   RD   RE   (   RR   R.   R1   RF   RG   RS   (   R    RJ   RK   RL   t   frR.   RM   RN   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRO   è   s    	%(   R>   R?   RO   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRX   ç   s   t   FeatureCompleterRulec           B  s   e  Z e ƒ  Z RS(    (   R>   R?   RX   RR   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRZ   ô   s   t   FeatureScannerRulec           B  s   e  Z e ƒ  Z RS(    (   R>   R?   RX   RR   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR[   ø   s   t   FeaturePredictorRulec           B  s   e  Z RS(    (   R>   R?   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR\   ü   s   t   IncrementalChartParserc           B  s/   e  Z d  Z e d d e d „ Z d d „ Z RS(   s  
    An *incremental* chart parser implementing Jay Earley's
    parsing algorithm:

    | For each index end in [0, 1, ..., N]:
    |   For each edge such that edge.end = end:
    |     If edge is incomplete and edge.next is not a part of speech:
    |       Apply PredictorRule to edge
    |     If edge is incomplete and edge.next is a part of speech:
    |       Apply ScannerRule to edge
    |     If edge is complete:
    |       Apply CompleterRule to edge
    | Return any complete parses in the chart
    i    i2   c         C  s›   | |  _  | |  _ | |  _ | |  _ g  |  _ g  |  _ x^ | D]V } | j d k re |  j j | ƒ q= | j d k r‡ |  j j | ƒ q= t d ƒ ‚ q= Wd S(   sö  
        Create a new Earley chart parser, that uses ``grammar`` to
        parse texts.

        :type grammar: CFG
        :param grammar: The grammar used to parse texts.
        :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.
        :param chart_class: The class that should be used to create
            the charts used by this parser.
        i    i   s9   Incremental inference rules must have NUM_EDGES == 0 or 1N(	   t   _grammart   _tracet   _trace_chart_widtht   _chart_classt   _axiomst   _inference_rulest	   NUM_EDGESR7   R4   (   R    RK   t   strategyt   tracet   trace_chart_widtht   chart_classt   rule(    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyt   __init__5  s    						c         C  s½  | d  k r |  j } n  |  j } t | ƒ } |  j j | ƒ |  j | ƒ } |  j } |  j | j ƒ  d } | rˆ t	 | j
 | ƒ ƒ n  x? |  j D]4 } t | j | | ƒ ƒ } | | | | | | ƒ q’ W|  j }	 xã t | j ƒ  d ƒ D]Ë }
 | d k rt	 d |
 d ƒ n  t | j d |
 ƒ ƒ } x‹ | r´| j ƒ  } xr |	 D]j } t | j | | | ƒ ƒ } | | | | | | ƒ x0 | D]( } | j ƒ  |
 k r| j | ƒ qqWqCWq*Wqê W| S(   Ni   s   
* Processing queue:s   
R.   (   t   NoneR_   t   _trace_new_edgesR"   R^   t   check_coverageRa   R`   R=   t   printt   pretty_format_leavesRb   RS   Rc   R   R1   t   popR.   R7   (   R    t   tokensRf   t   trace_new_edgesRJ   RK   t   trace_edge_widtht   axiomt	   new_edgest   inference_rulesR.   t   agendaR&   Ri   RN   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyt   chart_parse`  s6    				 N(   R>   R?   t   __doc__t   BU_LC_INCREMENTAL_STRATEGYR   Rj   Rk   Rx   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR]   %  s   %t   EarleyChartParserc           B  s   e  Z d  „  Z RS(   c         K  s   t  j |  | t |  d  S(   N(   R]   Rj   t   EARLEY_STRATEGY(   R    RK   t   parser_args(    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRj   …  s    (   R>   R?   Rj   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR{   „  s   t   IncrementalTopDownChartParserc           B  s   e  Z d  „  Z RS(   c         K  s   t  j |  | t |  d  S(   N(   R]   Rj   t   TD_INCREMENTAL_STRATEGY(   R    RK   R}   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRj   Š  s    (   R>   R?   Rj   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR~   ‰  s   t   IncrementalBottomUpChartParserc           B  s   e  Z d  „  Z RS(   c         K  s   t  j |  | t |  d  S(   N(   R]   Rj   t   BU_INCREMENTAL_STRATEGY(   R    RK   R}   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRj   ‘  s    (   R>   R?   Rj   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR€     s   t(   IncrementalBottomUpLeftCornerChartParserc           B  s   e  Z d  „  Z RS(   c         K  s   t  j |  | t |  d  S(   N(   R]   Rj   Rz   (   R    RK   R}   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRj   ˜  s    (   R>   R?   Rj   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR‚   —  s   t    IncrementalLeftCornerChartParserc           B  s   e  Z d  „  Z RS(   c         K  s5   | j  ƒ  s t d ƒ ‚ n  t j |  | t |  d  S(   NsN   IncrementalLeftCornerParser only works for grammars without empty productions.(   t   is_nonemptyR4   R]   Rj   t   LC_INCREMENTAL_STRATEGY(   R    RK   R}   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRj   Ÿ  s
    (   R>   R?   Rj   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRƒ   ž  s   t   FeatureIncrementalChartParserc           B  s   e  Z e d  e d „ Z RS(   i   c      
   K  s)   t  j |  | d | d | d | | d  S(   NRe   Rg   Rh   (   R]   Rj   (   R    RK   Re   Rg   Rh   R}   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRj   Ê  s    (   R>   R?   t"   BU_LC_INCREMENTAL_FEATURE_STRATEGYR@   Rj   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyR†   É  s   t   FeatureEarleyChartParserc           B  s   e  Z d  „  Z RS(   c         K  s   t  j |  | t |  d  S(   N(   R†   Rj   t   EARLEY_FEATURE_STRATEGY(   R    RK   R}   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRj   Ý  s    (   R>   R?   Rj   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRˆ   Ü  s   t$   FeatureIncrementalTopDownChartParserc           B  s   e  Z d  „  Z RS(   c         K  s   t  j |  | t |  d  S(   N(   R†   Rj   t   TD_INCREMENTAL_FEATURE_STRATEGY(   R    RK   R}   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRj   ä  s    (   R>   R?   Rj   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRŠ   ã  s   t%   FeatureIncrementalBottomUpChartParserc           B  s   e  Z d  „  Z RS(   c         K  s   t  j |  | t |  d  S(   N(   R†   Rj   t   BU_INCREMENTAL_FEATURE_STRATEGY(   R    RK   R}   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRj   ë  s    (   R>   R?   Rj   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRŒ   ê  s   t/   FeatureIncrementalBottomUpLeftCornerChartParserc           B  s   e  Z d  „  Z RS(   c         K  s   t  j |  | t |  d  S(   N(   R†   Rj   R‡   (   R    RK   R}   (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRj   ò  s    (   R>   R?   Rj   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyRŽ   ñ  s   i   s$   I saw John with a dog with my cookiei   c         C  sO  d d l  } d d l } d d l m } | ƒ  }	 | rN t d ƒ t |	 ƒ n  t d ƒ t | ƒ | j ƒ  }
 t |
 ƒ t ƒ  t |	 d | ƒ} | j ƒ  } | j |
 ƒ } t	 | j
 |	 j ƒ  ƒ ƒ } | j ƒ  | } | rþ t | ƒ | k sþ t d ƒ ‚ n  | r"x. | D] } t | ƒ qWn t d t | ƒ ƒ |  rKt d	 | ƒ n  d S(
   s0   
    A demonstration of the Earley parsers.
    iÿÿÿÿN(   t   demo_grammars	   * Grammars   * Sentence:Rf   s   Not all parses founds	   Nr trees:s   Time:(   t   syst   timet   nltk.parse.chartR   Rn   t   splitR{   t   clockRx   R"   t   parsesRC   t   lent   AssertionError(   t   print_timest   print_grammart   print_treesRf   t   sentt	   numparsesR   R‘   R   RK   Rq   t   earleyt   tRJ   R•   t   tree(    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyt   demoý  s0    	



!t   __main__N(@   Ry   t
   __future__R    R   t	   six.movesR   R’   R   R   R   R   R   R   R	   R
   R   R   R   R   R   t   nltk.parse.featurechartR   R   R   R   R   R   R   R   R   R@   RB   RP   RT   RU   RV   RX   RZ   R[   R\   R|   R   R   Rz   R…   R]   R{   R~   R€   R‚   Rƒ   R‰   R‹   R   R‡   R†   Rˆ   RŠ   RŒ   RŽ   RF   t   FalseR    R>   (    (    (    s5   lib/python2.7/site-packages/nltk/parse/earleychart.pyt   <module>   sŽ   X:A6			_(