
    OfJ                         S r SSKrSSKrSSKJr  SSKJr  SSK7  SSKJ	r	J
r
Jr   " S S5      r " S	 S
5      r " S S\5      rg)z
API for corpus readers.
    N)defaultdict)chain)*)FileSystemPathPointerPathPointerZipFilePathPointerc                       \ rS rSrSrSS jrS rS rS rS r	S	 r
S
 rS rSS jrSS jrS rS rS r\" \SS9rSrg)CorpusReader   ao  
A base class for "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 ``file identifier``, which is the relative path
to the file from the root directory.

A separate subclass is defined for each corpus format.  These
subclasses define one or more methods that provide 'views' on the
corpus contents, such as ``words()`` (for a list of words) and
``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 ``fileids`` or ``categories``, which can
be used to select which portion of the corpus should be returned.
Nc                 ~   [        U[        5      (       a]  [        U[        5      (       dH  [        R                  " SU5      nUR                  5       u  pgU(       a  [        Xg5      nO,[        U5      nO [        U[        5      (       d  [        S5      e[        U[        5      (       a  [        X5      nX l
         Xl         SU l        SU l        SU l        [        U[        5      (       aF  0 nU R                   H2  n	U H)  n
U
u  p[        R                  " X5      (       d  M$  XU	'     M0     M4     UnX0l         X@l        g)aF  
:type root: PathPointer or 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 ``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.  The value of ``encoding`` can be any
    of the following:

    - A string: ``encoding`` is the encoding name for all files.
    - A dictionary: ``encoding[file_id]`` is the encoding
      name for the file whose identifier is ``file_id``.  If
      ``file_id`` is not in ``encoding``, then the file
      contents will be processed using non-unicode byte strings.
    - A list: ``encoding`` should be a list of ``(regexp, encoding)``
      tuples.  The encoding for a file whose identifier is ``file_id``
      will be the ``encoding`` value for the first tuple whose
      ``regexp`` matches the ``file_id``.  If no tuple's ``regexp``
      matches the ``file_id``, the file contents will be processed
      using non-unicode byte strings.
    - None: the file contents of all files will be
      processed using non-unicode byte strings.
:param tagset: The name of the tagset used by this corpus, to be used
      for normalizing or converting the POS tags returned by the
      ``tagged_...()`` methods.
z(.*\.zip)/?(.*)$|z0CorpusReader: expected a string or a PathPointerREADMELICENSEzcitation.bibN)
isinstancestrr   rematchgroupsr   r   	TypeErrorfind_corpus_fileids_fileids_root_readme_license	_citationlist	_encoding_tagset)selfrootfileidsencodingtagsetmzipfilezipentryencoding_dictfileidxregexpencs                8/usr/lib/python3/dist-packages/nltk/corpus/reader/api.py__init__CorpusReader.__init__)   s   B dC  D+)F)F-t4A !
G)'<,T2D+..NOO gs##)$8G	 
1!' h%%M--!A$%MVxx//03f-	 " ( %H!	9     c                 "   [        U R                  [        5      (       a:  U R                  R                  R                   SU R                  R
                   3nOSU R                  R                  -  nSU R                  R                   SU< S3$ )N/z%s<z in >)	r   r   r   r$   filenameentrypath	__class____name__)r   r5   s     r+   __repr__CorpusReader.__repr__u   sr    djj"455jj((112!DJJ4D4D3EFD$**//)D4>>**+4xq99r.   c                     g)z
Load this corpus (if it has not already been loaded).  This is
used by LazyCorpusLoader as a simple method that can be used to
make sure a corpus is loaded -- e.g., in case a user wants to
do help(some_corpus).
N r   s    r+   ensure_loadedCorpusReader.ensure_loaded|   s     	r.   c                     U R                  U R                  5       nUR                  5       sSSS5        $ ! , (       d  f       g= f)z>
Return the contents of the corpus README file, if it exists.
N)openr   readr   fs     r+   readmeCorpusReader.readme   s+     YYt||$668 %$$	   6
Ac                     U R                  U R                  5       nUR                  5       sSSS5        $ ! , (       d  f       g= f)z?
Return the contents of the corpus LICENSE file, if it exists.
N)r@   r   rA   rB   s     r+   licenseCorpusReader.license   s+     YYt}}%668 &%%rF   c                     U R                  U R                  5       nUR                  5       sSSS5        $ ! , (       d  f       g= f)zD
Return the contents of the corpus citation.bib file, if it exists.
N)r@   r   rA   rB   s     r+   citationCorpusReader.citation   s+     YYt~~&!668 '&&rF   c                     U R                   $ )zM
Return a list of file identifiers for the fileids that make up
this corpus.
)r   r<   s    r+   r    CorpusReader.fileids   s    
 }}r.   c                 8    U R                   R                  U5      $ )z
