³ò
4ÒÇIc        	   @   sª   d  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	 l
 Z
 d d k Td e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d S(   s   
API for corpus readers.
iÿÿÿÿN(   t   defaultdict(   t
   deprecated(   t   PathPointert   FileSystemPathPointert   ZipFilePathPointer(   t   *t   CorpusReaderc           B   s³   e  Z d  Z e e d „ Z d „  Z d „  Z d „  Z d „  Z e e	 d „ Z
 d „  Z d „  Z d	 „  Z e e d
 d ƒZ e d ƒ d „  ƒ Z e d ƒ d „  ƒ Z e e ƒ Z RS(   sª  
    A base class for X{corpus reader} classes, each of which can be
    used to read a specific corpus format.  Each individual corpus
    reader instance is used to read a specific corpus, consisting of
    one or more files under a common root directory.  Each file is
    identified by its C{file identifier}, which is the relative path
    to the file from the root directory.

    A separate subclass is be defined for each corpus format.  These
    subclasses define one or more methods that provide 'views' on the
    corpus contents, such as C{words()} (for a list of words) and
    C{parsed_sents()} (for a list of parsed sentences).  Called with
    no arguments, these methods will return the contents of the entire
    corpus.  For most corpora, these methods define one or more
    selection arguments, such as C{fileids} or C{categories}, which can
    be used to select which portion of the corpus should be returned.
    c         C   s<  t  | t ƒ oN t i d | ƒ } | i ƒ  \ } } | o t | | ƒ } q~ t | ƒ } n! t  | t ƒ p t d ƒ ‚ n t  | t ƒ o t	 | | ƒ } n | |  _
 | |  _ t  | t ƒ oc h  } xP |  i
 D]E }	 x< | D]4 }
 |
 \ } } t i | |	 ƒ o | | |	 <Pqà qà WqÓ W| } n | |  _ | |  _ d S(   sy  
        @type root: L{PathPointer} or C{str}
        @param root: A path pointer identifying the root directory for
            this corpus.  If a string is specified, then it will be
            converted to a L{PathPointer} automatically.
        @param fileids: A list of the files that make up this corpus.
            This list can either be specified explicitly, as a list of
            strings; or implicitly, as a regular expression over file
            paths.  The absolute path for each file will be constructed
            by joining the reader's root to each file name.
        @param encoding: The default unicode encoding for the files
            that make up the corpus.  C{encoding}'s value can be any
            of the following:
            
              - B{A string}: C{encoding} is the encoding name for all
                files.
              - B{A dictionary}: C{encoding[file_id]} is the encoding
                name for the file whose identifier is C{file_id}.  If
                C{file_id} is not in C{encoding}, then the file
                contents will be processed using non-unicode byte
                strings.
              - B{A list}: C{encoding} should be a list of C{(regexp,
                encoding)} tuples.  The encoding for a file whose
                identifier is C{file_id} will be the C{encoding} value
                for the first tuple whose C{regexp} matches the
                C{file_id}.  If no tuple's C{regexp} matches the
                C{file_id}, the file contents will be processed using
                non-unicode byte strings.
              - C{None}: the file contents of all files will be
                processed using non-unicode byte strings.
        @param tag_mapping_function: A function for normalizing or
                simplifying the POS tags returned by the tagged_words()
                or tagged_sents() methods.
        s   (.*\.zip)/?(.*)$|s0   CorpusReader: expected a string or a PathPointerN(   t
   isinstancet
   basestringt   ret   matcht   groupsR   R   R   t	   TypeErrort   find_corpus_fileidst   _fileidst   _roott   listt	   _encodingt   _tag_mapping_function(   t   selft   roott   fileidst   encodingt   tag_mapping_functiont   mt   zipfilet   zipentryt   encoding_dictt   fileidt   xt   regexpt   enc(    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   __init__(   s2    $		
  

	c         C   sZ   t  |  i t ƒ o# d |  i i i |  i i f } n d |  i i } d |  i i | f S(   Ns   %s/%ss   %ss
   <%s in %r>(	   R   R   R   R   t   filenamet   entryt   patht	   __class__t   __name__(   R   R#   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   __repr__s   s    #c         C   s   t  |  i d ƒ ƒ i ƒ  S(   sN   
        Return the contents of the corpus README file, if it exists.
        t   README(   t   opent   abspatht   read(   R   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   readmez   s    c         C   s   |  i  S(   se   
        Return a list of file identifiers for the fileids that make up
        this corpus.
        (   R   (   R   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyR      s    c         C   s   |  i  i | ƒ S(   sç   
        Return the absolute path for the given file.

        @type file: C{str}
        @param file: The file identifier for the file whose path
            should be returned.
            
        @rtype: L{PathPointer}
        (   R   t   join(   R   t   file(    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyR)   ˆ   s    
c      	   C   s¨   | d j o |  i } n t | t ƒ o | g } n g  } | D] } | |  i i | ƒ qB ~ } | o5 t | g  } | D] } | |  i | ƒ q| ~ ƒ Sn | Sd S(   sô  
        Return a list of the absolute paths for all fileids in this corpus;
        or for the given list of fileids, if specified.

        @type fileids: C{None} or C{str} or C{list}
        @param fileids: Specifies the set of fileids for which paths should
            be returned.  Can be C{None}, for all fileids; a list of
            file identifiers, for a specified set of fileids; or a single
            file identifier, for a single file.  Note that the return
            value is always a list of paths, even if C{fileids} is a
            single file identifier.
            
        @param include_encoding: If true, then return a list of
            C{(path_pointer, encoding)} tuples.

        @rtype: C{list} of L{PathPointer}
        N(   t   NoneR   R   R   R   R,   t   zipR   (   R   R   t   include_encodingt   _[1]t   ft   pathst   _[2](    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   abspaths”   s    -5c         C   s(   |  i  | ƒ } |  i i | ƒ i | ƒ S(   s  
        Return an open stream that can be used to read the given file.
        If the file's encoding is not C{None}, then the stream will
        automatically decode the file's contents into unicode.

        @param file: The file identifier of the file to read.
        (   R   R   R,   R(   (   R   R-   R   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyR(   ²   s    c         C   s2   t  |  i t ƒ o |  i i | ƒ Sn |  i Sd S(   sÙ   
        Return the unicode encoding for the given corpus file, if known.
        If the encoding is unknown, or if the given file should be
        processed using byte strings (C{str}), then return C{None}.
        N(   R   R   t   dictt   get(   R   R-   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyR   ½   s    c         C   s   |  i  S(   N(   R   (   R   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt	   _get_rootÈ   s    t   docsR   
        The directory where this corpus is stored.

        @type: L{PathPointer}s   Use corpus.fileids() insteadc         C   s
   |  i  ƒ  S(   N(   R   (   R   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   filesÏ   s    c         C   s
   |  i  ƒ  S(   N(   R   (   R   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt
   _get_itemsÔ   s    (   R%   t
   __module__t   __doc__R.   R    R&   R+   R   R)   t   FalseR5   R(   R   R8   t   propertyR   R   R:   R;   t   items(    (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyR      s   K									t   CategorizedCorpusReaderc           B   sA   e  Z d  Z d „  Z d „  Z d „  Z e d „ Z e d „ Z RS(   s›  
    A mixin class used to aid in the implementation of corpus readers
    for categorized corpora.  This class defines the method
    L{categories()}, which returns a list of the categories for the
    corpus or for a specified set of fileids; and overrides L{fileids()}
    to take a C{categories} argument, restricting the set of fileids to
    be returned.

    Subclasses are expected to:

      - Call L{__init__()} to set up the mapping.
        
      - Override all view methods to accept a C{categories} parameter,
        which can be used *instead* of the C{fileids} parameter, to
        select which fileids should be included in the returned view.
    c         C   s  d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d | j o | d |  _ | d =n| d | j o | d |  _ | d =nW d | j o= | d |  _ | d =d | j o | d |  _ | d =qÖ n t d ƒ ‚ d | j p d | j p d | j o t d ƒ ‚ n d S(   sE  
        Initialize this mapping based on keyword arguments, as
        follows:

          - cat_pattern: A regular expression pattern used to find the
            category for each file identifier.  The pattern will be
            applied to each file identifier, and the first matching
            group will be used as the category label for that file.
            
          - cat_map: A dictionary, mapping from file identifiers to
            category labels.
            
          - cat_file: The name of a file that contains the mapping
            from file identifiers to categories.  The argument
            C{cat_delimiter} can be used to specify a delimiter.

        The corresponding argument will be deleted from C{kwargs}.  If
        more than one argument is specified, an exception will be
        raised.
        t   cat_patternt   cat_mapt   cat_filet   cat_delimiters=   Expected keyword argument cat_pattern or cat_map or cat_file.s7   Specify exactly one of: cat_pattern, cat_map, cat_file.N(   R.   t   _f2ct   _c2ft   _patternt   _mapt   _filet
   _delimitert
   ValueError(   R   t   kwargs(    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyR    ï   s,    						c         C   s…  t  t ƒ |  _ t  t ƒ |  _ |  i d  j	 oF xP|  i D]4 } t i |  i | ƒ i	 d ƒ } |  i
 | | ƒ q8 Wn|  i d  j	 oE xú |  i i ƒ  D]- \ } } x | D] } |  i
 | | ƒ q§ Wq” Wn¹ |  i d  j	 o¨ x¥ |  i |  i ƒ i ƒ  D]‡ } | i ƒ  } | i |  i d ƒ \ } } | |  i ƒ  j o t d t | f ƒ ‚ n x* | i |  i ƒ D] } |  i
 | | ƒ q_Wqò Wn d  S(   Ni   s)   In category mapping file %s: %s not found(   R    R   RF   RG   RH   R.   R   R	   R
   t   groupt   _addRI   R@   RJ   R(   t	   readlinest   stript   splitRK   R   RL   t   catfile(   R   t   file_idt   categoryt
   categoriest   line(    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   _init"  s0    
     c         C   s,   |  i  | i | ƒ |  i | i | ƒ d  S(   N(   RF   t   appendRG   (   R   RT   RU   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyRO   :  s    c            s|   ˆ  i  t j o ˆ  i ƒ  n | t j o t ˆ  i ƒ Sn t | t ƒ o | g } n t t ‡  f d †  | Dƒ g  ƒ ƒ S(   s~   
        Return a list of the categories that are defined for this corpus,
        or for the file(s) if it is given.
        c         3   s    x |  ] } ˆ  i  | Vq Wd  S(   N(   RF   (   t   .0t   d(   R   (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pys	   <genexpr>H  s    (   RF   R.   RX   t   sortedRG   R   R   t   sum(   R   R   (    (   R   s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyRV   >  s    c            s¬   | d j o t t ˆ  ƒ i ƒ  Sn… t | t ƒ o3 ˆ  i d j o ˆ  i ƒ  n t ˆ  i	 | ƒ SnB ˆ  i d j o ˆ  i ƒ  n t t
 ‡  f d †  | Dƒ g  ƒ ƒ Sd S(   s—   
        Return a list of file identifiers for the files that make up
        this corpus, or that make up the given category(s) if specified.
        c         3   s    x |  ] } ˆ  i  | Vq Wd  S(   N(   RG   (   RZ   t   c(   R   (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pys	   <genexpr>V  s    N(   R.   t   superRA   R   R   R   RF   RX   R\   RG   R]   (   R   RV   (    (   R   s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyR   J  s    (	   R%   R<   R=   R    RX   RO   R.   RV   R   (    (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyRA   Ý   s   	3		t   SyntaxCorpusReaderc           B   s  e  Z d  Z d „  Z d „  Z d „  Z d „  Z e d „ Z e d „ Z	 e e
 d „ Z e d „ Z e e
 d	 „ Z e d
 „ Z d „  Z e
 d „ Z d „  Z e
 d „ Z d „  Z e d ƒ e d d „ ƒ Z e d ƒ e d „ ƒ Z e d ƒ e d „ ƒ Z e d ƒ e d „ ƒ Z RS(   sC  
    An abstract base class for reading corpora consisting of
    syntactically parsed text.  Subclasses should define:

      - L{__init__}, which specifies the location of the corpus
        and a method for detecting the sentence blocks in corpus files.
      - L{_read_block}, which reads a block from the input stream.
      - L{_word}, which takes a block and returns a list of list of words.
      - L{_tag}, which takes a block and returns a list of list of tagged
        words.
      - L{_parse}, which takes a block and returns a list of parsed
        sentences.
    c         C   s   t  d ƒ ‚ d  S(   Ns   Abstract method(   t   AssertionError(   R   t   s(    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   _parsek  s    c         C   s   t  d ƒ ‚ d  S(   Ns   Abstract method(   Ra   (   R   Rb   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   _wordm  s    c         C   s   t  d ƒ ‚ d  S(   Ns   Abstract method(   Ra   (   R   Rb   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   _tago  s    c         C   s   t  d ƒ ‚ d  S(   Ns   Abstract method(   Ra   (   R   t   stream(    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   _read_blockq  s    c         C   sk   | d  j o |  i } n t | t ƒ o | g } n t g  } | D] } | |  i | ƒ i ƒ  qE ~ ƒ S(   N(   R.   R   R   R   t   concatR(   R*   (   R   R   R1   R2   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   rawt  s    c      	   C   sO   |  i  } t g  } |  i | t ƒ D]" \ } } | t | | d | ƒq# ~ ƒ S(   NR   (   t   _read_parsed_sent_blockRh   R5   t   Truet   StreamBackedCorpusView(   R   R   t   readerR1   R   R   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   parsed_sentsy  s    	
c      
      sX   ‡  ‡ f d †  } t  g  } ˆ  i | t ƒ D]" \ } } | t | | d | ƒq, ~ ƒ S(   Nc            s   ˆ  i  |  ˆ ƒ S(   N(   t   _read_tagged_sent_block(   Rf   (   R   t   simplify_tags(    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyRm     s    R   (   Rh   R5   Rk   Rl   (   R   R   Rp   Rm   R1   R   R   (    (   R   Rp   s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   tagged_sents~  s    
c      	   C   sO   |  i  } t g  } |  i | t ƒ D]" \ } } | t | | d | ƒq# ~ ƒ S(   NR   (   t   _read_sent_blockRh   R5   Rk   Rl   (   R   R   Rm   R1   R   R   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   sents„  s    	
c      
      sX   ‡  ‡ f d †  } t  g  } ˆ  i | t ƒ D]" \ } } | t | | d | ƒq, ~ ƒ S(   Nc            s   ˆ  i  |  ˆ ƒ S(   N(   t   _read_tagged_word_block(   Rf   (   R   Rp   (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyRm   Š  s    R   (   Rh   R5   Rk   Rl   (   R   R   Rp   Rm   R1   R   R   (    (   R   Rp   s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   tagged_words‰  s    
c      	   C   sI   t  g  } |  i | t ƒ D]% \ } } | t | |  i d | ƒq ~ ƒ S(   NR   (   Rh   R5   Rk   Rl   t   _read_word_block(   R   R   R1   R   R   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyt   words  s    
c         C   s   t  |  i | ƒ g  ƒ S(   N(   R]   Rr   (   R   Rf   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyRv   ˜  s    c         C   s   t  |  i | | ƒ g  ƒ S(   N(   R]   Ro   (   R   Rf   Rp   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyRt   ›  s    c         C   s:   t  d  g  } |  i | ƒ D] } | |  i | ƒ q ~ ƒ S(   N(   t   filterR.   Rg   Rd   (   R   Rf   R1   t   t(    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyRr   ž  s    c         C   s=   t  d  g  } |  i | ƒ D] } | |  i | | ƒ q ~ ƒ S(   N(   Rx   R.   Rg   Re   (   R   Rf   Rp   R1   Ry   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyRo   ¡  s    c         C   s:   t  d  g  } |  i | ƒ D] } | |  i | ƒ q ~ ƒ S(   N(   Rx   R.   Rg   Rc   (   R   Rf   R1   Ry   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyRj   ¥  s    sE   Use .raw() or .sents() or .tagged_sents() or .parsed_sents() instead.t   parsedc         C   sŒ   | d j o |  i  | ƒ Sn | d j o |  i | ƒ Sn | d j o |  i | ƒ Sn | d j o |  i | ƒ Sn t d | ƒ ‚ d  S(   NRz   Ri   t	   tokenizedt   taggeds   bad format %r(   Rn   Ri   Rs   Rq   RL   (   R   R@   t   format(    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyR*   ¬  s
    s   Use .parsed_sents() instead.c         C   s   |  i  | ƒ S(   N(   Rn   (   R   R@   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyRz   ´  s    s   Use .sents() instead.c         C   s   |  i  | ƒ S(   N(   Rs   (   R   R@   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyR{   ·  s    s   Use .tagged_sents() instead.c         C   s   |  i  | ƒ S(   N(   Rq   (   R   R@   (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyR|   º  s    (   R%   R<   R=   Rc   Rd   Re   Rg   R.   Ri   Rn   R>   Rq   Rs   Ru   Rw   Rv   Rt   Rr   Ro   Rj   R   R*   Rz   R{   R|   (    (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pyR`   ]  s0   												(   R=   t   osR	   t   nltkR    t   nltk.internalsR   t	   nltk.dataR   R   R   t   utilt   objectR   RA   R`   (    (    (    s,   /p/zhu/06/nlp/nltk/nltk/corpus/reader/api.pys   <module>   s   
Ç€