
3Ic        ,   @   s  d  Z  d d k l Z d d k Z d d k l Z d d k l Z l Z d d k l	 Z	 l
 Z
 d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k l Z d d k l Z d d	 k l Z l Z e a d a d a  d
 e f d     YZ! d   Z" d d d d d d d d d d d d d d d d d d d d d d d d d d d g Z# d    Z$ d!   Z% d" e d d#  Z& d$   Z' d%   Z( d&   Z) d' Z* d( Z+ d) Z, d* Z- d+ Z. d, Z/ d- Z0 d. Z1 d/ Z2 d0 Z3 d1 Z4 d2 Z5 d3 Z6 d4 Z7 d5 Z8 d6 Z9 d7 Z: d8 Z; d9 Z< d: Z= d; Z> d< Z? d= Z, d> Z? d? Z> d@ Z@ dA ZA dB   ZB dC   ZC dD   ZD dE ZE dF ZF dG ZG dH   ZH dI   ZI dJ   ZJ dK   ZK dL   ZL dM   ZM dN   ZN dO   ZO eN eL eK dP    dQ ZP dR   ZQ dS   ZR eS   dT  ZT dU   ZU dV eV f dW     YZW dX   ZX dY   ZY dZ   ZZ d[   Z[ d\   Z\ d]   Z] d^   Z^ d_   Z_ d`   Z` da   Za db   Zb dc   Zc ed dd j o ec   n de g Ze d S(f   s  
