mò
:¦”Ec           @   sO  d  Z  d Z d Z d k Td „  Z d „  Z d2 d „ Z d „  Z d „  Z	 e
 d	 „ Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d3 d4 d5 d6 d7 f Z d d „ Z h  e d8 d9 d: d; d< d= d> d? d@ g	 <e dA dB dC dD dE dF dG dH g <e dI dJ dK dL g <e g  <Z d d/ d0 „ Z d/ d1 „ Z d2 S(M   så  Utility functions to use with the wordnet module.

Usage
-----
    >>> dog = N['dog'][0]

    # (First 10) adjectives that are transitively SIMILAR to the main sense of 'red'
    >>> closure(ADJ['red'][0], SIMILAR)[:10]
    ['red' in {adjective: red, reddish, ruddy, blood-red, carmine, cerise, cherry, cherry-red, crimson, ruby, ruby-red, scarlet}, {adjective: chromatic}, {adjective: amber, brownish-yellow, yellow-brown}, {adjective: amethyst}, {adjective: aureate, gilded, gilt, gold, golden}, {adjective: azure, cerulean, sky-blue, bright blue}, {adjective: blue, bluish, blueish, light-blue, dark-blue, blue-black}, {adjective: bluish green, blue-green, cyan, teal}, {adjective: blushful, rosy}, {adjective: bottle-green}]

    >>> # Adjectives that are transitively SIMILAR to any of the senses of 'red'
    >>> #flatten1(map(lambda sense:closure(sense, SIMILAR), ADJ['red']))    # too verbose

    >>> # Hyponyms of the main sense of 'dog'(n.) that are homophonous with verbs
    >>> filter(lambda sense:V.get(sense.form), flatten1(map(lambda e:e.getSenses(), hyponyms(N['dog'][0]))))
    ['dog' in {noun: dog, domestic dog, Canis familiaris}, 'pooch' in {noun: pooch, doggie, doggy, barker, bow-wow}, 'toy' in {noun: toy dog, toy}, 'hound' in {noun: hound, hound dog}, 'basset' in {noun: basset, basset hound}, 'cocker' in {noun: cocker spaniel, English cocker spaniel, cocker}, 'bulldog' in {noun: bulldog, English bulldog}]

    >>> # Find the senses of 'raise'(v.) and 'lower'(v.) that are antonyms
    >>> filter(lambda p:p[0] in p[1].pointerTargets(ANTONYM), product(V['raise'].getSenses(), V['lower'].getSenses()))
    [('raise' in {verb: raise, lift, elevate, get up, bring up}, 'lower' in {verb: lower, take down, let down, get down, bring down})]
s"   Oliver Steele <steele@osteele.com>s   2.0(   t   *c         C   sQ   t  |  d ƒ p= t |  t ƒ o t |  d |  d ‚ qM t |  d ‚ n d  S(   Nt   pointerss     is not a Sense or Synset.  Try s   [0] instead.s    is not a Sense or Synset(   t   hasattrt   entityt
   isinstancet   Wordt	   TypeError(   R   (    (    tO   /afs/cs.wisc.edu/p/zhu/public/html/space2/TTP2/basic_image_selection/wntools.pyt   _requireSource)   s    c         C   sZ   t  |  t ƒ o t | d „ |  i ƒ  ƒ Sn t |  ƒ |  g t | d „ |  i | ƒ ƒ S(   s°  
    >>> dog = N['dog'][0]
    >>> from pprint import pprint
    >>> pprint(tree(dog, HYPERNYM))
    ['dog' in {noun: dog, domestic dog, Canis familiaris},
     [{noun: canine, canid},
      [{noun: carnivore},
       [{noun: placental, placental mammal, eutherian, eutherian mammal},
        [{noun: mammal},
         [{noun: vertebrate, craniate},
          [{noun: chordate},
           [{noun: animal, animate being, beast, brute, creature, fauna},
            [{noun: organism, being},
             [{noun: living thing, animate thing},
              [{noun: object, physical object}, [{noun: entity}]]]]]]]]]]]]
    >>> #pprint(tree(dog, HYPONYM)) # too verbose to include here
    c         C   s   t  |  | ƒ S(   N(   t   treet   st   t(   R
   R   (    (    R   t   <lambda>C   s    c         C   s   t  |  | ƒ S(   N(   R	   R
   R   (   R
   R   (    (    R   R   E   s    N(   R   t   sourceR   t   mapt   pointerTypet	   getSensesR   t   pointerTargets(   R   R   (    (    R   R	   0   s
     
c         C   s£   t  |  t ƒ o& t t t | d „ |  i ƒ  ƒ ƒ Sn t |  ƒ | d j o
 g  } n |  | j o; | i |  ƒ x+ |  i | ƒ D] } t | | | ƒ q Wn | S(   sp  Return the transitive closure of source under the pointerType
    relationship.  If source is a Word, return the union of the
    closures of its senses.
    
    >>> dog = N['dog'][0]
    >>> closure(dog, HYPERNYM)
    ['dog' in {noun: dog, domestic dog, Canis familiaris}, {noun: canine, canid}, {noun: carnivore}, {noun: placental, placental mammal, eutherian, eutherian mammal}, {noun: mammal}, {noun: vertebrate, craniate}, {noun: chordate}, {noun: animal, animate being, beast, brute, creature, fauna}, {noun: organism, being}, {noun: living thing, animate thing}, {noun: object, physical object}, {noun: entity}]
    c         C   s   t  |  | ƒ S(   N(   R	   R
   R   (   R
   R   (    (    R   R   Q   s    N(   R   R   R   t   reducet   unionR   R   R   R   t   accumulatort   Nonet   appendR   t   targett   closure(   R   R   R   R   (    (    R   R   G   s     &

 c         C   s   t  |  t ƒ S(   si   Return source and its hyponyms.  If source is a Word, return
    the union of the hyponyms of its senses.N(   R   R   t   HYPONYM(   R   (    (    R   t   hyponyms[   s     c         C   s   t  |  t ƒ S(   sk   Return source and its hypernyms.  If source is a Word, return
    the union of the hypernyms of its senses.N(   R   R   t   HYPERNYM(   R   (    (    R   t	   hypernyms`   s     c         C   s*   t  t |  | ƒ t | | ƒ ƒ d g d S(   s/  Return the meet of a and b under the pointerType relationship.
    
    >>> meet(N['dog'][0], N['cat'][0])
    {noun: carnivore}
    >>> meet(N['dog'][0], N['person'][0])
    {noun: organism, being}
    >>> meet(N['thought'][0], N['belief'][0])
    {noun: content, cognitive content, mental object}
    i    N(   t   intersectionR   t   aR   t   bR   (   R   R   R   (    (    R   t   meetf   s    	 c         C   s   |  t | ƒ  | j S(   s_   Return true iff _str_ starts with _prefix_.
    
    >>> startsWith('unclear', 'un')
    1
    N(   t   strt   lent   prefix(   R!   R#   (    (    R   t
   startsWithv   s     c         C   s   |  t | ƒ | j S(   s[   Return true iff _str_ ends with _suffix_.
    
    >>> endsWith('clearly', 'ly')
    1
    N(   R!   R"   t   suffix(   R!   R%   (    (    R   t   endsWith~   s     c         C   s)   |  | j p t i |  ƒ t i | ƒ j S(   s£   Return true iff a and b have the same lowercase representation.
    
    >>> equalsIgnoreCase('dog', 'Dog')
    1
    >>> equalsIgnoreCase('dOg', 'DOG')
    1
    N(   R   R   t   stringt   lower(   R   R   (    (    R   t   equalsIgnoreCase†   s     c         C   s   t  |  ƒ t t t f j S(   sÅ   Return true iff _item_ is a Sequence (a List, String, or Tuple).
    
    >>> issequence((1,2))
    1
    >>> issequence([1,2])
    1
    >>> issequence('12')
    1
    >>> issequence(1)
    0
    N(   t   typet   itemt   ListTypet
   StringTypet	   TupleType(   R+   (    (    R   t
   issequence•   s     c         C   s9   g  } x, |  D]$ } | | j o | i | ƒ q q W| S(   sc   Return the intersection of _u_ and _v_.
    
    >>> intersection((1,2,3), (2,3,4))
    [2, 3]
    N(   t   wt   ut   et   vR   (   R1   R3   R2   R0   (    (    R   R   £   s      c         C   sh   t  |  ƒ } | |  j o d k } | i | ƒ } n x, | D]$ } | | j o | i | ƒ q< q< W| S(   s[   Return the union of _u_ and _v_.
    
    >>> union((1,2,3), (2,3,4))
    [1, 2, 3, 4]
    N(   t   listR1   R0   t   copyR3   R2   R   (   R1   R3   R2   R0   R5   (    (    R   R   ¯   s     	 c         C   s   t  t | d „ |  ƒ ƒ S(   sÁ   Return the Cartesian product of u and v.
    
    >>> product("123", "abc")
    [('1', 'a'), ('1', 'b'), ('1', 'c'), ('2', 'a'), ('2', 'b'), ('2', 'c'), ('3', 'a'), ('3', 'b'), ('3', 'c')]
    c         C   s   t  |  d „ | ƒ S(   Nc         C   s
   | |  f S(   N(   R   R   (   R   R   (    (    R   R   Ä   s    (   R   R   R3   (   R   R3   (    (    R   R   Ä   s    N(   t   flatten1R   R3   R1   (   R1   R3   (    (    R   t   product¾   s     c         C   s9   g  } x, |  D]$ } | | j o | i | ƒ q q W| S(   su  Return a copy of _sequence_ with equal items removed.
    
    >>> removeDuplicates("this is a test")
    ['t', 'h', 'i', 's', ' ', 'a', 'e']
    >>> removeDuplicates(map(lambda tuple:apply(meet, tuple), product(N['story'].getSenses(), N['joke'].getSenses())))
    [{noun: message, content, subject matter, substance}, None, {noun: abstraction}, {noun: communication}]
    N(   R   t   sequenceR+   R   (   R8   R   R+   (    (    R   t   removeDuplicatesÆ   s      c         C   so   g  } xb |  D]Z } t | ƒ t j o t | ƒ } n t | ƒ t j o | i | ƒ q | i | ƒ q W| S(   N(	   R   R8   R+   R*   R.   R4   R,   t   extendR   (   R8   R+   R   (    (    R   R6   Ù   s     t    t   -t    t   .t   nounc         C   s%   d t  | ƒ d „ } | t |  t ƒ S(   sÿ  Search for _form_ in the index file corresponding to
    _pos_. getIndex applies to _form_ an algorithm that replaces
    underscores with hyphens, hyphens with underscores, removes
    hyphens and underscores, and removes periods in an attempt to find
    a form of the string that is an exact match for an entry in the
    index file corresponding to _pos_.  getWord() is called on each
    transformed string until a match is found or all the different
    strings have been tried. It returns a Word or None.i   c         C   s¿   | o | i | ƒ o | | Sn™ | o‘ | d \ } } t i | | | ƒ o
 | | j } | o | i | ƒ o | | Sn |  |  | | d d d ƒp | o |  |  | | d ƒ Sn d  S(   Ni    i   t   lookup(   R@   t
   dictionaryt   has_keyt   formt   substitutionst   oldt   newR'   t   replacet
   substitutet   trySubstitutions(   RI   RC   RD   R@   RA   RE   RF   RH   (    (    R   RI   ô   s    "N(   t   dictionaryFort   posRI   t   returnMatchRC   t   GET_INDEX_SUBSTITUTIONS(   RC   RK   RI   (    (    R   t   getIndexë   s     
R
   t   sest   vest   ft   xest   xt   zest   zt   chest   cht   shest   sht   ment   mant   iest   yt   esR2   t   edt   ingt   ert   esti    c   	      C   s¤   d k  l } l } | | ƒ } t i i t h  t d <t	 d <t
 d <t d <| d ƒ } t | ƒ } t | } d | | ƒ | | g  d „ } | | |  | ƒ S(
   sy  Recursively uninflect _form_, and return the first form found
    in the dictionary.  If _collect_ is true, a sequence of all forms
    is returned, instead of just the first one.
    
    >>> morphy('dogs')
    'dog'
    >>> morphy('churches')
    'church'
    >>> morphy('aardwolves')
    'aardwolf'
    >>> morphy('abaci')
    'abacus'
    >>> morphy('hardrock', 'adv')
    (   s   _normalizePOSs   _dictionaryForR?   t   verbt   adjt   advs   .exci   c         C   s  d  k  } t | | ƒ }	 |	 o! |	 | i |	 d ƒ d d !} n | o0 | i | ƒ o  | o | i | ƒ q| Sn¢ | o‹ | d \ } }
 | d } d  } t | | ƒ o | t | ƒ  |
 } n |  |  | | ƒ p | o |  |  | | ƒ } | o | p | Sn | o | Sn d  S(   NR;   i   iÿÿÿÿi    (   R'   t   binarySearchFilet   excfileRC   t
   exceptionst   findR@   RA   RB   t   collectt
   collectionR   RD   RE   RF   R   RH   R&   R"   RI   (   RI   RC   RD   R@   RA   Rg   Rj   Rk   RE   Rh   RF   RH   R'   (    (    R   RI   0  s$    	!
,N(   t   wordnett   _normalizePOSt   _dictionaryForRK   t   ost   patht   joint   WNSEARCHDIRt   NOUNt   VERBt	   ADJECTIVEt   ADVERBt   fnamet   openRg   t   MORPHOLOGICAL_SUBSTITUTIONSRD   Rj   RI   RC   (	   RC   RK   Rj   Rn   Rw   Rm   Rg   RI   RD   (    (    R   t   morphy  s     A
c         C   s3   d  k  } d  k } |  o d  | _ n | i | ƒ S(   N(   t   doctestt   wntoolst   resetR   t   mastert   testmod(   R}   R{   R|   (    (    R   t   _testS  s    N(   R;   R<   (   R<   R;   (   R<   R=   (   R;   R=   (   R>   R=   (   R
   R=   (   RO   R
   (   RP   RQ   (   RR   RS   (   RT   RU   (   RV   RW   (   RX   RY   (   RZ   R[   (   R\   R]   (   R
   R=   (   R\   R]   (   R^   R2   (   R^   R=   (   R_   R2   (   R_   R=   (   R`   R2   (   R`   R=   (   Ra   R=   (   Rb   R=   (   Ra   R2   (   Rb   R2   (   t   __doc__t
   __author__t   __version__Rl   R   R	   R   R   R   R   R   R    R$   R&   R)   R/   R   R   R7   R9   R6   RM   RN   Rs   Rt   Ru   Rv   Ry   Rz   R€   (   R€   R7   R6   R)   R9   RN   R$   R   Rƒ   RM   R   R‚   R   Rz   R   R   R   R	   R/   R&   R    Ry   (    (    R   t   ?   s.   													i7