Return the absolute path for the given file.

:type fileid: str
:param fileid: The file identifier for the file whose path
    should be returned.
:rtype: PathPointer
)r   join)r   r'   s     r+   abspathCorpusReader.abspath   s     zzv&&r.   c                    Uc  U R                   nO[        U[        5      (       a  U/nU Vs/ s H  o@R                  R	                  U5      PM     nnU(       a;  U(       a4  [        [        XQ Vs/ s H  o@R                  U5      PM     snU5      5      $ U(       a  [        [        XQ5      5      $ U(       a3  [        [        XQ Vs/ s H  o@R                  U5      PM     sn5      5      $ U$ s  snf s  snf s  snf )an  
Return a list of the absolute paths for all fileids in this corpus;
or for the given list of fileids, if specified.

:type fileids: None or str or list
:param fileids: Specifies the set of fileids for which paths should
    be returned.  Can be 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 ``fileids`` is a
    single file identifier.

:param include_encoding: If true, then return a list of
    ``(path_pointer, encoding)`` tuples.

:rtype: list(PathPointer)
)r   r   r   r   rP   r   zipr!   )r   r    include_encodinginclude_fileidrC   pathss         r+   abspathsCorpusReader.abspaths   s    $ ?mmG%%iG-45W#W5Eg#FgMM!$4g#FPQQE+,,Eg#FgMM!$4g#FGHHL 6 $G $Gs   $C,/C1C6c                    Uc  U R                   nO[        U[        5      (       a  U/n/ nU H<  nU R                  U5       nUR	                  UR                  5       5        SSS5        M>     [        U5      $ ! , (       d  f       MZ  = f)z
:param fileids: A list specifying the fileids that should be used.
:return: the given file(s) as a single string.
:rtype: str
N)r   r   r   r@   appendrA   concat)r   r    contentsrC   fps        r+   rawCorpusReader.raw   sp     ?mmG%%iGA1	*   h s    A88
B	c                 |    U R                  U5      nU R                  R                  U5      R                  U5      nU$ )z
Return an open stream that can be used to read the given file.
If the file's encoding is not 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   rP   r@   )r   filer!   streams       r+   r@   CorpusReader.open   s4     ==&&++H5r.   c                     [        U R                  [        5      (       a  U R                  R                  U5      $ U R                  $ )z
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 (str), then return None.
)r   r   dictget)r   rb   s     r+   r!   CorpusReader.encoding   s4     dnnd++>>%%d++>>!r.   c                     U R                   $ N)r   r<   s    r+   	_get_rootCorpusReader._get_root   s    zzr.   zO
        The directory where this corpus is stored.

        :type: PathPointer)doc)r   r   r   r   r   r   r   )utf8N)NFFrj   )r7   
__module____qualname____firstlineno____doc__r,   r8   r=   rD   rH   rK   r    rQ   rX   r_   r@   r!   rk   propertyr   __static_attributes__r;   r.   r+   r
   r
      s_    $JX:	' D  
	" Dr.   r
   c                      ^  \ rS rSrSrS rS rS rSS jrSU 4S jjr	S r
SU 4S	 jjrSU 4S
 jjrSU 4S jjrSU 4S jjrSrU =r$ )CategorizedCorpusReaderi  ai  
A mixin class used to aid in the implementation of corpus readers
for categorized corpora.  This class defines the method
``categories()``, which returns a list of the categories for the
corpus or for a specified set of fileids; and overrides ``fileids()``
to take a ``categories`` argument, restricting the set of fileids to
be returned.

Subclasses are expected to:

  - Call ``__init__()`` to set up the mapping.

  - Override all view methods to accept a ``categories`` parameter,
    which can be used *instead* of the ``fileids`` parameter, to
    select which fileids should be included in the returned view.