A WordNet Browser application which launches the default browser
(if it is not already running) and opens a new tab with a connection
to http://localhost:port/ .  It also starts an HTTP server on the
specified port and begins serving browser requests.  The default
port is 8000.  (For command-line help, run "python wordnet -h")
This application requires that the user's web browser supports
Javascript.
i(   t   pathN(   t   argv(   t
   HTTPServert   BaseHTTPRequestHandler(   t
   quote_plust   unquote_plus(   t   defaultdict(   t   wordnet(   t   Synsett   Lemmat   MyServerHandlerc           B   s/   e  Z d    Z d   Z e d  Z d   Z RS(   c         C   s   |  i    d  S(   N(   t	   send_head(   t   self(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   do_HEADe   s    c   	      C   s  |  i  d } t |  d j o- t o d } d } qhd GHt i d  n| d j o d } t   } n| d	 j oA d
 } t o t o t a t t	  } n t t  } d } n| i
 d  o d
 } t |  } | d j oY d } t i  i |  o t |  i   } qUt | d | d d d d t } qh| } t |  } n| i d  o d
 } | i d  d i d  } g  } | D]: } | i d  o$ | | i d  d i d d  qq~ d } t |  \ } } nv | i d  o, d
 } | t d  } t |  \ } } n: | d j o d
 } t d  \ } } n d } d | } |  i |  |  i i |  d  S(   Ni   s   SHUTDOWN THE SERVERs#   Server must be killed with SIGTERM.s
   text/plains   Server shutting down!i    s   favicon.icos   image/x-icont    s	   text/htmlt   greens   .htmls'   NLTK Wordnet Browser Database Info.htmls   * Database Info *s    <p>The database info file:<p><b>s   </b>s   <p>was not found. Run this:s   <p><b>python dbinfo_html.py</b>s   <p>to produce it.t   searcht   ?t   &t   nextWordt   =t   +t    t   lookup_t
   start_pageR   s   Could not parse request: '%s'(   R    R   t   server_modet   ost   _exitt   favicon_datat   firstClientt   Falset   get_static_index_paget   Truet   endswitht   isfilet   opent   readt   html_headert   html_trailert   get_static_page_by_patht
   startswitht   splitt   replacet   page_from_wordt   lent   page_from_hrefR   t   wfilet   write(	   R   t   spt   paget   typet   wordt   uspt   partst   _[1]t   p(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   do_GETh   sX    

*K
c         C   s+   |  i  d  |  i d |  |  i   d  S(   Ni   s   Content-type(   t   send_responset   send_headert   end_headers(   R   R2   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR      s    c         G   s9   t  o. t  i d |  i   |  i   | | f  n d  S(   Ns   %s - - [%s] %s
(   t   logfileR/   t   address_stringt   log_date_time_string(   R   t   formatt   args(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   log_message   s    		(   t   __name__t
   __module__R   R8   t   NoneR   RA   (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR
   c   s   		Dc             sL   t  d d  }  |  i   } |  i     f d       t i |   GHd  S(   Ns   favicon.icot   rbc            s>   t  |   d j o |  g Sn |  d d !g   |  d  Sd  S(   NiH   i    (   R,   (   t   s(   R)   (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR)      s    (   R#   R$   t   closet   base64t   urlsafe_b64encode(   t   fRF   (    (   R)   s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   encode_icon   s
    
tH   AAABAAEAEBAAAAAAAABoBQAAFgAAACgAAAAQAAAAIAAAAAEACAAAAAAAAAAAAAAAAAAAAAAAsH   AAAAAAAAAAD___8A9___ANb3_wDO9_8AjPf_ALXv_wCc7_8AjO__AHvv_wBz7_8Aa-__AKXnsH   _wCc5_8AlOf_AITn_wBz5_8Aa-f_AGPn_wBa5_8Ac97_AGve_wBj3v8AWt7_AFLe_wBK3v8AtH   Qt7_AFrW_wBS1v8AStb_AELW_wA51v8AMdb_ACnO_wAhzv8AGM7_ABjG_wD___cA__f3APf3sH   9wB73vcAUtb3AErW9wAhxvcAAMb3AFLO7wAYxu8AEMbvACG95wAYvecA9-fWAHPG1gBKvdYAtH   Ob3WACG91gDv3s4Axt7OACm1zgCMtb0ASq29ACGlvQBStbUAUq21ADGttQA5pbUA3satAEqctH   rQDWvaUAY62lAOfGnADWvZwAtbWcAJStnADGrZQAzq2MAIycjABznIwAa5yMAN61hADWrXsAtH   zq17AMalewCtpXsAa4x7AMaccwC9nHMAtZRzAISUcwBrjHMAzqVrALWUawCtlGsArYxrAHuEtH   awBre2sAY3trAHuEYwBzhGMAc3tjAGt7YwDGlFoAvYxaAGNzWgBSa1oAxpRSAK2MUgDGjEoAtH   vYxKAL2ESgC1hEoArYRKAIRzSgB7a0oAc2tKAGtrSgBaY0oAtYRCAK17QgCle0IApXM5AJxztH   OQCcazkAjGMxAIRaMQBzWjEAa1oxAIRaKQB7ShAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAtH   AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAtH   AAAAAAAAAAAAAAAGFh4YLAYAAAAAAAAAAAAHGB4gIBYWBgAAAAAAAAAAFhgzQR45MixGMQAAtH   AAAAABQYTF0WbzplWQAAAABFVEgKFExdFG59eywWCwBAdHRJCgpLXRhxe3IvIiIDT2p0VAdhtH   fn5xbzplciAwFFNqanQ3BwoKChYYGB4gICxYanRqalRPWVRZRhMYHiAYTmlqdnZ2dnh5eX1GsH   FhgeFEVjaT1SVithKzg7WhMYGAsATmM9UjgwXDt2eFsIFgcAAAAAFDRDLUo-bnhZAAAAAAAAtH   AAgwRS1cO3Z2WgAAAAAAAAADUTZHbVJ0d0kAAAAAAAAAADFPY2pqZEgAAAAAAAAA__8AAP__sH   AAD__wAAsaEAAE5eAABOXgAA__4AAPv_AAD__wAA__8AAM3-AADw_wAA__8AAML-AAD__wAAs   xf4=c           C   s   t  i d i t   S(   s0   
    Return the data for the favicon image.
    R   (   RH   t   urlsafe_b64decodet   joint   FAVICON_BASE64_DATA(    (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR      s    c         C   s:   |  i  d  } | d j o t |  | d  Sn d Sd S(   s\   
    Extract the unique counter from the URL if it has one.  Otherwise return
    null.
    s   %23ii   N(   t   rfindt   intRD   (   R0   t   pos(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   get_unique_counter_from_url   s    i@  c         C   s"  | a  | oU y t | d d  a Wqi t j
 o, } t i i d | |  t i d  qi Xn d a d t	 |   } | o t
 i   } t | |  } n t d |  f t  } t o t i d |  n | o | i   n y | i   Wn t j
 o n X| o | i   n d S(   s4  
    Run NLTK Wordnet Browser Server.
    
    @param port: The port number for the server to listen on, defaults to
                 8000
    @type  port: C{int}

    @param runBrowser: True to start a web browser and point it at the web
                       server.
    @type  runBrowser: C{boolean}
    t   ai   s    Couldn't open %s for writing: %ss   http://localhost:R   s0   NLTK Wordnet browser server running serving: %s
N(   R   R#   R<   t   IOErrort   syst   stderrR/   t   exitRD   t   strt	   threadingt   Eventt   startBrowserR   R
   t   sett   serve_forevert   KeyboardInterruptRZ   (   t   portt
   runBrowsert   logfilenamet   et   urlt   server_readyt   browser_threadt   server(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   wnb   s2    
c            s2      f d   } t  i d |  } | i   | S(   Nc              s4    i    t i d  t i   d d d d d  S(   Ni   t   newi   t	   autoraise(   t   waitt   timet   sleept
   webbrowserR#   (    (   Rp   Rq   (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   run?  s    
t   target(   Rf   t   Threadt   start(   Rp   Rq   R{   t   t(    (   Rp   Rq   s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyRh   >  s    
c           C   s@   t  i d d f t  i d d f t  i d d f t  i d d f g S(	   Nt   Nt   nount   Vt   verbt   Jt   adjt   Rt   adv(   t   wnt   NOUNt   VERBt   ADJt   ADV(    (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   _pos_tuples[  s    c         C   s   |  d d j o d |  d |  d f }  n x, t  |   D] \ } } | t j	 o Pq9 q9 Wx. t   D]# } | | |  | j o | Sqe qe Wt S(   s   
    This function returns the complete pos tuple for the partial pos
    tuple given to it.  It attempts to match it against the first
    non-null component of the given pos tuple.
    i    RF   R`   i   i   (   t	   enumerateRD   R   (   t	   pos_tuplet   nt   xt   pt(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt
   _pos_matchb  s     	
 !i    i   i   i   i   i   i   i   i   i	   i
   i   i   i   i   i   i   i   i   i   i   i   i   i   i   i   i   c            sQ     f d       g  } | i  D]' } | i |  j o | | |  q  q  ~  S(   Nc            s/   |  g  j o g  Sn |  d   |  d  Sd  S(   Ni    i   (    (   t   l(   t   flattern(    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s    (   t   lemmast   name(   R3   t   synsett   funcR6   R   (    (   R   s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   lemma_property  s    c         C   s?   |  d } |  d } | g  } | D] } | t  |  q" ~ f S(   Ni    i   (   t   rebuild_tree(   t	   orig_treet   nodet   childrenR6   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s    

c         C   s  | i  t i j o&t d | i   f t d | i   f t d | i   f t	 d t
 | i d     d f t d | i   f t d | i   f t d	 | i   f t d
 | i   f t d | i   f t d | i   f t d | i   f t d | i   f t d t |  | d    f t d t |  | d    f f Sn| i  t i j o t d t |  | d    f t d | i   f t d | i   f t	 d t
 | i d     d f t  d | i!   f t" d | i#   f t$ d | i%   f t& d | i'   f t d t |  | d    f f	 Sn | i  t i( j p | i  t i) j ot t d t |  | d    f t* d | i+   f t, d t |  | d    f t d | i   f t$ d | i%   f f SnP | i  t i- j o# t d t |  | d     f f Sn t. d! t/ | i     d" S(#   s   
    Get synset relations data for a synset.  Note that this doesn't
    yet support things such as full hyponym vs direct hyponym.
    t   Hyponymss   Instance hyponymss   Direct hypernymss   Indirect hypernymsc         S   s
   |  i    S(    (   t	   hypernyms(   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   <lambda>  s    i   s   Instance hypernymss   Part holonymss   Part meronymss   Substance holonymss   Substance meronymss   Member holonymss   Member meronymst
   Attributest   Antonymsc         S   s
   |  i    S(    (   t   antonyms(   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s    s   Derivationally related formc         S   s
   |  i    S(    (   t   derivationally_related_forms(   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s    t   Antonymc         S   s
   |  i    S(    (   R   (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s    t   Hyponymc         S   s
   |  i    S(    (   R   (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s    t   Entailmentst   Causess   Also sees   Verb Groupsc         S   s
   |  i    S(    (   R   (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s    c         S   s
   |  i    S(    (   R   (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s    s
   Similar tot
   Pertainymsc         S   s
   |  i    S(    (   t
   pertainyms(   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s    c         S   s
   |  i    S(    (   R   (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s    s   Unhandles synset POS type: N(0   R^   R   R   t   HYPONYMt   hyponymst   INSTANCE_HYPONYMt   instance_hyponymst   HYPERNYMR   t   INDIRECT_HYPERNYMSR   t   treet   INSTANCE_HYPERNYMt   instance_hypernymst   PART_HOLONYMt   part_holonymst   PART_MERONYMt   part_meronymst   SUBSTANCE_HOLONYMt   substance_holonymst   SUBSTANCE_MERONYMt   substance_meronymst   MEMBER_HOLONYMt   member_holonymst   MEMBER_MERONYMt   member_meronymst	   ATTRIBUTEt
   attributest   ANTONYMR   t   DERIVATIONALLY_RELATED_FORMR   t
   ENTAILMENTt   entailmentst   CAUSEt   causest   ALSO_SEEt	   also_seest
   VERB_GROUPt   verb_groupsR   t   ADJ_SATt   SIMILARt   similar_tost	   PERTAINYMR   t	   TypeErrorRe   (   R3   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   get_relations_data  s~    &su  
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'
'http://www.w3.org/TR/html4/strict.dtd'>
<html>
<head>
<meta name='generator' content=
'HTML Tidy for Windows (vers 14 February 2006), see www.w3.org'>
<meta http-equiv='Content-Type' content=
'text/html; charset=us-ascii'>
<title>NLTK Wordnet Browser display of: %s</title></head>
<body bgcolor='#F5F5F5' text='#000000'>
s   
</body>
</html>
s  
<h3>Search Help</h3>
<ul><li>The display below the line is an example of the output the browser
shows you when you enter a search word. The search word was <b>green</b>.</li>
<li>The search result shows for different parts of speech the <b>synsets</b>
i.e. different meanings for the word.</li>
<li>All underlined texts are hypertext links. There are two types of links:
word links and others. Clicking a word link carries out a search for the word
in the Wordnet database.</li>
<li>Clicking a link of the other type opens a display section of data attached
to that link. Clicking that link a second time closes the section again.</li>
<li>Clicking <u>S:</u> opens a section showing the relations for that synset.
</li>
<li>Clicking on a relation name opens a section that displays the associated
synsets.</li>
<li>Type a search word in the <b>Word</b> field and start the search by the
<b>Enter/Return</b> key or click the <b>Search</b> button.</li>
</ul>
<hr width='100%'>
c         C   s   d |  S(   Ns	   <b>%s</b>(    (   t   txt(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   _bold  s    c         C   s   d |  S(   Ns   <center>%s</center>(    (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   _center  s    c         C   s   d |  | |  f S(   Ns   <h%d>%s</h%d>(    (   R   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   _hlev  s    c         C   s   d |  S(   Ns	   <i>%s</i>(    (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   _italic  s    c         C   s   d |  S(   Ns   <li>%s</li>(    (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   _li   s    c         C   s   t  |  | t S(   s:  
    Return a HTML page of NLTK Browser format constructed from the
    word and body

    @param word: The word that the body corresponds to
    @type word: str
    @param body: The HTML body corresponding to the word
    @type body: str
    @return: a HTML page for the word-body combination
    @rtype: str
    (   R%   R&   (   R3   t   body(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   pg"  s    c         C   s   d |  d S(   Ns   <ul>s   </ul>(    (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   _ul0  s    c         C   s(   t  t d d d d |  d d   S(   s0   
    abbc = asterisks, breaks, bold, center
    s   <br>i
   t   *R   s
   **********s
   **********(   R   R   (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   _abbc2  s    s   (has full hyponym continuation)s   
c         C   s   t  i |   S(   sc   
    The synset key is the unique name of the synset, this can be
    retrived via synset.name
    (   R   R   (   t
   synset_key(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   _get_synset<  s    c            sw  t  | t  o t d   n d } t | i t t f  } | t j p t d | i  | d } t i t	   |   } | i
 |  | d } | i | i   j o t |  } n d t | |  | f }   f d   }	 | d i g  }
 | i D] } |
 |	 | i  q ~
  7} d	 | i d
 i g  } | i D] } | d | q:~  f } | | t   | |  d S(   s|  
    Returns the HTML string for one synset or word

    @param word: the current word
    @type word: str
    @param synset: a synset
    @type synset: synset
    @param synset_relations: information about which synset relations
    to display.
    @type synset_relations: dict(synset_key, set(relation_id))
    @return: The HTML string built for this synset
    @rtype: str
    s)   word not supported by _collect_one_synsett   Ss!   pos_tuple is null: synset.pos: %si   t   ;s   <li>%s (%s) c            sP   |  i  d d  }  |  i     j o t |   Sn t |   } t | |   Sd  S(   Nt   _R   (   R*   t   lowerR   t	   Referencet   make_lookup_link(   t   wt   ref(   R3   (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   format_lemma^  s
    s   , s    (%s) <i>%s</i> s   ; s   "%s"s   </li>
(   t
   isinstancet   tuplet   NotImplementedErrorR   R^   RD   t   AssertionErrort   copyt   deepcopyR   t   toggle_synsetR   t   keysR   R   RZ   R   t
   definitiont   examplest   _synset_relations(   R3   R   t   synset_relationst   typR   t   descrR   t   synset_labelRF   R   R6   R   t   _[2]Ro   t   gl(    (   R3   s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   _collect_one_synsetC  s$    

:5c            s0   d d i     f d   t i  |  D  S(   s\   
    Return a HTML unordered list of synsets for the given word and
    part of speach.
    s   <ul>%s
</ul>
R   c         3   s%   x |  ] } t   |    Vq Wd  S(   N(   R   (   t   .0R   (   R   R3   (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pys	   <genexpr>s  s   	(   RZ   R   t   synsets(   R3   R^   R   (    (   R   R3   s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   _collect_all_synsetsm  s    c      	      s    i  | i   j o d Sn t |  |     f d         f d    d d i  f d   t |    D  d } | S(   s  
    Builds the HTML string for the relations of a synset

    @param word: The current word
    @type word: str
    @param synset: The synset for which we're building the relations.
    @type synset: Synset
    @param synset_relations: synset keys and relation types for which to display relations.
    @type synset_relations: dict(synset_key, set(relation_type))
    @return: The HTML for a synset's relations
    @rtype: str
    R   c            s   t  |   t j o% t t |  i d  |  i d  Sn t  |   t j o   |  i  Sni t  |   t j o9 d   |  d  d i   f d   |  d D  f Sn t	 d t  |   |  f   d  S(   Ni    s   %s
<ul>%s</ul>
R   c         3   s#   x |  ] } d    |  Vq Wd S(   s   <li>%s</li>
N(    (   R   t   sr(   t   relation_html(    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pys	   <genexpr>  s    i   s?   r must be a synset, lemma or list, it was: type(r) = %s, r = %s(
   R2   R   R   R   t   lemma_namesR	   R   R   RZ   R   (   t   r(   R   (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s    %)c            s   |  \ } } } d t  t i   i  |  i   |  } |  i  i j o+ | d d i   f d   | D  7} n | S(   Ns
   <i>%s</i>
s   <ul>%s</ul>
R   c         3   s#   x |  ] } d    |  Vq Wd S(   s   <li>%s</li>
N(    (   R   R  (   R   (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pys	   <genexpr>  s    (   R   R   R   t   toggle_synset_relationt   encodeR   R   RZ   (   R   t   db_namet	   disp_namet   relst   synset_html(   R   R   R   (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   make_synset_html  s   
%s   <ul>s   
c         3   s8   x1 |  ]* } | d  g  j o d   |  Vq q Wd S(   i   s   <li>%s</li>N(    (   R   R   (   R  (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pys	   <genexpr>  s   s   </ul>(   R   R   R   RZ   R   (   R3   R   R   t   html(    (   R   R   R  R   s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR   w  s    )R   c           B   s8   e  Z d  Z e   d  Z d   Z d   Z d   Z RS(   sB   
    A reference to a page that may be generated by page_word
    c         C   s   | |  _  | |  _ d S(   s9  
        Build a reference to a new page.
        
        word is the word or words (seperated by commas) for which to
        search for synsets of

        synset_relations is a dictionary of synset keys to sets of
        synset relation identifaiers to unfold a list of synset
        relations for.
        N(   R3   R   (   R   R3   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   __init__  s    	c         C   s+   t  i |  i |  i f d  } t i |  S(   sJ   
        Encode this reference into a string to be used in a URL.
        i(   t   cPicklet   dumpsR3   R   RH   RI   (   R   t   string(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR    s    c         C   sM   | |  i  | i j o |  i  | i i |  n |  i  | i i |  |  S(   s   
        Toggle the display of the relations for the given synset and
        relation type.

        This function will throw a KeyError if the synset is currently
        not being displayed.
        (   R   R   t   removet   add(   R   R   t   relation(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR    s    c         C   sA   | i  |  i i   j o |  i | i  =n t   |  i | i  <|  S(   sN   
        Toggle displaying of the relation types for the given synset
        (   R   R   R   Ri   (   R   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s    (   RB   RC   t   __doc__t   dictR
  R  R  R   (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s
   	
	c         C   s1   t  i |   }  t i |   \ } } t | |  S(   s:   
    Decode a reference encoded with Reference.encode
    (   RH   RY   R  t   loadsR   (   R  R3   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   decode_reference  s    c         C   s   d |  i    | f S(   Ns   <a href="lookup_%s">%s</a>(   R  (   R   t   label(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s    c         C   s   t  t |    S(   s-  
    Return a HTML page for the given word.

    @param word: The currently active word
    @type word: str
    @return: A tuple (page,word), where page is the new current HTML page
             to be sent to the browser and
             word is the new current word
    @rtype: A tuple (str,str)
    (   t   page_from_referenceR   (   R3   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR+     s    c         C   s   t  t |    S(   sQ  
    Returns a tuple of the HTML page built and the new current word

    @param href: The hypertext reference to be solved
    @type href: str
    @return: A tuple (page,word), where page is the new current HTML page
             to be sent to the browser and
             word is the new current word
    @rtype: A tuple (str,str)
    (   R  R  (   t   href(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR-     s    c         C   s  |  i  } t t  } | i d  } g  } g  } | D]% } | | i   i   i d d  q6 ~ D] } | d j o | | qb qb ~ } t |  d j o d
 Sn xe t i	 t i
 t i t i g D]E } t i | |  } | o& | | | j o | | i |  q q Wd }	 x t   D] \ } }
 } | | j oc |	 t d |  d 7}	 xI | | D]9 } y |	 t | | |  i  7}	 WqZt j
 o qZXqZWqqW|	 p d	 | }	 n |	 | f S(   sQ  
    Returns a tuple of the HTML page built and the new current word

    @param href: The hypertext reference to be solved
    @type href: str
    @return: A tuple (page,word), where page is the new current HTML page
             to be sent to the browser and
             word is the new current word
    @rtype: A tuple (str,str)
    t   ,R   R   R   i    s$   Please specify a word to search for.i   s   
s8   The word or words '%s' where not found in the dictonary.(   R   s$   Please specify a word to search for.(   R3   R   t   listR)   t   stripR   R*   R,   R   R   R   R   R   t   morphyt   appendR   R   R   R   t   KeyError(   R  R3   t	   pos_formst   wordsR6   R   R   R^   t   formR   t   pos_strR   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR  
  s6    	6! 
  c         C   s   |  d j o t  t  Sn |  d j o t  t  Sn |  d j o d Sno |  d j o t t  SnT |  d j o t t  Sn9 |  d j o t   Sn! |  d j o t   Sn	 d	 |  St |   } | i   } | i   | S(
   s8   
    Return a static HTML page from the path given.
    s   index_2.htmls
   index.htmls'   NLTK Wordnet Browser Database Info.htmls7   Display of Wordnet Database Statistics is not supporteds   upper_2.htmls
   upper.htmls   web_help.htmls   wx_help.htmls4   Internal error: Path for static page '%s' is unknown(	   R   R   R    t   get_static_upper_paget   get_static_web_help_paget   get_static_wx_help_pageR#   R$   RG   (   R    RJ   R1   (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR'   :  s&    
c           C   s   d S(   s*   
    Return the static web help page.
    s  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
     <!-- Natural Language Toolkit: Wordnet Interface: Graphical Wordnet Browser
            Copyright (C) 2001-2009 NLTK Project
            Author: Jussi Salmela <jtsalmela@users.sourceforge.net>
            URL: <http://www.nltk.org/>
            For license information, see LICENSE.TXT -->
     <head>
          <meta http-equiv='Content-Type' content='text/html; charset=us-ascii'>
          <title>NLTK Wordnet Browser display of: * Help *</title>
     </head>
<body bgcolor='#F5F5F5' text='#000000'>
<h2>NLTK Wordnet Browser Help</h2>
<p>The NLTK Wordnet Browser is a tool to use in browsing the Wordnet database. It tries to behave like the Wordnet project's web browser but the difference is that the NLTK Wordnet Browser uses a local Wordnet database.
<p><b>You are using the Javascript client part of the NLTK Wordnet BrowseServer.</b> We assume your browser is in tab sheets enabled mode.</p>
<p>For background information on Wordnet, see the Wordnet project home page: <a href="http://wordnet.princeton.edu/"><b> http://wordnet.princeton.edu/</b></a>. For more information on the NLTK project, see the project home:
<a href="http://nltk.sourceforge.net/"><b>http://nltk.sourceforge.net/</b></a>. To get an idea of what the Wordnet version used by this browser includes choose <b>Show Database Info</b> from the <b>View</b> submenu.</p>
<h3>Word search</h3>
<p>The word to be searched is typed into the <b>New Word</b> field and the search started with Enter or by clicking the <b>Search</b> button. There is no uppercase/lowercase distinction: the search word is transformed to lowercase before the search.</p>
<p>In addition, the word does not have to be in base form. The browser tries to find the possible base form(s) by making certain morphological substitutions. Typing <b>fLIeS</b> as an obscure example gives one <a href="MfLIeS">this</a>. Click the previous link to see what this kind of search looks like and then come back to this page by using the <b>Alt+LeftArrow</b> key combination.</p>
<p>The result of a search is a display of one or more
<b>synsets</b> for every part of speech in which a form of the
search word was found to occur. A synset is a set of words
having the same sense or meaning. Each word in a synset that is
underlined is a hyperlink which can be clicked to trigger an
automatic search for that word.</p>
<p>Every synset has a hyperlink <b>S:</b> at the start of its
display line. Clicking that symbol shows you the name of every
<b>relation</b> that this synset is part of. Every relation name is a hyperlink that opens up a display for that relation. Clicking it another time closes the display again. Clicking another relation name on a line that has an opened relation closes the open relation and opens the clicked relation.</p>
<p>It is also possible to give two or more words or collocations to be searched at the same time separating them with a comma like this <a href="Mcheer up,clear up">cheer up,clear up</a>, for example. Click the previous link to see what this kind of search looks like and then come back to this page by using the <b>Alt+LeftArrow</b> key combination. As you could see the search result includes the synsets found in the same order than the forms were given in the search field.</p>
<p>
There are also word level (lexical) relations recorded in the Wordnet database. Opening this kind of relation displays lines with a hyperlink <b>W:</b> at their beginning. Clicking this link shows more info on the word in question.</p>
<h3>The Buttons</h3>
<p>The <b>Search</b> and <b>Help</b> buttons need no more explanation. </p>
<p>The <b>Show Database Info</b> button shows a collection of Wordnet database statistics.</p>
<p>The <b>Shutdown the Server</b> button is shown for the first client of the BrowServer program i.e. for the client that is automatically launched when the BrowServer is started but not for the succeeding clients in order to protect the server from accidental shutdowns.
</p></body>
</html>
(    (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR#  U  s    ,c           C   s   d S(   s&   
    Get the static welcome page.
    s  
<h3>Search Help</h3>
<ul><li>The display below the line is an example of the output the browser
shows you when you enter a search word. The search word was <b>green</b>.</li>
<li>The search result shows for different parts of speech the <b>synsets</b>
i.e. different meanings for the word.</li>
<li>All underlined texts are hypertext links. There are two types of links:
word links and others. Clicking a word link carries out a search for the word
in the Wordnet database.</li>
<li>Clicking a link of the other type opens a display section of data attached
to that link. Clicking that link a second time closes the section again.</li>
<li>Clicking <u>S:</u> opens a section showing the relations for that synset.</li>
<li>Clicking on a relation name opens a section that displays the associated
synsets.</li>
<li>Type a search word in the <b>Next Word</b> field and start the search by the
<b>Enter/Return</b> key or click the <b>Search</b> button.</li>
</ul>
(    (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   get_static_welcome_message  s    c         C   s%   d } |  o
 d } n d } | | S(   s$   
    Get the static index page.
    sX  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"  "http://www.w3.org/TR/html4/frameset.dtd">
<HTML>
     <!-- Natural Language Toolkit: Wordnet Interface: Graphical Wordnet Browser
            Copyright (C) 2001-2009 NLTK Project
            Author: Jussi Salmela <jtsalmela@users.sourceforge.net>
            URL: <http://www.nltk.org/>
            For license information, see LICENSE.TXT -->
     <HEAD>
         <TITLE>NLTK Wordnet Browser</TITLE>
     </HEAD>

<frameset rows="7%%,93%%">
    <frame src="%s" name="header">
    <frame src="start_page" name="body">
</frameset>
</HTML>
s
   upper.htmls   upper_2.html(    (   t   with_shutdownt   templatet
   upper_link(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR     s
    
c         C   s%   d } |  o
 d } n d } | | S(   s   
    Return the upper frame page,

    If with_shutdown is True then a 'shutdown' button is also provided
    to shutdown the server.
    s  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <!-- Natural Language Toolkit: Wordnet Interface: Graphical Wordnet Browser
        Copyright (C) 2001-2009 NLTK Project
        Author: Jussi Salmela <jtsalmela@users.sourceforge.net>
        URL: <http://www.nltk.org/>
        For license information, see LICENSE.TXT -->
    <head>
                <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Untitled Document</title>
    </head>
    <body>
    <form method="GET" action="search" target="body">
            Current Word:&nbsp;<input type="text" id="currentWord" size="10" disabled>
            Next Word:&nbsp;<input type="text" id="nextWord" name="nextWord" size="10">
            <input name="searchButton" type="submit" value="Search">
    </form>
        <a target="body" href="web_help.html">Help</a>
        %s

</body>
</html>
s*   <a href="SHUTDOWN THE SERVER">Shutdown</a>R   (    (   R&  R'  t   shutdown_link(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyR"    s
    
c           C   s	   t  GHd S(   s0   
    Display the command line help message.
    N(   R  (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   usage  s    c          C   s  t  i  t d d d d d d g  \ }  } d } t } t } d  } x |  D] \ } } | d j p | d	 j o t |  } qJ | d
 j p | d j o t |  } qJ | d j p | d j o
 t } qJ | d j p | d j o
 t } qJ qJ W| o t   n t | | |  d  S(   Ni   s   l:p:shs   logfile=s   port=s   server-modet   helpi@  s   -ls	   --logfiles   -ps   --ports   -ss   --server-modes   -hs   --help(	   t   getoptR   R   RD   Re   R]   R    R*  Rt   (   t   optsR   Rl   R   t	   help_modeRn   t   optt   value(    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pyt   app  s&     
t   __main__R1  (f   R  Rb   R    R   R   t   BaseHTTPServerR   R   t   urllibR   R   Rz   t   datetimet   reRf   Rx   R,  RH   R  R   t   nltk.compatR   t   nltk.corpusR   R   t   nltk.corpus.reader.wordnetR   R	   R    R   RD   R   R<   R
   RK   R[   R   R_   Rt   Rh   R   R   R   R   t   CLASS_REGIONALR   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   FRAMESR   t   CLASS_CATEGORYt   CLASS_USAGER   R   R   R   R   R%   R&   t   explanationR   R   R   R   R   R   R   R   t   full_hyponym_cont_textR   R   R  R   R   t   objectR   R  R   R+   R-   R  R'   R#  R%  R   R"  R*  R1  RB   t   __all__(    (    (    s*   /p/zhu/06/nlp/nltk/nltk/app/wordnet_app.pys   <module>   s   -	Z				B						X										*
	58					0		/			)		