
B_Kc           @   s  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 l	 Z	 d  d k
 l
 Z
 d  d k l Z d  d k l Z l Z d d  Z d   Z d	 d d
  Z d d  Z d d d  Z d e f d     YZ d d d  Z d   Z e d  d d  Z d   Z d   Z d   Z d   Z e e d d  Z  d   Z! d   Z" e e d d  Z# d   Z$ d   Z% d  e& f d!     YZ' d" e( f d#     YZ) d$ e) f d%     YZ* d& e) f d'     YZ+ d( e) f d)     YZ, d* e e, f d+     YZ- d, e, f d-     YZ. d. e. f d/     YZ/ d* e e, f d0     YZ- d1 e e+ f d2     YZ0 h  d  d3  Z1 d S(4   iN(   t   islicet   chain(   t   pprint(   t   defaultdict(   t
   Deprecatedt   slice_boundst   selfc   
      C   sm  d d  k  } t |   t |  t i t i f  p |  i }  n d |  i GHxt t	 i
 |   i    D] \ } } | i d  o qg n t | d t  o qg n | i |  \ } } } } | oU | d d j oD | d  j p t |  t |  j o | d } d | | f } n | i | | | |  }	 t i d	 | |	 f d
 d d d t |  d GHqg Wd  S(   Nis%   %s supports the following operations:t   _t   __deprecated__i    R   i   s   %s.%ss   %s%st   initial_indents     - t   subsequent_indentt    i   (   t   inspectt   strt
   isinstancet   typest   TypeTypet	   ClassTypet	   __class__t   __name__t   sortedt   pydoct
   allmethodst   itemst
   startswitht   getattrt   Falset
   getargspect   Nonet   lent   formatargspect   textwrapt   fill(
   t   objt   selfnameR   t   namet   methodt   argst   varargst   varkwt   defaultst   argspec(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   usage   s&    
 &
c          C   sD   d d k  }  d d k } t |  i  | i j o |  i i i d j S(   s|  
    @rtype: C{boolean}
    @return: true if this function is run within idle.  Tkinter
    programs that are run in idle should never call C{Tk.mainloop}; so
    this function should be used to gate all calls to C{Tk.mainloop}.

    @warning: This function works by checking C{sys.stdin}.  If the
    user has modified C{sys.stdin}, then it may return incorrect
    results.
    iNt   PyShell(   t   sysR   t   typet   stdint   InstanceTypeR   R   (   R,   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   in_idle5   s    i    c         C   s    t  t t |  | |    d S(   s   
    Pretty print a sequence of data items

    @param data: the data stream to print
    @type data: C{sequence} or C{iterator}
    @param start: the start position
    @type start: C{int}
    @param end: the end position
    @type end: C{int}
    N(   R   t   listR    (   t   datat   startt   end(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   prH   s    iF   c         C   s!   d i  t i |  d |  GHd S(   s   
    Pretty print a string, breaking lines on whitespace

    @param s: the string to print, consisting of words and spaces
    @type s: C{string}
    @param width: the display width
    @type width: C{int}
    s   
t   widthN(   t   joinR   t   wrap(   t   sR6   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   print_stringU   s    	R   c         C   s%   d i  t i | i  |   d |  S(   s,  
    Pretty print a list of text tokens, breaking lines on whitespace

    @param tokens: the tokens to print
    @type tokens: C{list}
    @param separator: the string to use to separate tokens
    @type separator: C{str}
    @param width: the display width (default=70)
    @type width: C{int}
    s   
R6   (   R7   R   R8   (   t   tokenst	   separatorR6   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt	   tokenwrap`   s    t   Indexc           B   s   e  Z d    Z RS(   c         C   s<   t  i |  t  x% | D] \ } } |  | i |  q Wd  S(   N(   R   t   __init__R1   t   append(   R   t   pairst   keyt   value(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR?   t   s     (   R   t
   __module__R?   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR>   r   s   t   {t   }c         C   s2   t  i |  t  i  i | d | | i    GHd S(   s\  
    Search C{string} for substrings matching C{regexp} and wrap
    the matches with braces.  This is convenient for learning about
    regular expressions.

    @param regexp: The regular expression.
    @type regexp: C{string}
    @param string: The string being matched.
    @type string: C{string}
    @param left: The left delimiter (printed before the matched substring)
    @type left: C{string}
    @param right: The right delimiter (printed after the matched substring)
    @type right: C{string}
    @rtype: C{string}
    @return: A string with markers surrounding the matched substrings.
    s   \g<0>N(   t   ret   compilet   Mt   subt   rstrip(   t   regexpt   stringt   leftt   right(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   re_show~   s    c         C   sO   t  |  d  o |  i   Sn. t |  t  o t |   i   Sn
 t d  d  S(   Nt   reads2   Must be called with a filename or file-like object(   t   hasattrRQ   R   t
   basestringt   opent
   ValueError(   t   f(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt
   filestring   s
    c         c   s   | d j o
 g  } n | i |   xY | oQ | i d  } | V| d j o, y | | |  7} | d 8} Wq{ q{ Xq' q' Wd S(   s  Traverse the nodes of a tree in breadth-first order.
    (No need to check for cycles.)
    The first argument should be the tree root;
    children should be a function taking as argument a tree node
    and returning an iterator of the node's children.
    i    i   N(   R   R@   t   pop(   t   treet   childrent   deptht   queuet   node(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   breadth_first   s    

c         C   sp  d } d g } y | i t i t i   Wn t j
 o n Xy | i t i   d  Wn t t f j
 o n Xy | i t i   d  Wn t t f j
 o n X| i d  xR | D]J } | p q n y t	 |  |  } | } Wn t
 t f j
 o q XPq W| pI t
 d d i g  } | D] } | o | t |  q0q0~    n | | f Sd S(   sr  
    Given a byte string, attempt to decode it.
    Tries the standard 'UTF8' and 'latin-1' encodings,
    Plus several gathered from locale information.

    The calling program *must* first call::

        locale.setlocale(locale.LC_ALL, '')

    If successful it returns C{(decoded_unicode, successful_encoding)}.
    If unsuccessful it raises a C{UnicodeError}.
    s   utf-8i   s   latin-1s@   Unable to decode input data.  Tried the following encodings: %s.s   , N(   R   R@   t   localet   nl_langinfot   CODESETt   AttributeErrort	   getlocalet
   IndexErrort   getdefaultlocalet   unicodet   UnicodeErrort   LookupErrorR7   t   repr(   R2   t   successful_encodingt	   encodingst   enct   decodedt   _[1](    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   guess_encoding   s<    	 
Cc         C   s}   d d k  l } | t  } xZ |  D]R } t |  | d  o* x5 |  | D] } | | i |  qH Wq# | | |  | <q# W| S(   Ni(   R   t   __iter__(   t   nltk.compatR   R1   RR   R@   (   t   dR   t   inverted_dictRB   t   term(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   invert_dict   s      c         C   s   t  i d d |  i    } t  i d d |  } t  i d d |  } t  i d d |  } t  i d d |  } t  i d d |  } | i   S(   s   
    Remove HTML markup from the given string.

    @param html: the HTML string to be cleaned
    @type html: C{string}
    @rtype: C{string}
    s"   (?is)<(script|style).*?>.*?(</\1>)t    s   (?s)<!--(.*?)-->[\n]?s	   (?s)<.*?>R   s   &nbsp;s     (   RG   RJ   t   strip(   t   htmlt   cleaned(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt
   clean_html	  s    
c         C   s,   d d k  l } | |   i   } t |  S(   Ni(   t   urlopen(   t   urllibR{   RQ   Rz   (   t   urlR{   Rx   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt	   clean_url  s    c         C   s   | o t  | f | d |   }  n | o t  |  | f | d  }  n t |   }  t d t |   | d  } g  } t |  D] } | t |  | | | ! q ~ S(   s  
    A utility that produces a sequence of ngrams from a sequence of items.
    For example:
    
    >>> ngrams([1,2,3,4,5], 3)
    [(1, 2, 3), (2, 3, 4), (3, 4, 5)]
    
    Use ingram for an iterator version of this function.  Set pad_left
    or pad_right to true in order to get additional ngrams:
    
    >>> ngrams([1,2,3,4,5], 2, pad_right=True)
    [(1, 2), (2, 3), (3, 4), (4, 5), (5, None)]

    @param sequence: the source data to be converted into ngrams
    @type sequence: C{sequence} or C{iterator}
    @param n: the degree of the ngrams
    @type n: C{int}
    @param pad_left: whether the ngrams should be left-padded
    @type pad_left: C{boolean}
    @param pad_right: whether the ngrams should be right-padded
    @type pad_right: C{boolean}
    @param pad_symbol: the symbol to use for padding (default is None)
    @type pad_symbol: C{any}
    @return: The ngrams
    @rtype: C{list} of C{tuple}s
    i   i    (   R   R1   t   maxR   t   ranget   tuple(   t   sequencet   nt   pad_leftt	   pad_rightt
   pad_symbolt   countRn   t   i(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   ngrams*  s    c         K   s   t  |  d |  S(   s  
    A utility that produces a sequence of bigrams from a sequence of items.
    For example:
    
    >>> bigrams([1,2,3,4,5])
    [(1, 2), (2, 3), (3, 4), (4, 5)]
    
    Use ibigrams for an iterator version of this function.

    @param sequence: the source data to be converted into bigrams
    @type sequence: C{sequence} or C{iterator}
    @return: The bigrams
    @rtype: C{list} of C{tuple}s
    i   (   R   (   R   t   kwargs(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   bigramsO  s    c         K   s   t  |  d |  S(   s  
    A utility that produces a sequence of trigrams from a sequence of items.
    For example:
    
    >>> trigrams([1,2,3,4,5])
    [(1, 2, 3), (2, 3, 4), (3, 4, 5)]
    
    Use itrigrams for an iterator version of this function.

    @param sequence: the source data to be converted into trigrams
    @type sequence: C{sequence} or C{iterator}
    @return: The trigrams
    @rtype: C{list} of C{tuple}s
    i   (   R   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   trigrams`  s    c         c   s   t  |   }  | o t | f | d |   }  n | o t |  | f | d  }  n g  } x/ | d j o! | i |  i    | d 8} q_ Wx- |  D]% } | i |  t |  V| d =q Wd S(   s  
    A utility that produces an iterator over ngrams generated from a sequence of items.
    
    For example:
    
    >>> list(ingrams([1,2,3,4,5], 3))
    [(1, 2, 3), (2, 3, 4), (3, 4, 5)]
    
    Use ngrams for a list version of this function.  Set pad_left
    or pad_right to true in order to get additional ngrams:
    
    >>> list(ingrams([1,2,3,4,5], 2, pad_right=True))
    [(1, 2), (2, 3), (3, 4), (4, 5), (5, None)]

    @param sequence: the source data to be converted into ngrams
    @type sequence: C{sequence} or C{iterator}
    @param n: the degree of the ngrams
    @type n: C{int}
    @param pad_left: whether the ngrams should be left-padded
    @type pad_left: C{boolean}
    @param pad_right: whether the ngrams should be right-padded
    @type pad_right: C{boolean}
    @param pad_symbol: the symbol to use for padding (default is None)
    @type pad_symbol: C{any}
    @return: The ngrams
    @rtype: C{iterator} of C{tuple}s
    i   i    N(   t   iterR   R@   t   nextR   (   R   R   R   R   R   t   historyt   item(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   ingramsq  s     c         k   s&   x t  |  d |  D] } | Vq Wd S(   s  
    A utility that produces an iterator over bigrams generated from a sequence of items.
    
    For example:
    
    >>> list(ibigrams([1,2,3,4,5]))
    [(1, 2), (2, 3), (3, 4), (4, 5)]
    
    Use bigrams for a list version of this function.

    @param sequence: the source data to be converted into bigrams
    @type sequence: C{sequence} or C{iterator}
    @return: The bigrams
    @rtype: C{iterator} of C{tuple}s
    i   N(   R   (   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   ibigrams  s     c         k   s&   x t  |  d |  D] } | Vq Wd S(   s  
    A utility that produces an iterator over trigrams generated from a sequence of items.
    
    For example:
    
    >>> list(itrigrams([1,2,3,4,5])
    [(1, 2, 3), (2, 3, 4), (3, 4, 5)]
    
    Use trigrams for a list version of this function.

    @param sequence: the source data to be converted into trigrams
    @type sequence: C{sequence} or C{iterator}
    @return: The trigrams
    @rtype: C{iterator} of C{tuple}s
    i   N(   R   (   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt	   itrigrams  s     t   OrderedDictc           B   s   e  Z e d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z e e d	  Z d
   Z e d  Z d   Z d   Z RS(   c         K   sb   |  i  | | i d   |  _ | i d  |  _ | d  j o t i |   n t i |  |  d  S(   Nt   keyst   default_factory(   R   t   gett   _keyst   _default_factoryR   t   dictR?   (   R   R2   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR?     s
    c         C   s$   t  i |  |  |  i i |  d  S(   N(   R   t   __delitem__R   t   remove(   R   RB   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s    c         C   s;   y t  i |  |  SWn  t j
 o |  i |  Sn Xd  S(   N(   R   t   __getitem__t   KeyErrort   __missing__(   R   RB   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s    c         C   s   d   |  i    D S(   Nc         s   s   x |  ] } | Vq Wd  S(   N(    (   t   .0RB   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pys	   <genexpr>  s    (   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyRp     s    c         C   s6   |  i  o | |  i j o t    n |  i    Sd  S(   N(   R   R   R   (   R   RB   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s    c         C   s;   t  i |  | |  | |  i j o |  i i |  n d  S(   N(   R   t   __setitem__R   R@   (   R   RB   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s    c         C   s   t  i |   |  i i   d  S(   N(   R   t   clearR   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s    c         C   s   t  i |   } |  i | _ | S(   N(   R   t   copyR   (   R   Rr   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s    c         C   s   t  |  i   |  i    S(   N(   t   zipR   t   values(   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s    c         C   s  | o | o? t  | t  p t  t |  t |  j p t  | Sqt  | t  p' t  | t  p t  | t  p t  t  | t  p t  | t  o | i   Sqt  | t  o) g  } | D] \ } } | | q ~ Sqn  d |  i j o |  i Sn g  Sd  S(   NR   (	   R   R1   t   AssertionErrorR   R   R   R   t   __dict__R   (   R   R2   R   Rn   RB   RC   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s      -c         C   sE   |  i  o. |  i  i   } |  | } |  | =| | f Sn
 t    d  S(   N(   R   RX   R   (   R   RB   RC   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   popitem	  s    

c         C   s;   t  i |  | |  | |  i j o |  i i |  n d  S(   N(   R   t
   setdefaultR   R@   (   R   RB   t   failobj(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s    c         C   sR   t  i |  |  x; |  i |  D]* } | |  i j o |  i i |  q  q  Wd  S(   N(   R   t   updateR   R   R@   (   R   R2   RB   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s
     c         C   s   t  |  i |  i  S(   N(   t   mapR   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s    (   R   RD   R   R?   R   R   Rp   R   R   R   R   R   R   R   R   R   R   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s   											t   AbstractLazySequencec           B   s   e  Z d  Z d   Z d   Z d   Z d   Z d   Z e e d  Z	 d   Z
 d   Z d	   Z d
   Z d   Z d Z d   Z d   Z d   Z RS(   sY  
    An abstract base class for read-only sequences whose values are
    computed as needed.  Lazy sequences act like tuples -- they can be
    indexed, sliced, and iterated over; but they may not be modified.

    The most common application of lazy sequences in NLTK is for
    I{corpus view} objects, which provide access to the contents of a
    corpus without loading the entire corpus into memory, by loading
    pieces of the corpus from disk as needed.
    
    The result of modifying a mutable element of a lazy sequence is
    undefined.  In particular, the modifications made to the element
    may or may not persist, depending on whether and when the lazy
    sequence caches that element's value or reconstructs it from
    scratch.

    Subclasses are required to define two methods:
    
      - L{__len__()}
      - L{iterate_from()}.
    c         C   s   t  d   d S(   se   
        Return the number of tokens in the corpus file underlying this
        corpus view.
        s!   should be implemented by subclassN(   t   NotImplementedError(   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   __len__:  s    c         C   s   t  d   d S(   s   
        Return an iterator that generates the tokens in the corpus
        file underlying this corpus view, starting at the token number
        C{start}.  If C{start>=len(self)}, then this iterator will
        generate no tokens.
        s!   should be implemented by subclassN(   R   (   R   R3   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   iterate_fromA  s    c         C   s   t  | t  o) t |  |  \ } } t |  | |  Snx | d j  o | t |   7} n | d j  o t d   n y |  i |  i   SWn t j
 o t d   n Xd S(   s   
        Return the C{i}th token in the corpus file underlying this
        corpus view.  Negative indices and spans are both supported.
        i    s   index out of rangeN(	   R   t   sliceR   t   LazySubsequenceR   Rd   R   R   t   StopIteration(   R   R   R3   t   stop(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR   J  s    !c         C   s   |  i  d  S(   sd   Return an iterator that generates the tokens in the corpus
        file underlying this corpus view.i    (   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyRp   \  s    c            s   t    f d   |  D  S(   s7   Return the number of times this list contains C{value}.c         3   s*   x# |  ] } |   j o	 d  Vq q Wd S(   i   N(    (   R   t   elt(   RC   (    s   /p/zhu/06/nlp/nltk/nltk/util.pys	   <genexpr>c  s    (   t   sum(   R   RC   (    (   RC   s   /p/zhu/06/nlp/nltk/nltk/util.pyR   a  s    c         C   sp   t  |  t | |   \ } } x? t t |  | |   D]% \ } } | | j o | | Sq7 q7 Wt d   d S(   s  Return the index of the first occurance of C{value} in this
        list that is greater than or equal to C{start} and less than
        C{stop}.  Negative start & stop values are treated like negative
        slice bounds -- i.e., they count from the end of the list.s   index(x): x not in listN(   R   R   t	   enumerateR    RU   (   R   RC   R3   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   indexe  s
     c         C   s   t  |  i |   S(   s+   Return true if this list contains C{value}.(   t   boolR   (   R   RC   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   __contains__o  s    c         C   s   t  |  | g  S(   s,   Return a list concatenating self with other.(   t   LazyConcatenation(   R   t   other(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   __add__s  s    c         C   s   t  | |  g  S(   s,   Return a list concatenating other with self.(   R   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   __radd__w  s    c         C   s   t  |  g |  S(   s<   Return a list concatenating self with itself C{count} times.(   R   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   __mul__{  s    c         C   s   t  |  g |  S(   s<   Return a list concatenating self with itself C{count} times.(   R   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   __rmul__  s    i<   c         C   s   g  } d } x |  D]m } | i  t |   | t | d  d 7} | |  i j o, t |  d j o d d i | d   Sq q Wd d i |  Sd S(   s   
        @return: A string representation for this corpus view that is
        similar to a list's representation; but if it would be more
        than 60 characters long, it is truncated.
        i   ii   s	   [%s, ...]s   , s   [%s]N(   R@   Ri   R   t   _MAX_REPR_SIZER7   (   R   t   piecest   lengthR   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   __repr__  s     #c         C   s7   t  | t t f  p d Sn t t |   t |   S(   s  
        Return a number indicating how C{self} relates to other.

          - If C{other} is not a corpus view or a C{list}, return -1.
          - Otherwise, return C{cmp(list(self), list(other))}.

        Note: corpus views do not compare equal to tuples containing
        equal elements.  Otherwise, transitivity would be violated,
        since tuples do not compare equal to lists.
        i(   R   R   R1   t   cmp(   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   __cmp__  s    c         C   s   t  d |  i i   d S(   sH   
        @raise ValueError: Corpus view objects are unhashable.
        s   %s objects are unhashableN(   RU   R   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   __hash__  s    (   R   RD   t   __doc__R   R   R   Rp   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR   $  s    						
							R   c           B   s8   e  Z d  Z d Z d   Z d   Z d   Z d   Z RS(   s   
    A subsequence produced by slicing a lazy sequence.  This slice
    keeps a reference to its source sequence, and generates its values
    by looking them up in the source sequence.
    id   c         C   sR   | | |  i  j  o$ t t | i |  | |   Sn t i |  | | |  Sd S(   s  
        Construct a new slice from a given underlying sequence.  The
        C{start} and C{stop} indices should be absolute indices --
        i.e., they should not be negative (for indexing from the back
        of a list) or greater than the length of C{source}.
        N(   t   MIN_SIZER1   R    R   t   objectt   __new__(   t   clst   sourceR3   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s    $c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   _sourcet   _startt   _stop(   R   R   R3   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR?     s    		c         C   s   |  i  |  i S(   N(   R   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s    c         C   s3   t  |  i i | |  i  t d t |   |   S(   Ni    (   R    R   R   R   R   R   (   R   R3   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s    (   R   RD   R   R   R   R?   R   R   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s   			R   c           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s$  
    A lazy sequence formed by concatenating a list of lists.  This
    underlying list of lists may itself be lazy.  C{LazyConcatenation}
    maintains an index that it uses to keep track of the relationship
    between offsets in the concatenated lists and offsets in the
    sublists.
    c         C   s   | |  _  d g |  _ d  S(   Ni    (   t   _listt   _offsets(   R   t   list_of_lists(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR?     s    	c         C   sO   t  |  i  t  |  i  j o% x" |  i |  i d  D] } q6 Wn |  i d S(   Ni(   R   R   R   R   (   R   t   tok(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s     c         c   sq  | |  i  d j  o t i |  i  |  d } n t |  i   d } |  i  | } t |  i t  o |  i i |  } n t |  i | d   } x | D] } | t |  i   d j oF | t |  |  i  d j p
 t
 d  |  i  i | t |   n- |  i  | d | t |  j p
 t
 d  x$ | t d | |  D] } | Vq@W| t |  7} | d 7} q Wd  S(   Nii   s!   offests not monotonic increasing!s"   inconsistent list value (num elts)i    (   R   t   bisectt   bisect_rightR   R   R   R   R   R    R   R   R@   R   (   R   t   start_indext   sublist_indexR   t   sublist_itert   sublistRC   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s(     !% 	(   R   RD   R   R?   R   R   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s   		t   LazyMapc           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s  
    A lazy sequence whose elements are formed by applying a given
    function to each element in one or more underlying lists.  The
    function is applied lazily -- i.e., when you read a value from the
    list, C{LazyMap} will calculate that value by applying its
    function to the underlying lists' value(s).  C{LazyMap} is
    essentially a lazy version of the Python primitive function
    C{map}.  In particular, the following two expressions are
    equivalent:

        >>> map(f, sequences...)
        >>> list(LazyMap(f, sequences...))

    Like the Python C{map} primitive, if the source lists do not have
    equal size, then the value C{None} will be supplied for the
    'missing' elements.
    
    Lazy maps can be useful for conserving memory, in cases where
    individual values take up a lot of space.  This is especially true
    if the underlying list's values are constructed lazily, as is the
    case with many corpus readers.

    A typical example of a use case for this class is performing
    feature detection on the tokens in a corpus.  Since featuresets
    are encoded as dictionaries, which can take up a lot of memory,
    using a C{LazyMap} can significantly reduce memory usage when
    training and running classifiers.
    c         O   s   | p t  d   n | |  _ | |  _ | i d d  |  _ |  i d j o h  |  _ n
 d |  _ t d   | D  t |  j |  _	 d S(   sJ  
        @param function: The function that should be applied to
            elements of C{lists}.  It should take as many arguments
            as there are C{lists}.
        @param lists: The underlying lists.
        @kwparam cache_size: Determines the size of the cache used
            by this lazy map.  (default=5)
        s"   LazyMap requires at least two argst
   cache_sizei   i    c         s   s"   x |  ] } t  | t  Vq Wd  S(   N(   R   R   (   R   t   lst(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pys	   <genexpr>4  s   	N(
   t	   TypeErrort   _listst   _funcR   t   _cache_sizet   _cacheR   R   R   t	   _all_lazy(   R   t   functiont   listst   config(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR?     s    					c   
      c   s}  t  |  i  d j oA |  i o7 x, |  i d i |  D] } |  i |  Vq7 Wd  Sn#t  |  i  d j oT x	t oE y |  i |  i d |  VWn t j
 o d  Sn X| d 7} qp Wn|  i o g  } |  i D] } | | i |  q ~ } x~t o g  } x< | D]4 } y | i | i    Wq| i d   qXqW| d  g t  |  i  j o d  Sn |  i |   V| d 7} q Wn x t o y, g  } |  i D] } | | | q~ } Wn t j
 o d  g t  |  i  } xE t
 |  i  D]4 \ }	 } y | | | |	 <Wqt j
 o qXqW| d  g t  |  i  j o d  Sq\n X|  i |   V| d 7} qWd  S(   Ni   i    (   R   R   R   R   R   t   TrueRd   R@   R   R   R   (
   R   R   RC   Rn   R   t	   iteratorst   elementst   iteratort   _[2]R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR   7  sD      
 
-
 
/ c         C   sG  t  | t  o< g  } |  i D] } | | | q ~ } t |  i |  Sn | d j  o | t |   7} n | d j  o t d   n |  i d  j	 o | |  i j o |  i | Sn y |  i	 |  i
   } Wn t j
 o t d   n X|  i d  j	 o; t |  i  |  i j o |  i i   n | |  i | <n | Sd  S(   Ni    s   index out of range(   R   R   R   R   R   R   Rd   R   R   R   R   R   R   R   (   R   R   Rn   R   t   sliced_listst   val(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR   `  s    (! c         C   s   t  d   |  i D  S(   Nc         s   s   x |  ] } t  |  Vq Wd  S(   N(   R   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pys	   <genexpr>x  s    (   R   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR   w  s    (   R   RD   R   R?   R   R   R   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR      s
   		)	t   LazyMappedListc           B   s   e  Z d  Z d   Z RS(   s   Use LazyMap instead.c         C   s   t  i |  | |  d  S(   N(   R   R?   (   R   R   t   func(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR?   }  s    (   R   RD   R   R?   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR   {  s   t   LazyZipc           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   sf  
    A lazy sequence whose elements are tuples, each containing the i-th 
    element from each of the argument sequences.  The returned list is 
    truncated in length to the length of the shortest argument sequence. The
    tuples are constructed lazily -- i.e., when you read a value from the
    list, C{LazyZip} will calculate that value by forming a C{tuple} from
    the i-th element of each of the argument sequences.
    
    C{LazyZip} is essentially a lazy version of the Python primitive function
    C{zip}.  In particular, the following two expressions are equivalent:

        >>> zip(sequences...)
        >>> list(LazyZip(sequences...))
            
    Lazy zips can be useful for conserving memory in cases where the argument
    sequences are particularly long.
    
    A typical example of a use case for this class is combining long sequences
    of gold standard and predicted values in a classification or tagging task
    in order to calculate accuracy.  By constructing tuples lazily and 
    avoiding the creation of an additional long sequence, memory usage can be
    significantly reduced.
    c         G   s   t  i |  d   |  d S(   s\   
        @param lists: the underlying lists
        @type lists: C{list} of C{list}
        c          W   s   |  S(    (    (   t   elts(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   <lambda>  s    N(   R   R?   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR?     s    c         c   sF   t  i |  |  } x- | t |   j  o | i   V| d 7} q Wd  S(   Ni   (   R   R   R   R   (   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s
    c         C   s   t  d   |  i D  S(   Nc         s   s   x |  ] } t  |  Vq Wd  S(   N(   R   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pys	   <genexpr>  s    (   t   minR   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s    (   R   RD   R   R?   R   R   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s   		t   LazyEnumeratec           B   s   e  Z d  Z d   Z RS(   s<  
    A lazy sequence whose elements are tuples, each ontaining a count (from
    zero) and a value yielded by underlying sequence.  C{LazyEnumerate} is
    useful for obtaining an indexed list. The tuples are constructed lazily
    -- i.e., when you read a value from the list, C{LazyEnumerate} will
    calculate that value by forming a C{tuple} from the count of the i-th
    element and the i-th element of the underlying sequence.
    
    C{LazyEnumerate} is essentially a lazy version of the Python primitive
    function C{enumerate}.  In particular, the following two expressions are
    equivalent:

        >>> enumerate(sequence)
        >>> list(LazyEnumerate(sequence))
            
    Lazy enumerations can be useful for conserving memory in cases where the
    argument sequences are particularly long.
    
    A typical example of a use case for this class is obtaining an indexed
    list for a long sequence of values.  By constructing tuples lazily and 
    avoiding the creation of an additional long sequence, memory usage can be
    significantly reduced.
    c         C   s#   t  i |  t t |   |  d S(   sL   
        @param lst: the underlying list
        @type lst: C{list}
        N(   R   R?   t   xrangeR   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR?     s    (   R   RD   R   R?   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s   c           B   s   e  Z d  Z d   Z RS(   s   Use LazyMap instead.c         C   s   t  i |  | |  d  S(   N(   R   R?   (   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR?     s    (   R   RD   R   R?   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s   t   LazyMappedChainc           B   s   e  Z d  Z d   Z RS(   s4   Use LazyConcatenation(LazyMap(func, lists)) instead.c         C   s   t  i |  t | |   d  S(   N(   R   R?   R   (   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR?     s    (   R   RD   R   R?   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyR     s   c         C   sd  | d } t  |  } d } d } t |  d  o t i |  i  i d } n. |  i d d  |  i   d } |  i d  x| | j  o| | f } | | d }	 | i |	  o | |	 \ }
 } n d } x t	 o |  i t
 d |	 d   |	 d j o |  i   n |  i   }
 |  i   } | d j o Pn | |	 d }	 |	 | d j o t Sq q W| | j  o |
 | f | |	 <n |
 | j o) | |	 d j p
 t d  |	 d } nu | |  | j o | Sn\ | | j o) | |	 d j p
 t d  |	 d } n& | | j  o |
 t  |  d } n | d 7} | | f } | | j o t Sq q Wt S(   s  
    Searches through a sorted file using the binary search algorithm.

    @type  file: file
    @param file: the file to be searched through.
    @type  key: {string}
    @param key: the identifier we are searching for.
    @return: The line from the file with first word key.
    R   i    R#   i   i   Rv   s   infinite loop(   R   RR   t   ost   statR#   t   st_sizet   seekt   tellR   R   R   t   readlineR   R   (   t   fileRB   t   cachet
   cacheDeptht   keylenR3   t   currentDepthR4   t	   lastStatet   middlet   offsett   linet	   thisState(    (    s   /p/zhu/06/nlp/nltk/nltk/util.pyt   binary_search_file  sT    


(2   R_   RG   R   R   R   R   R   t	   itertoolsR    R   R   Rq   R   t   nltk.internalsR   R   R*   R0   R   R5   R:   R=   R>   RP   RW   R   R^   Ro   Ru   Rz   R~   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R  (    (    (    s   /p/zhu/06/nlp/nltk/nltk/util.pys   <module>   sP   			:			%		,		['/{*!