c                 F   SU l         SU l        SU l        SU l        SU l        SU l        SU;   a  US   U l        US	 OFSU;   a  US   U l        US	 O2SU;   a!  US   U l        US	 SU;   a  US   U l        US	 O[        S5      eSU;   d  SU;   d  SU;   a  [        S5      eg)a  
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
    ``cat_delimiter`` can be used to specify a delimiter.

The corresponding argument will be deleted from ``kwargs``.  If
more than one argument is specified, an exception will be
raised.
Ncat_patterncat_mapcat_filecat_delimiterz=Expected keyword argument cat_pattern or cat_map or cat_file.z7Specify exactly one of: cat_pattern, cat_map, cat_file.)_f2c_c2f_pattern_map_file
_delimiter
ValueError)r   kwargss     r+   r,    CategorizedCorpusReader.__init__  s    * 			
F""=1DM}%& y)DIy!6!
+DJz"&("("9?+R  F"i6&9Z6=QL  >Rr.   c           	         [        [        5      U l        [        [        5      U l        U R                  bU  U R
                   HD  n[        R                  " U R                  U5      R                  S5      nU R                  X5        MF     g U R                  b>  U R                  R                  5        H  u  pU H  nU R                  X5        M     M!     g U R                  b  U R                  U R                  5       nUR                  5        H  nUR                  5       nUR!                  U R"                  S5      u  pXR%                  5       ;  a  ['        SU R                  < SU< S35      eUR!                  U R"                  5       H  nU R                  X5        M     M     S S S 5        g g ! , (       d  f       g = f)N   zIn category mapping file z: z
 not found)r   setr|   r}   r~   r   r   r   group_addr   itemsr   r@   	readlinesstripsplitr   r    r   )r   file_idcategory
categoriesrC   lines         r+   _initCategorizedCorpusReader._initK  sK   $	$	==$==88DMM7;AA!D		', ) YY"'+yy'8# *HIIg0 !+ (9 ZZ#4::&!KKMD::<D*.**T__a*H'Glln4(+/::w@  %/$4$4T__$E		'4 %F * '& $&&s   >B*F22
G c                 |    U R                   U   R                  U5        U R                  U   R                  U5        g rj   )r|   addr}   )r   r   r   s      r+   r   CategorizedCorpusReader._addf  s0    		'x(		((r.   c                    ^  T R                   c  T R                  5         Uc  [        T R                  5      $ [	        U[
        5      (       a  U/n[        [        R                  " U 4S jU 5       6 5      $ )zf
Return a list of the categories that are defined for this corpus,
or for the file(s) if it is given.
c              3   B   >#    U  H  nTR                   U   v   M     g 7frj   )r|   ).0dr   s     r+   	<genexpr>5CategorizedCorpusReader.categories.<locals>.<genexpr>u  s     !@1$))A,   )r|   r   sortedr}   r   r   r   union)r   r    s   ` r+   r   "CategorizedCorpusReader.categoriesj  sZ    
 99JJL?$))$$gs##iGcii!@!@ABBr.   c                 ~  >^  Uc  [         TT ]  5       $ [        U[        5      (       aS  T R                  c  T R                  5         UT R                  ;   a  [        T R                  U   5      $ [        SU-  5      eT R                  c  T R                  5         [        [        R                  " U 4S jU 5       6 5      $ )z
Return a list of file identifiers for the files that make up
this corpus, or that make up the given category(s) if specified.
zCategory %s not foundc              3   B   >#    U  H  nTR                   U   v   M     g 7frj   )r}   )r   cr   s     r+   r   2CategorizedCorpusReader.fileids.<locals>.<genexpr>  s     %GJqdiilJr   )superr    r   r   r|   r   r}   r   r   r   r   )r   r   r6   s   ` r+   r    CategorizedCorpusReader.fileidsw  s    
 7?$$
C((yy 

TYY&dii
344 !8:!EFFyy 

#))%GJ%GHIIr.   c                 P    Ub  Ub  [        S5      eUb  U R                  U5      $ U$ )Nz'Specify fileids or categories, not both)r   r    )r   r    r   s      r+   _resolve CategorizedCorpusReader._resolve  s3    :#9FGG!<<
++Nr.   c                 @   > [         TU ]  U R                  X5      5      $ rj   )r   r_   r   r   r    r   r6   s      r+   r_   CategorizedCorpusReader.raw  s    w{4===>>r.   c                 @   > [         TU ]  U R                  X5      5      $ rj   )r   wordsr   r   s      r+   r   CategorizedCorpusReader.words      w}T]]7?@@r.   c                 @   > [         TU ]  U R                  X5      5      $ rj   )r   sentsr   r   s      r+   r   CategorizedCorpusReader.sents  r   r.   c                 @   > [         TU ]  U R                  X5      5      $ rj   )r   parasr   r   s      r+   r   CategorizedCorpusReader.paras  r   r.   )r}   r   r|   r   r   r~   rj   NN)r7   ro   rp   rq   rr   r,   r   r   r   r    r   r_   r   r   r   rt   __classcell__)r6   s   @r+   rv   rv     sF    "1f56)CJ&?AAA Ar.   rv   c                       \ rS rSrSrS rS rS rS rSS jr	SS	 jr
SS
 jrSS jrSS jrS rSS jrS rSS jrS rSrg)SyntaxCorpusReaderi  a  
