³ò
ŒB_Kc           @   sâ   d  d k  Z  d  d k l Z d  d k Te  i d ƒ Z e  i d ƒ Z e  i d ƒ Z e  i d ƒ Z e  i d ƒ Z	 d	 e
 f d
 „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z e d ƒ Z d S(   iÿÿÿÿN(   t   defaultdict(   t   *s   ([A-Za-z]+)(\[[A-Za-z,]+\])?s"   ([A-Za-z]+(?:\[[A-Za-z,]+\])?)(.*)s   ([\\/])([.,]?)([.,]?)(.*)s!   ([A-Za-z_]+)\s*(::|[-=]+>)\s*(.+)s   ([^#]*)(?:#.*)?t
   CCGLexiconc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sØ   
    Class representing a lexicon for CCG grammars.
    primitives - The list of primitive categories for the lexicon
    families - Families of categories
    entries - A mapping of words to possible categories
    c         C   s.   t  | ƒ |  _ | |  _ | |  _ | |  _ d  S(   N(   t   PrimitiveCategoryt   _startt   _primitivest	   _familiest   _entries(   t   selft   startt
   primitivest   familiest   entries(    (    s&   /p/zhu/06/nlp/nltk/nltk/ccg/lexicon.pyt   __init__*   s    		c         C   s   |  i  | S(   N(   R   (   R   t   word(    (    s&   /p/zhu/06/nlp/nltk/nltk/ccg/lexicon.pyt
   categories1   s    c         C   s   |  i  S(   N(   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/ccg/lexicon.pyR	   5   s    c         C   s–   d } t  } xƒ |  i i ƒ  D]r } | p | d } n | | d } t  } x@ |  i | D]1 } | p | d } n t } | t | ƒ } qY Wq W| S(   Nt    s   
s    => s    | (   t   TrueR   t   keyst   Falset   str(   R   t   stt   firstt   identt   cat(    (    s&   /p/zhu/06/nlp/nltk/nltk/ccg/lexicon.pyt   __str__:   s      (   t   __name__t
   __module__t   __doc__R   R   R	   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/ccg/lexicon.pyR   #   s
   			c         C   s¹   |  d } d } xk | d j o] | i  d ƒ oL | i  d ƒ o  t | ƒ \ } } | | } q | | d } | d } q W| i  d ƒ o | d | d f Sn t d |  d ‚ d  S(   Ni   t   (R   t   )i    s   Unmatched bracket in string 's   '(   t
   startswitht   matchBracketst   AssertionError(   t   stringt   restt   insidet   part(    (    s&   /p/zhu/06/nlp/nltk/nltk/ccg/lexicon.pyR    R   s    
!c         C   s1   |  i  d ƒ o t |  ƒ Sn t i |  ƒ i ƒ  S(   NR   (   R   R    t
   reNextPrimt   matcht   groups(   R"   (    (    s&   /p/zhu/06/nlp/nltk/nltk/ccg/lexicon.pyt   nextCategoryc   s    c         C   s   t  |  d |  d ƒ S(   Ni    i   (   t	   Direction(   t   app(    (    s&   /p/zhu/06/nlp/nltk/nltk/ccg/lexicon.pyt   parseApplicationi   s    c         C   s#   |  o |  d d !i  d ƒ Sn g  S(   Ni   iÿÿÿÿt   ,(   t   split(   t   subscr(    (    s&   /p/zhu/06/nlp/nltk/nltk/ccg/lexicon.pyt   parseSubscriptsm   s    c         C   sû   |  d d j o= |  d d  j o( | d  j o t ƒ  } n | | f SqN n |  d } | | j oM | | \ } } | d  j o
 | } n | i | | f g ƒ } | | f Sn | | j o' t |  d ƒ } t | | ƒ | f Sn t d | d ‚ d  S(   Ni    t   vari   s   String 's-   ' is neither a family nor primitive category.(   t   Nonet   CCGVart
   substituteR0   R   R!   (   t   chunksR
   R   R1   t   catstrR   t   cvart   subscrs(    (    s&   /p/zhu/06/nlp/nltk/nltk/ccg/lexicon.pyt   parsePrimitiveCategorys   s     

c         C   s   t  |  | | ƒ d S(   Ni    (   t   augParseCategory(   t   lineR
   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/ccg/lexicon.pyt   parseCategory‹   s    c   
      C   sG  t  |  ƒ \ } } | i d ƒ o& t | d d !| | | ƒ \ } } n+ t t i | ƒ i ƒ  | | | ƒ \ } } xÈ | d j oº t i | ƒ i ƒ  } t | d d !ƒ } | d } t  | ƒ \ } } | i d ƒ o& t | d d !| | | ƒ \ }	 } n+ t t i | ƒ i ƒ  | | | ƒ \ }	 } t	 | |	 | ƒ } qu W| | f S(   NR   i   iÿÿÿÿR   i    i   (
   R)   R   R:   R9   t   rePrimR'   R(   t   reAppR,   t   FunctionalCategory(
   R;   R
   R   R1   R   R#   t   resR+   t   dirt   arg(    (    s&   /p/zhu/06/nlp/nltk/nltk/ccg/lexicon.pyR:      s    &*
&*c         C   s3  g  } h  } t  t ƒ } x|  i ƒ  D]ó } t i | ƒ i ƒ  d i ƒ  } | d j o q% n | i d ƒ oB | g  } | d i ƒ  i d ƒ D] } | | i ƒ  q ~ } q% t	 i | ƒ i ƒ  \ } } }	 t
 |	 | | ƒ \ }
 } | d j o |
 | f | | <q% | | i |
 ƒ q% Wt | d | | | ƒ S(   Ni    R   s   :-i   R-   s   ::(   R    t   listt
   splitlinest   reCommR'   R(   t   stripR   R.   t   reLexR:   t   appendR   (   t   lex_strR
   R   R   R;   t   _[1]t   primR   t   sepR6   R   R1   (    (    s&   /p/zhu/06/nlp/nltk/nltk/ccg/lexicon.pyt   parseLexicon©   s      Bsª  
    # Rather minimal lexicon based on the openccg `tinytiny' grammar.
    # Only incorporates a subset of the morphological subcategories, however.
    :- S,NP,N                    # Primitive categories
    Det :: NP/N                  # Determiners
    Pro :: NP
    IntransVsg :: S\NP[sg]    # Tensed intransitive verbs (singular)
    IntransVpl :: S\NP[pl]    # Plural
    TransVsg :: S\NP[sg]/NP   # Tensed transitive verbs (singular)
    TransVpl :: S\NP[pl]/NP   # Plural

    the => NP[sg]/N[sg]
    the => NP[pl]/N[pl]

    I => Pro
    me => Pro
    we => Pro
    us => Pro

    book => N[sg]
    books => N[pl]
   
    peach => N[sg]
    peaches => N[pl]

    policeman => N[sg]
    policemen => N[pl]

    boy => N[sg]
    boys => N[pl]
   
    sleep => IntransVsg
    sleep => IntransVpl
   
    eat => IntransVpl
    eat => TransVpl
    eats => IntransVsg
    eats => TransVsg

    see => TransVpl
    sees => TransVsg
    (   t   ret   nltkR    t   apit   compileR=   R&   R>   RG   RE   t   objectR   R    R)   R,   R0   R9   R<   R2   R:   RM   t   openccg_tinytiny(    (    (    s&   /p/zhu/06/nlp/nltk/nltk/ccg/lexicon.pys   <module>   s$   
/							)