³ò
4ÒÇIc           @   sà   d  d k  Z  d  d k l Z l Z l Z l Z d  d k Td  d k l Z d e	 f d „  ƒ  YZ
 d e	 f d „  ƒ  YZ d	 e	 f d
 „  ƒ  YZ d e	 f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z e d j o e ƒ  n d S(   iÿÿÿÿN(   t   DependencyProductiont   DependencyGrammart   StatisticalDependencyGrammart   parse_dependency_grammar(   t   *(   t   pformatt   DependencySpanc           B   sV   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   sW  
    A contiguous span over some part of the input string representing 
    dependency (head -> modifier) relationships amongst words.  An atomic 
    span corresponds to only one word so it isn't a 'span' in the conventional
    sense, as its _start_index = _end_index = _head_index for concatenation
    purposes.  All other spans are assumed to have arcs between all nodes
    within the start and end indexes of the span, and one head index corresponding
    to the head word for the entire span.  This is the same as the root node if 
    the dependency structure were depicted as a graph.
    c         C   sR   | |  _  | |  _ | |  _ | |  _ t | | | t | ƒ f ƒ |  _ | |  _ d  S(   N(   t   _start_indext
   _end_indext   _head_indext   _arcst   hasht   tuplet   _hasht   _tags(   t   selft   start_indext	   end_indext
   head_indext   arcst   tags(    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyt   __init__    s    				!c         C   s   |  i  S(   sn   
        @return: An value indexing the head of the entire C{DependencySpan}.
        @rtype: C{int}.
        (   R	   (   R   (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyR   (   s    c         C   s   d |  i  |  i |  i f S(   so   
        @return: A concise string representatino of the C{DependencySpan}.
        @rtype: C{string}.
        s   Span %d-%d; Head Index: %d(   R   R   R	   (   R   (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyt   __repr__/   s    c         C   se   d |  i  |  i |  i f } xB t t |  i ƒ ƒ D]+ } | d | |  i | |  i | f 7} q2 W| S(   so   
        @return: A verbose string representation of the C{DependencySpan}.
        @rtype: C{string}.
        s   Span %d-%d; Head Index: %ds   
%d <- %d, %s(   R   R   R	   t   ranget   lenR
   R   (   R   t   strt   i(    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyt   __str__6   s
     )c         C   s\   t  | |  i ƒ oI |  i | i j o6 |  i | i j o# |  i | i j o |  i | i j S(   sk   
        @return: true if this C{DependencySpan} is equal to C{other}.
        @rtype: C{boolean}.
        (   t
   isinstancet	   __class__R   R   R	   R
   (   R   t   other(    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyt   __eq__@   s
    c         C   s   |  | j S(   sk   
        @return: false if this C{DependencySpan} is equal to C{other}
        @rtype: C{boolean}	
        (    (   R   R   (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyt   __ne__K   s    c         C   sL   t  | |  i ƒ p d Sn t |  i |  i |  i f | i | i | i f ƒ S(   s”   
        @return: -1 if args are of different class.  Otherwise returns the
        cmp() of the two sets of spans.
        @rtype: C{int} 
        iÿÿÿÿ(   R   R   t   cmpR   R	   R   (   R   R   (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyt   __cmp__R   s    c         C   s   |  i  S(   sD   
        @return: The hash value of this C{DependencySpan}.
        (   R   (   R   (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyt   __hash__[   s    (   t   __name__t
   __module__t   __doc__R   R   R   R   R   R    R"   R#   (    (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyR      s   
				
				t	   ChartCellc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   så   
    A cell from the parse chart formed when performing the CYK algorithm.
    Each cell keeps track of its x and y coordinates (though this will probably
    be discarded), and a list of spans serving as the cell's entries.
    c         C   s%   | |  _  | |  _ t g  ƒ |  _ d S(   s“   
        @param x: This cell's x coordinate.
        @type x: C{int}.
        @param y: This cell's y coordinate.
        @type y: C{int}.
        N(   t   _xt   _yt   sett   _entries(   R   t   xt   y(    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyR   k   s    		c         C   s   |  i  i | ƒ d S(   sÂ   
        Appends the given span to the list of spans
        representing the chart cell's entries.
        
        @param span: The span to add.
        @type span: C{DependencySpan}.
        N(   R+   t   add(   R   t   span(    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyR.   v   s    c         C   s   d |  i  |  i |  i f S(   sk   
        @return: A verbose string representation of this C{ChartCell}.
        @rtype: C{string}.
        s   CC[%d,%d]: %s(   R(   R)   R+   (   R   (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyR   €   s    c         C   s   d |  S(   sk   
        @return: A concise string representation of this C{ChartCell}.
        @rtype: C{string}.
        s   %s(    (   R   (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyR   ‡   s    (   R$   R%   R&   R   R.   R   R   (    (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyR'   e   s
   		
	t   ProjectiveDependencyParserc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s«  
    A projective, rule-based, dependency parser.  A ProjectiveDependencyParser
    is created with a DependencyGrammar, a set of productions specifying 
    word-to-word dependency relations.  The parse() method will then 
    return the set of all parses, in tree representation, for a given input
    sequence of tokens.  Each parse must meet the requirements of the both
    the grammar and the projectivity constraint which specifies that the 
    branches of the dependency tree are not allowed to cross.  Alternatively, 
    this can be understood as stating that each parent node and its children 
    in the parse tree form a continuous substring of the input sequence.
    c         C   s   | |  _  d S(   s  
        Create a new ProjectiveDependencyParser, from a word-to-word
        dependency grammar C{DependencyGrammar}.

        @param dependency_grammar: A word-to-word relation dependencygrammar.
        @type dependency_grammar: A C{DependencyGrammar}.
        N(   t   _grammar(   R   t   dependency_grammar(    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyR   ¡   s    c         C   sn  t  | ƒ |  _ g  } x¼ t d t |  i ƒ d ƒ D]ž } | i g  ƒ xˆ t d t |  i ƒ d ƒ D]j } | | i t | | ƒ ƒ | | d j o9 | | | i t | d | | d d g d g ƒ ƒ qb qb Wq2 WxÐ t d t |  i ƒ d ƒ D]² } x© t | d d d ƒ D]‘ } xˆ t | d | d ƒ D]p } xg | | | i D]T } xK | | | i D]8 } x/ |  i	 | | ƒ D] } | | | i | ƒ qtWq[WqCWq+WqWqñ Wg  }	 g  }
 x´ | t |  i ƒ d i D]˜ } d } x] t t | ƒ ƒ D]I } | d | d | | | | d d d | i
 | d d d d f
 7} qíWt | ƒ } |	 i | ƒ |
 i | i ƒ  ƒ qÎW|
 S(	   sU  
        Performs a projective dependency parse on the list of tokens using
        a chart-based, span-concatenation algorithm similar to Eisner (1996).
        
        @param tokens: The list of input tokens.
        @type tokens:a C{list} of L{String}
        @return: A list of parse trees.
        @rtype: a C{list} of L{tree}
        i    i   iÿÿÿÿt   nulli   t    s   	%d	%s	%s	%s	%s	%s	%d	%s	%s	%s
t   -(   t   listt   _tokensR   R   t   appendR'   R.   R   R+   t   concatenateR
   t   DependencyGrapht   tree(   R   t   tokenst   chartR   t   jt   kt   span1t   span2t   newspant   graphst   treest   parset   conll_formatt   dg(    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyRE   «   sF    
  A      -  Gc         C   sY  g  } | i  | i  j o	 d GHn | i  | i  j o | } | } | } n | i | i } | i | i } |  i i |  i | i |  i | i ƒ oC | i | | i | i  <| i t | i  | i	 | i | | ƒ ƒ n | i | i } |  i i |  i | i |  i | i ƒ oC | i | | i | i  <| i t | i  | i	 | i | | ƒ ƒ n | S(   s  
        Concatenates the two spans in whichever way possible.  This
        includes rightward concatenation (from the leftmost word of the 
        leftmost span to the rightmost word of the rightmost span) and
        leftward concatenation (vice-versa) between adjacent spans.  Unlike 
        Eisner's presentation of span concatenation, these spans do not 
        share or pivot on a particular word/word-index.

        return: A list of new spans formed through concatenation.
        rtype: A C{list} of L{DependencySpan}
        s8   Error: Mismatched spans - replace this with thrown error(
   R   R
   R   R1   t   containsR7   R	   R8   R   R   (   R   R@   RA   t   spanst	   temp_spant   new_arcst   new_tags(    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyR9   Ó   s"    	
*,*,(   R$   R%   R&   R   RE   R9   (    (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyR0   ”   s   	
	(t'   ProbabilisticProjectiveDependencyParserc           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s  
    A probabilistic, projective dependency parser.  This parser returns 
    the most probable projective parse derived from the probabilistic 
    dependency grammar derived from the train() method.  The probabilistic 
    model is an implementation of Eisner's (1996) Model C, which conditions 
    on head-word, head-tag, child-word, and child-tag.  The decoding 
    uses a bottom-up chart-based span concatenation algorithm that's 
    identical to the one utilized by the rule-based projective parser.
    c         C   s	   d GHd S(   sq   
        Create a new probabilistic dependency parser.  No additional 
        operations are necessary.
        R4   N(    (   R   (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyR     s    c         C   s)  t  | ƒ |  _ g  } xt d t |  i ƒ d ƒ D]ø } | i g  ƒ xâ t d t |  i ƒ d ƒ D]Ä } | | i t | | ƒ ƒ | | d j o“ |  i i i | | d ƒ o\ xn |  i i | | d D]; } | | | i	 t
 | d | | d d g | g ƒ ƒ qÊ Wq&d | | d GHg  Sqb qb Wq2 WxÐ t d t |  i ƒ d ƒ D]² } x© t | d d d ƒ D]‘ } xˆ t | d | d ƒ D]p } xg | | | i D]T } xK | | | i D]8 } x/ |  i | | ƒ D] }	 | | | i	 |	 ƒ qÎWqµWqWq…WqhWqKWg  }
 g  } t } d } x| t |  i ƒ d i D]ç } d } d } x” t t | ƒ ƒ D]€ } | d | | d | i | d d f 7} | d	 | d | | | | | i | | i | d | i | d d d
 d
 f
 7} qYWt | ƒ } |  i | ƒ } | | j o | i ƒ  } | } q4q4W| | g S(   sZ  
        Parses the list of tokens subject to the projectivity constraint
        and the productions in the parser's grammar.  This uses a method 
        similar to the span-concatenation algorithm defined in Eisner (1996).
        It returns the most probable parse derived from the parser's 
        probabilistic dependency grammar.
        i    i   iÿÿÿÿs7   No tag found for input token '%s', parse is impossible.i   R4   s   %s	%s	%d	%s
R3   s   	%d	%s	%s	%s	%s	%s	%d	%s	%s	%s
R5   (   R6   R7   R   R   R8   R'   R1   R   t   has_keyR.   R   R+   R9   t   NoneR
   R:   t   compute_probR;   (   R   R<   R=   R   R>   t   tagR?   R@   RA   RB   RC   RD   t	   max_parset	   max_scoreRE   RF   t   malt_formatRG   t   score(    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyRE     s\       =      -  )Uc         C   si  g  } | i  | i  j o	 d GHn | i  | i  j o | } | } | } n | i | i } | i | i } |  i i |  i | i |  i | i ƒ oC | i | | i | i  <| i t | i  | i	 | i | | ƒ ƒ n | i | i } | i | i } |  i i |  i | i |  i | i ƒ oC | i | | i | i  <| i t | i  | i	 | i | | ƒ ƒ n | S(   s  
        Concatenates the two spans in whichever way possible.  This
        includes rightward concatenation (from the leftmost word of the 
        leftmost span to the rightmost word of the rightmost span) and
        leftward concatenation (vice-versa) between adjacent spans.  Unlike 
        Eisner's presentation of span concatenation, these spans do not 
        share or pivot on a particular word/word-index.

        return: A list of new spans formed through concatenation.
        rtype: A C{list} of L{DependencySpan}
        s8   Error: Mismatched spans - replace this with thrown error(
   R   R
   R   R1   RH   R7   R	   R8   R   R   (   R   R@   RA   RI   RJ   RK   RL   (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyR9   ;  s$    	
*,*,c      	   C   sÊ  g  } h  } h  } xœ| D]”} x‹t  d t | i ƒ ƒ D]q} | i | d } | i | ƒ } | i | ƒ }	 | |	 }
 x/t  d | d |	 d ƒ D]} | i | d } | i | d } | i | ƒ o | | i | ƒ n t | g ƒ | | <d } d } d } d } | d j  o>| | } | d j o. | i | | d } | i | | d } n | d	 j o6 | i | | d d } | i | | d d } n | d j o | i t	 | | g ƒ ƒ n d
 | | | | | f } d | | | f } | i | ƒ o | | c d 7<n d | | <| i | ƒ o | | c d 7<q¥d | | <q“ | d j oB| | d } | |
 j  o. | i | | d } | i | | d } n | d j o6 | i | | d d } | i | | d d } n | d j o | i t	 | | g ƒ ƒ n d | | | | | f } d | | | f } | i | ƒ o | | c d 7<n d | | <| i | ƒ o | | c d 7<q¥d | | <q“ q“ Wq8 Wq Wt
 | | | ƒ |  _ d S(   sˆ  
        Trains a StatisticalDependencyGrammar based on the list of input 
        DependencyGraphs.  This model is an implementation of Eisner's (1996)
        Model C, which derives its statistics from head-word, head-tag, 
        child-word, and child-tag relationships.

        param graphs: A list of dependency graphs to train from.
        type: A list of C{DependencyGraph}
        i   t   depsi    i   t   wordRQ   t   STOPt   STARTiÿÿÿÿs'   (head (%s %s) (mods (%s, %s, %s) left))s   (mods (%s, %s, %s) left))s(   (head (%s %s) (mods (%s, %s, %s) right))s   (mods (%s, %s, %s) right))N(   R   R   t   nodelistt   left_childrent   right_childrenRN   R.   R*   R8   R    R   R1   (   R   RC   t   productionst   eventsR   RG   t
   node_indext   childrent   nr_left_childrent   nr_right_childrent   nr_childrent   child_indext	   head_wordt   head_tagt   childt	   child_tagt	   prev_wordt   prev_tagt   array_indext
   head_eventt	   mod_event(    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyt   train\  sv    
  
 


c         C   s°  d } x£t  d t | i ƒ ƒ D]‰} | i | d } | i | ƒ } | i | ƒ } | | } xGt  d | d | d ƒ D]*} | i | d }	 | i | d }
 d } d } d	 } d	 } | d j  oæ | | } | d j o. | i | | d } | i | | d } n | d
 j o6 | i | | d d } | i | | d d } n d | | | |	 |
 f } d | |	 |
 f } |  i i | } |  i i | } | | | 9} qz | d j oê | | d } | | j  o. | i | | d } | i | | d } n | d j o6 | i | | d d } | i | | d d } n d | | | |	 |
 f } d | |	 |
 f } |  i i | } |  i i | } | | | 9} qz qz Wq W| S(   sb  
        Computes the probability of a dependency graph based 
        on the parser's probability model (defined by the parser's
        statistical dependency grammar).

        param dg: A dependency graph to score.
        type dg: a C{DependencyGraph}
        return: The probability of the dependency graph.
        rtype: A number/double.
        g      ð?i   RV   i    i   RW   RQ   RX   RY   iÿÿÿÿs'   (head (%s %s) (mods (%s, %s, %s) left))s   (mods (%s, %s, %s) left))s(   (head (%s %s) (mods (%s, %s, %s) right))s   (mods (%s, %s, %s) right))(   R   R   RZ   R[   R\   R1   t   _events(   R   RG   t   probR_   R`   Ra   Rb   Rc   Rd   Re   Rf   Rg   Rh   Ri   Rj   Rk   Rl   Rm   t   h_countt   m_count(    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyRP   ¥  sT     
 
