ó
ù`]c           @  si  d  Z  d d l m Z m Z m Z d d l Z d d l m 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 m Z d d	 l m Z m Z m Z m Z m Z m Z m Z m Z m Z d d
 l m Z d d l Td d l Td d l  Td e f d „  ƒ  YZ! d e f d „  ƒ  YZ" e d e f d „  ƒ  Yƒ Z# e d e f d „  ƒ  Yƒ Z$ e d e f d „  ƒ  Yƒ Z% 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. d „  Z/ e d ƒ Z0 d „  Z1 e2 d k ree1 ƒ  n  d S(    uB  
The lexicon is constructed by calling
``lexicon.fromstring(<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.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   print_functiont   divisiont   unicode_literalsN(   t   string_types(   t   ParserI(   t   AbstractChartRulet   EdgeIt   Chart(   t   Tree(   t
   fromstringt   Token(	   t   ForwardTt	   BackwardTt   ForwardApplicationt   BackwardApplicationt   ForwardCompositiont   BackwardCompositiont   ForwardSubstitutiont
   BackwardBxt
   BackwardSx(   t   python_2_unicode_compatible(   t   *t   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 RS(   c         C  s1   | |  _  | |  _ | |  _ | | | f |  _ d  S(   N(   t   _spant   _categt   _rulet   _comparison_key(   t   selft   spant   categt   rule(    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   __init__?   s    			c         C  s   |  j  S(   N(   R   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   lhsF   s    c         C  s   |  j  S(   N(   R   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR   I   s    c         C  s   |  j  d S(   Ni    (   R   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   startL   s    c         C  s   |  j  d S(   Ni   (   R   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   endO   s    c         C  s   |  j  d |  j d S(   Ni   i    (   R   R   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   lengthR   s    c         C  s   d S(   N(    (    (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   rhsU   s    c         C  s   d S(   Ni    (    (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   dotX   s    c         C  s   t  S(   N(   t   True(   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   is_complete[   s    c         C  s   t  S(   N(   t   False(   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   is_incomplete^   s    c         C  s   d  S(   N(   t   None(   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   nextsyma   s    c         C  s   |  j  S(   N(   R   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR   d   s    c         C  s   |  j  S(   N(   R   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR   g   s    (   t   __name__t
   __module__R   R    R   R!   R"   R#   R$   R%   R'   R)   R+   R   R   (    (    (    s-   lib/python2.7/site-packages/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 RS(   u<   
    Class representing leaf edges in a CCG derivation.
    c         C  s7   | |  _  | |  _ | |  _ | | j ƒ  | f |  _ d  S(   N(   t   _post   _tokent   _leafR   R   (   R   t   post   tokent   leaf(    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR   p   s    			c         C  s   |  j  j ƒ  S(   N(   R0   R   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR    w   s    c         C  s   |  j  |  j  d f S(   Ni   (   R/   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR   z   s    c         C  s   |  j  S(   N(   R/   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR!   }   s    c         C  s   |  j  d S(   Ni   (   R/   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR"   €   s    c         C  s   d S(   Ni   (    (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR#   ƒ   s    c         C  s   |  j  S(   N(   R1   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR$   †   s    c         C  s   d S(   Ni    (    (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR%   ‰   s    c         C  s   t  S(   N(   R&   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR'   Œ   s    c         C  s   t  S(   N(   R(   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR)      s    c         C  s   d  S(   N(   R*   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR+   ’   s    c         C  s   |  j  S(   N(   R0   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR3   •   s    c         C  s   |  j  j ƒ  S(   N(   R0   R   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR   ˜   s    c         C  s   |  j  S(   N(   R1   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR4   ›   s    (   R,   R-   t   __doc__R   R    R   R!   R"   R#   R$   R%   R'   R)   R+   R3   R   R4   (    (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR.   k   s   													t   BinaryCombinatorRulec           B  s/   e  Z d  Z d Z d „  Z d „  Z d „  Z RS(   uw   
    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-   lib/python2.7/site-packages/nltk/ccg/chart.pyR   ¨   s    c         c  sÀ   | j  ƒ  | j ƒ  k s d  S|  j j | j ƒ  | j ƒ  ƒ r¼ x| |  j j | j ƒ  | j ƒ  ƒ D]V } t d | j ƒ  | j  ƒ  f d | d |  j ƒ } | j | | | f ƒ r_ | Vq_ q_ Wn  d  S(   NR   R   R   (   R"   R!   R7   t   can_combineR   t   combineR   t   insert(   R   t   chartt   grammart	   left_edget
   right_edget   rest   new_edge(    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   apply¬   s    !(c         C  s   d |  j  S(   Nu   %s(   R7   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   __str__¾   s    (   R,   R-   R5   t   NUMEDGESR   RB   RC   (    (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR6   Ÿ   s
   		t   ForwardTypeRaiseRulec           B  s/   e  Z d  Z d Z d „  Z d „  Z d „  Z RS(   u1   
    Class for applying forward type raising
    i   c         C  s   t  |  _ d  S(   N(   R   R7   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR   Ì   s    c         c  s   | j  ƒ  | j ƒ  k s d  Sxj |  j j | j ƒ  | j ƒ  ƒ D]G } t d | j ƒ  d | d |  j ƒ } | j | | f ƒ r> | Vq> q> Wd  S(   NR   R   R   (   R"   R!   R7   R:   R   R   R   R;   (   R   R<   R=   R>   R?   R@   RA   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyRB   Ï   s    ($c         C  s   d |  j  S(   Nu   %s(   R7   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyRC   Ø   s    (   R,   R-   R5   RD   R   RB   RC   (    (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyRE   Ä   s
   			t   BackwardTypeRaiseRulec           B  s/   e  Z d  Z d Z d „  Z d „  Z d „  Z RS(   u3   
    Class for applying backward type raising.
    i   c         C  s   t  |  _ d  S(   N(   R   R7   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR   ä   s    c         c  s   | j  ƒ  | j ƒ  k s d  Sxj |  j j | j ƒ  | j ƒ  ƒ D]G } t d | j ƒ  d | d |  j ƒ } | j | | f ƒ r> | Vq> q> Wd  S(   NR   R   R   (   R"   R!   R7   R:   R   R   R   R;   (   R   R<   R=   R>   R?   R@   RA   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyRB   ç   s    ($c         C  s   d |  j  S(   Nu   %s(   R7   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyRC   ð   s    (   R,   R-   R5   RD   R   RB   RC   (    (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyRF   Ü   s
   			t   CCGChartParserc           B  s,   e  Z d  Z d d „ Z d „  Z d „  Z RS(   uV   
    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-   lib/python2.7/site-packages/nltk/ccg/chart.pyR     s    		c         C  s   |  j  S(   N(   RH   (   R   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyRK     s    c         C  s²  t  | ƒ } t t  | ƒ ƒ } |  j } xh t | j ƒ  ƒ D]T } xK | j | j | ƒ ƒ D]1 } t | | | j | ƒ ƒ } | j | d ƒ qY Wq: Wx
t d | j ƒ  d ƒ D]ï } xæ t d | j ƒ  | d ƒ D]Ç } x¾ t d | ƒ D]­ }	 | }
 | |	 } | | } xŠ | j	 d |
 | f ƒ D]p } xg | j	 d | | f ƒ D]M } xD |  j
 D]9 } d } x* | j | | | | ƒ D] } | d 7} qsWqNWq>WqWqæ WqÐ Wq¬ W| j | j ƒ  ƒ S(   Ni   i   i    R   (    (   t   listt   CCGChartRH   t   ranget
   num_leavest
   categoriesR4   R.   R;   t   selectRI   RB   t   parsesR!   (   R   t   tokensR<   t   lext   indexR3   RA   R   R!   t   partt   lstartt   midt   rendt   leftt   rightR   t   edges_added_by_rulet   newedge(    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   parse  s(    	 $

&(   R,   R-   R5   R   RK   R`   (    (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyRG   
  s   	RO   c           B  s   e  Z d  „  Z d „  Z RS(   c         C  s   t  j |  | ƒ d  S(   N(   R   R   (   R   RU   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR   ;  s    c         C  sh  | s t  d ƒ ‚ | | k r& | | St | t ƒ rŒ | | j ƒ  |  j | j ƒ  g ƒ } | | j ƒ  d f | g ƒ } | g | | <| g Sg  | | <g  } x» |  j | ƒ D]ª } g  | D] }	 |  j |	 | | | ƒ ^ q¹ }
 xv t j	 |
 Œ  D]e } t
 |  j | j ƒ  | j ƒ  !| j ƒ  t | | ƒ ƒ t | j ƒ  ƒ f } | j | | | ƒ ƒ qí Wq¬ W| | | <| S(   Nu&   CCGChart cannot build incomplete treesu   Leaf(   t   AssertionErrort
   isinstanceR.   R3   t   _tokensR!   t   child_pointer_listst   _treest	   itertoolst   productR
   R"   R    t   compute_semanticst   strR   t   append(   R   t   edget   completet   memot
   tree_classt   wordR4   t   treest   cplt   cpt   child_choicest   childrenR    (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyRe   A  s*    %
+	
(   R,   R-   R   Re   (    (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyRO   :  s   	c         C  s2  |  d j  ƒ  d j ƒ  d  k r$ d  St |  ƒ d k rt | j ƒ  t ƒ rb |  d |  d g }  n  | j ƒ  j } |  d j  ƒ  d j ƒ  } |  d j  ƒ  d j ƒ  } t | t ƒ rÁ t	 | | ƒ St | t
 ƒ rÝ t | | ƒ St | t ƒ rù t | | ƒ St d | d ƒ ‚ n t |  d j  ƒ  d j ƒ  ƒ Sd  S(   Ni    i   i   u   Unsupported combinator 'u   '(   t   labelt	   semanticsR*   t   lenRb   R   t   BackwardCombinatorR7   t   UndirectedFunctionApplicationt   compute_function_semanticst   UndirectedCompositiont   compute_composition_semanticst   UndirectedSubstitutiont   compute_substitution_semanticsRa   t   compute_type_raised_semantics(   Rt   Rk   R8   t   functiont   argument(    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyRh   a  s      c         C  s  |  j  ƒ  } d } d } xÉ | D]Á \ } } d | } d t t | ƒ t | ƒ ƒ } | t | ƒ d } | | t | ƒ d }	 | d | | d |	 7} | t | ƒ d }
 |
 | t | ƒ d } | d |
 | d | 7} q Wt | j ƒ  ƒ t | j ƒ  ƒ t d |  ƒ d  S(   Nu    u   %si   u    i    (   R2   t   maxRw   t   printt   rstript   printCCGTree(   t   treet   leafcatst   leafstrt   catstrR4   t   catt   str_catt   nextlent   lcatlent   rcatlent   lleaflent   rleaflen(    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   printCCGDerivation|  s    
c         C  sX  |  } t  | t ƒ s' d |  t | ƒ Sx& | D] } t | t | | ƒ ƒ } q. Wt  | j ƒ  t ƒ sŸ t | d |  t d | j ƒ  ƒ d |  t | d ƒ ƒ S| j ƒ  \ } } | d k rÁ | St |  d | |  d d | ƒ d | j ƒ  } | j	 ƒ  d  k	 r&| d t | j	 ƒ  ƒ d 7} n  | |  t | ƒ d |  } t | d | ƒ | S(	   Ni   u   %si    u   Leafu    u   -u    {u   }(   Rb   R   Rw   R‚   R…   Ru   t   tupleRƒ   R   Rv   R*   Ri   (   t   lwidthR†   t   rwidtht   childR3   t   opt   str_rest	   respadlen(    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyR…   •  s$    7"!ur  
    :- 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' |  j d j ƒ  ƒ D] } t | ƒ q% Wd  S(   Nu   I might cook and eat the bacon(   RG   RV   t   DefaultRuleSetR`   t   splitR‘   (   t   parserR`   (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   demoÞ  s    u   __main__(3   R5   t
   __future__R    R   R   Rf   t   sixR   t
   nltk.parseR   t   nltk.parse.chartR   R   R   t	   nltk.treeR   t   nltk.ccg.lexiconR	   R
   t   nltk.ccg.combinatorR   R   R   R   R   R   R   R   R   t   nltk.compatR   t   nltk.ccg.logict   nltk.sem.logicR   R.   R6   RE   RF   t   ApplicationRuleSett   CompositionRuleSett   SubstitutionRuleSett   TypeRaiseRuleSetR™   RG   RO   Rh   R‘   R…   RV   Rœ   R,   (    (    (    s-   lib/python2.7/site-packages/nltk/ccg/chart.pyt   <module>   sN   @


-4$				0'			'		