An abstract base class for reading corpora consisting of
syntactically parsed text.  Subclasses should define:

  - ``__init__``, which specifies the location of the corpus
    and a method for detecting the sentence blocks in corpus files.
  - ``_read_block``, which reads a block from the input stream.
  - ``_word``, which takes a block and returns a list of list of words.
  - ``_tag``, which takes a block and returns a list of list of tagged
    words.
  - ``_parse``, which takes a block and returns a list of parsed
    sentences.
c                     [        5       erj   NotImplementedErrorr   ss     r+   _parseSyntaxCorpusReader._parse      !##r.   c                     [        5       erj   r   r   s     r+   _wordSyntaxCorpusReader._word  r   r.   c                     [        5       erj   r   r   s     r+   _tagSyntaxCorpusReader._tag  r   r.   c                     [        5       erj   r   r   rc   s     r+   _read_blockSyntaxCorpusReader._read_block  r   r.   Nc                     U R                   n[        U R                  US5       VVs/ s H  u  p4[        X2US9PM     snn5      $ s  snnf NTr!   )_read_parsed_sent_blockr\   rX   StreamBackedCorpusViewr   r    readerr'   r*   s        r+   parsed_sentsSyntaxCorpusReader.parsed_sents  sP    -- $(==$#?#?KF 'vD#?
 	
   A
c                    ^ ^ U U4S jn[        T R                  US5       VVs/ s H  u  pE[        XCUS9PM     snn5      $ s  snnf )Nc                 (   > TR                  U T5      $ rj   )_read_tagged_sent_blockrc   r   r"   s    r+   r   /SyntaxCorpusReader.tagged_sents.<locals>.reader      //??r.   Tr   r\   rX   r   r   r    r"   r   r'   r*   s   ` `   r+   tagged_sentsSyntaxCorpusReader.tagged_sents  N    	@  $(==$#?#?KF 'vD#?
 	
   A
c                     U R                   n[        U R                  US5       VVs/ s H  u  p4[        X2US9PM     snn5      $ s  snnf r   )_read_sent_blockr\   rX   r   r   s        r+   r   SyntaxCorpusReader.sents  sP    && $(==$#?#?KF 'vD#?
 	
r   c                    ^ ^ U U4S jn[        T R                  US5       VVs/ s H  u  pE[        XCUS9PM     snn5      $ s  snnf )Nc                 (   > TR                  U T5      $ rj   )_read_tagged_word_blockr   s    r+   r   /SyntaxCorpusReader.tagged_words.<locals>.reader  r   r.   Tr   r   r   s   ` `   r+   tagged_wordsSyntaxCorpusReader.tagged_words  r   r   c                     [        U R                  US5       VVs/ s H  u  p#[        X R                  US9PM     snn5      $ s  snnf r   )r\   rX   r   _read_word_block)r   r    r'   r*   s       r+   r   SyntaxCorpusReader.words  sK     $(==$#?#?KF 'v/D/DsS#?
 	
s    A
c                 ^    [        [        R                  " U R                  U5      5      5      $ rj   )r   r   from_iterabler   r   s     r+   r   #SyntaxCorpusReader._read_word_block  s#    E''(=(=f(EFGGr.   c                 ^    [        [        R                  " U R                  X5      5      5      $ rj   )r   r   r   r   )r   rc   r"   s      r+   r   *SyntaxCorpusReader._read_tagged_word_block  s#    E''(D(DV(TUVVr.   c                     [        [        S U R                  U5       Vs/ s H  o R                  U5      PM     sn5      5      $ s  snf rj   )r   filterr   r   r   rc   ts      r+   r   #SyntaxCorpusReader._read_sent_block  s9    F49I9I&9Q!R9QA**Q-9Q!RSTT!R   Ac                     [        [        S U R                  U5       Vs/ s H  o0R                  X25      PM     sn5      5      $ s  snf rj   )r   r   r   r   )r   rc   r"   r   s       r+   r   *SyntaxCorpusReader._read_tagged_sent_block  s?    48H8H8PQ8P1))A.8PQR
 	
Qr   c                     [        [        S U R                  U5       Vs/ s H  o R                  U5      PM     sn5      5      $ s  snf rj   )r   r   r   r   r   s      r+   r   *SyntaxCorpusReader._read_parsed_sent_block  s9    F4$:J:J6:R!S:RQ++a.:R!STUU!Sr   r;   rj   r   )r7   ro   rp   rq   rr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rt   r;   r.   r+   r   r     sQ    $$$$
	

	

HWU

Vr.   r   )rr   osr   collectionsr   	itertoolsr   nltk.corpus.reader.util	nltk.datar   r   r   r
   rv   r   r;   r.   r+   <module>r     sM    
 	 #  % L Lh h`VA VA~]V ]Vr.   