(   R$   R%   R&   R   RE   R9   Rn   RP   (    (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyRM   û   s   			.	!	Ic           C   s   t  ƒ  t ƒ  d  S(   N(   t   projective_rule_parse_demot   projective_prob_parse_demo(    (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyt   demoÞ  s    c          C   sU   t  d ƒ }  |  GHt |  ƒ } | i d d d d d g ƒ } x | D] } | GHqB Wd S(   s„   
    A demonstration showing the creation and use of a 
    C{DependencyGrammar} to perform a projective dependency 
    parse.
    sP   
    'scratch' -> 'cats' | 'walls'
    'walls' -> 'the'
    'cats' -> 'the'
    t   thet   catst   scratcht   wallsN(   R   R0   RE   (   t   grammart   pdpRD   R;   (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyRs   ä  s    	 c       	   C   sâ   Hd GHd GHd GHt  d ƒ }  |  GHHd GHd GHt |  ƒ } | i d d d	 d d
 d g ƒ } x | D] } | GHq` WHd GHd GHd GHd GHt  d ƒ }  |  GHHd GHt |  ƒ } | i d d d	 d d
 d g ƒ } x | D] } | GHqÏ Wd S(   s   
    A demonstration showing the creation of a C{DependencyGrammar} 
    in which a specific number of modifiers is listed for a given 
    head.  This can further constrain the number of possible parses
    created by a C{ProjectiveDependencyParser}.
    s>   A grammar with no arity constraints. Each DependencyProductions;   specifies a relationship between one head word and only ones   modifier word.:sj   
    'fell' -> 'price' | 'stock'
    'price' -> 'of' | 'the'
    'of' -> 'stock'
    'stock' -> 'the'
    s<   For the sentence 'The price of the stock fell', this grammars(   will produce the following three parses:Rv   t   pricet   oft   stockt   fells0   Comparatively, the following grammar contains a s5   C{DependencyProduction} that specifies a relationships4   between the one head word, 'price', and two modifiers   words, 'of' and 'the'.sh   
    'fell' -> 'price' | 'stock'
    'price' -> 'of' 'the'
    'of' -> 'stock'
    'stock' -> 'the'
    s:   This constrains the number of possible parses to just one:N(   R   R0   RE   (   Rz   R{   RD   R;   (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyt   arity_parse_demoõ  s<    	! 		! c          C   s¥   g  }  t  i d ƒ D] } | o |  t | ƒ q q ~  } t ƒ  } d GH| i | ƒ d d d d d d g } d	 Gd
 i | ƒ Gd GH| i | ƒ } d GH| d GHd S(   sU   
    A demo showing the training and use of a projective 
    dependency parser.
    s   

s6   Training Probabilistic Projective Dependency Parser...t   Cathyt   zagt   hent   wildt   zwaaient   .s	   Parsing 't    s   '...s   Parse:i    N(   t   conll_data2t   splitR:   RM   Rn   t   joinRE   (   t   _[1]t   entryRC   t   ppdpt   sentRE   (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pyRt   $  s    4	t   __main__(   t   matht   nltkR    R   R   R   t   dependencygrapht   pprintR   t   objectR   R'   R0   RM   Ru   Rs   R€   Rt   R$   (    (    (    s;   /p/zhu/06/nlp/nltk/nltk/parse/projectivedependencyparser.pys   <module>
   s   "
P/gã			/	