³ò
ŒB_Kc        	   @   s­  d  Z  d d k Td d k l Z l Z l Z d d k l Z l Z d d k	 Z	 d d k
 Td e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e e ƒ e e ƒ g Z e e ƒ e e ƒ e e ƒ g Z e e ƒ e e ƒ g Z e ƒ  e ƒ  g Z e e e e Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d „  Z  e	 i! d ƒ Z" d „  Z# e$ d j o e# ƒ  n d S(   s>  
The lexicon is constructed by calling
  lexicon.parseLexicon(<lexicon string>).

In order to construct a parser, you also need a rule set.
The standard English rules are provided in chart as
  chart.DefaultRuleSet

The parser can then be constructed by calling, for example:
  parser = chart.CCGChartParser(<lexicon>, <ruleset>)

Parsing is then performed by running
  parser.nbest_parse(<sentence>.split())

While this returns a list of trees, the default representation
of the produced trees is not very enlightening, particularly
given that it uses the same tree class as the CFG parsers.
It is probably better to call:
  chart.printCCGDerivation(<parse tree extracted from list>)
which should print a nice representation of the derivation.

This entire process is shown far more clearly in the demonstration:
python chart.py
iÿÿÿÿ(   t   *(   t   AbstractChartRulet   EdgeIt   Chart(   t   Treet   defaultdictNt   CCGEdgec           B   s   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   _spant   _categt   _rule(   t   selft   spant   categt   rule(    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   __init__,   s    		c         C   s   |  i  S(   N(   R   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   lhs2   s    c         C   s   |  i  S(   N(   R   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   3   s    c         C   s   |  i  d S(   Ni    (   R   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   start4   s    c         C   s   |  i  d S(   Ni   (   R   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   end5   s    c         C   s   |  i  d |  i d S(   Ni   i    (   R   R   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   length6   s    c         C   s   d S(   N(    (    (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   rhs7   s    c         C   s   d S(   Ni    (    (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   dot8   s    c         C   s   t  S(   N(   t   True(   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   is_complete9   s    c         C   s   t  S(   N(   t   False(   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   is_incomplete:   s    c         C   s   d  S(   N(   t   None(   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   next;   s    c         C   s   |  i  S(   N(   R   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   =   s    c         C   s   |  i  S(   N(   R	   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   ?   s    c         C   sR   t  | t ƒ p d Sn t |  i |  i |  i f | i ƒ  | i ƒ  | i ƒ  f ƒ S(   Niÿÿÿÿ(	   t
   isinstanceR   t   cmpR   R   R	   R   R   R   (   R
   t   other(    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   __cmp__B   s    c         C   s   t  |  i |  i |  i f ƒ S(   N(   t   hashR   R   R	   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   __hash__G   s    (   t   __name__t
   __module__R   R   R   R   R   R   R   R   R   R   R   R   R   R   R    (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   +   s   														t   CCGLeafEdgec           B   s•   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s<   
    Class representing leaf edges in a CCG derivation.
    c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   _posR   t   _leaf(   R
   t   posR   t   leaf(    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   N   s    		c         C   s   |  i  S(   N(   R   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   T   s    c         C   s   |  i  |  i  d f S(   Ni   (   R$   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   U   s    c         C   s   |  i  S(   N(   R$   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   V   s    c         C   s   |  i  d S(   Ni   (   R$   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   W   s    c         C   s   d S(   Ni   (    (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   X   s    c         C   s   |  i  S(   N(   R%   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   Y   s    c         C   s   d S(   Ni    (    (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   Z   s    c         C   s   t  S(   N(   R   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   [   s    c         C   s   t  S(   N(   R   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   \   s    c         C   s   d  S(   N(   R   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   ]   s    c         C   s   |  i  S(   N(   R   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   _   s    c         C   s   |  i  S(   N(   R%   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR'   b   s    c         C   sO   t  | t ƒ p d Sn t |  i |  i |  i f | i ƒ  | i ƒ  | i ƒ  ƒ S(   Niÿÿÿÿ(	   R   R#   R   R   R   R	   R   R   R   (   R
   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   d   s    c         C   s   t  |  i |  i |  i f ƒ S(   N(   R   R$   R   R%   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR    i   s    (   R!   R"   t   __doc__R   R   R   R   R   R   R   R   R   R   R   R   R'   R   R    (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR#   J   s    														t   BinaryCombinatorRulec           B   s/   e  Z d  Z d Z d „  Z d „  Z d „  Z RS(   sw   
    Class implementing application of a binary combinator to a chart.
    Takes the directed combinator to apply.
    i   c         C   s   | |  _  d  S(   N(   t   _combinator(   R
   t
   combinator(    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   r   s    c         c   sÉ   | i  ƒ  | i ƒ  j p d  Sn |  i i | i ƒ  | i ƒ  ƒ o‚ x |  i i | i ƒ  | i ƒ  ƒ D]X } t d | i ƒ  | i  ƒ  f d | d |  i ƒ } | i | | | f ƒ o	 | Vqe qe Wn d  S(   NR   R   R   (   R   R   R*   t   can_combineR   t   combineR   t   insert(   R
   t   chartt   grammart	   left_edget
   right_edget   rest   new_edge(    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt
   apply_iterv   s    "" 0c         C   s   t  |  i ƒ S(   N(   t   strR*   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   __str__„   s    (   R!   R"   R(   t   NUMEDGESR   R5   R7   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR)   l   s
   		t   ForwardTypeRaiseRulec           B   s/   e  Z d  Z d Z d „  Z d „  Z d „  Z RS(   s1   
    Class for applying forward type raising
    i   c         C   s   t  |  _ d  S(   N(   t   ForwardTR*   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR      s    c         c   s”   | i  ƒ  | i ƒ  j p d  Sn xl |  i i | i ƒ  | i ƒ  ƒ D]I } t d | i ƒ  d | d |  i ƒ } | i | | f ƒ o	 | VqC qC Wd  S(   NR   R   R   (   R   R   R*   R-   R   R   R   R.   (   R
   R/   R0   R1   R2   R3   R4   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR5   ‘   s    " $c         C   s   t  |  i ƒ S(   N(   R6   R*   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR7   ™   s    (   R!   R"   R(   R8   R   R5   R7   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR9   ‰   s
   		t   BackwardTypeRaiseRulec           B   s/   e  Z d  Z d Z d „  Z d „  Z d „  Z RS(   s3   
    Class for applying backward type raising.
    i   c         C   s   t  |  _ d  S(   N(   t	   BackwardTR*   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   ¢   s    c         c   s”   | i  ƒ  | i ƒ  j p d  Sn xl |  i i | i ƒ  | i ƒ  ƒ D]I } t d | i ƒ  d | d |  i ƒ } | i | | f ƒ o	 | VqC qC Wd  S(   NR   R   R   (   R   R   R*   R-   R   R   R   R.   (   R
   R/   R0   R1   R2   R3   R4   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR5   ¤   s    " $c         C   s   t  |  i ƒ S(   N(   R6   R*   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR7   ¬   s    (   R!   R"   R(   R8   R   R5   R7   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR;   œ   s
   		t   CCGChartParserc           B   s/   e  Z d  Z d d „ Z d „  Z e d „ Z RS(   sV   
    Chart parser for CCGs.
    Based largely on the ChartParser class from NLTK.
    i    c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   _lexicont   _rulest   _trace(   R
   t   lexicont   rulest   trace(    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   Ã   s    		c         C   s   |  i  S(   N(   R>   (   R
   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyRA   È   s    c         C   s¶  t  | ƒ } t t  | ƒ ƒ } |  i } xh t | i ƒ  ƒ D]T } xK | i | i | ƒ ƒ D]1 } t | | | i | ƒ ƒ } | i | d ƒ qY Wq: Wx
t d | i ƒ  d ƒ D]ï } xæ t d | i ƒ  | d ƒ D]Ç }	 x¾ t d | ƒ D]­ }
 |	 } |	 |
 } |	 | } xŠ | i	 d | | f ƒ D]p } xg | i	 d | | f ƒ D]M } xD |  i
 D]9 } d } x* | i | | | | ƒ D] } | d 7} qsWqNWq>WqWqæ WqÐ Wq¬ W| i | i ƒ  ƒ |  S(   Ni   i   i    R   (    (   t   listt   CCGChartR>   t   ranget
   num_leavest
   categoriesR'   R#   R.   t   selectR?   R5   t   parsesR   (   R
   t   tokenst   nR/   t   lext   indext   catR4   R   R   t   partt   lstartt   midt   rendt   leftt   rightR   t   edges_added_by_rulet   newedge(    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   nbest_parseÌ   s:    	     

  
  &(   R!   R"   R(   R   RA   R   RX   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR=   ¾   s   	RE   c           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   t  i |  | ƒ d  S(   N(   R   R   (   R
   RK   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR   î   s    c      	   C   st  | | j o | | Sn g  } g  | | <t  | t ƒ oU | | i ƒ  |  i | i ƒ  g ƒ } | | i ƒ  d f | g ƒ } | | | <| Sn xÕ |  i | ƒ D]Ä } g  }	 | D] }
 |	 |  i |
 | | | ƒ q¯ ~	 } t | ƒ d j o* t | d ƒ t d ƒ j o | g } n xK |  i	 | ƒ D]: } | i ƒ  t
 | i ƒ  ƒ f } | i | | | ƒ ƒ q$Wqž W| | | <| S(   Nt   Leafi    t    (   R   R#   R   t   _tokensR   t   child_pointer_listst   _treest   lent   typet   _choose_childrenR6   R   t   append(   R
   t   edget   completet   memot
   tree_classt   treest   wordR'   t   cplt   _[1]t   cpt   child_choicest   childrenR   (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyR]   ô   s*    
%
 ,0 
(   R!   R"   R   R]   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyRE   í   s   	c         C   s  |  i  ƒ  } d } d } x× | D]Ï \ } } d t t | ƒ t t | ƒ ƒ ƒ } | t t | ƒ ƒ d } | | t t | ƒ ƒ d } | d | t | ƒ d | 7} | t | ƒ d }	 |	 | t | ƒ d }
 | d |	 | d |
 7} q W| GH| GHt d |  ƒ d  S(   NRZ   i   t    i    (   R&   t   maxR^   R6   t   printCCGTree(   t   treet   leafcatst   leafstrt   catstrR'   RO   t   nextlent   lcatlent   rcatlent   lleaflent   rleaflen(    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   printCCGDerivation  s     % c      	   C   s  |  } t  | t ƒ p d |  t | ƒ Sn x& | D] } t | t | | ƒ ƒ } q3 Wt  | i t ƒ p= t | d |  t t | i ƒ ƒ d |  t | d ƒ ƒ Sn | i \ } } |  d | |  d t | ƒ GH| |  t t | ƒ ƒ d |  } | d t | ƒ GH| S(   Ni   i    Rm   t   -(   R   R   R^   Rn   Ro   t   nodet   tupleR6   (   t   lwidthRp   t   rwidtht   childR3   t   opt	   respadlen(    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyRo   '  s      "sv  
    :- S, NP, N, VP    # Primitive categories, S is the target primitive

    Det :: NP/N         # Family of words
    Pro :: NP
    TV :: VP/NP
    Modal :: (S\NP)/VP # Backslashes need to be escaped

    I => Pro             # Word -> Category mapping
    you => Pro
    
    the => Det

    # Variables have the special keyword 'var'
    # '.' prevents permutation
    # ',' prevents composition
    and => var\.,var/.,var

    which => (N\N)/(S/NP)

    will => Modal # Categories can be either explicit, or families.
    might => Modal

    cook => TV
    eat => TV

    mushrooms => N
    parsnips => N
    bacon => N
    c          C   s@   t  t t ƒ }  x* |  i d i ƒ  d ƒ D] } t | ƒ q( Wd  S(   Ns   I might cook and eat the baconi   (   R=   RM   t   DefaultRuleSetRX   t   splitRy   (   t   parsert   parse(    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pyt   demoe  s     t   __main__(%   R(   t   nltk.parse.apit   nltk.parse.chartR   R   R   t   nltkR   R   RA   R+   R   R#   R)   R9   R;   t   ForwardApplicationt   BackwardApplicationt   ApplicationRuleSett   ForwardCompositiont   BackwardCompositiont
   BackwardBxt   CompositionRuleSett   ForwardSubstitutiont
   BackwardSxt   SubstitutionRuleSett   TypeRaiseRuleSetR‚   t   ParserIR=   RE   Ry   Ro   t   parseLexiconRM   R†   R!   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/ccg/chart.pys   <module>   s8   

"				/#				