
    gJ                         d Z ddlZddlZddlmZ ddlmZ ddl ddlm	Z	m
Z
mZ  G d d      Z G d	 d
      Z G d de      Zy)z
API for corpus readers.
    N)defaultdict)chain)*)FileSystemPathPointerPathPointerZipFilePathPointerc                   x    e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 ZddZddZd Zd Zd Z eed      Zy)CorpusReadera  
    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                 >   t        |t              rTt        |t              sDt        j                  d|      }|j                         \  }}|rt        ||      }n't        |      }nt        |t              st        d      t        |t              rt        ||      }|| _
        	 || _        	 d| _        d| _        d| _        t        |t              r>i }| j                  D ]+  }	|D ]$  }
|
\  }}t        j                  ||	      s|||	<    + - |}|| _        	 || _        y)a.  
        :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                K/var/www/openai/venv/lib/python3.12/site-packages/nltk/corpus/reader/api.py__init__zCorpusReader.__init__)   s   B dC D+)F-t4A !
GX)'8<,T2D+.NOO gs#)$8G	 
1!' h%M--!A$%MVSxx/03f-	 " ( %H!	9     c                    t        | j                  t              r:| j                  j                  j                   d| j                  j
                   }nd| j                  j                  z  }d| j                  j                   d|dS )N/z%s<z in >)	r   r   r   r#   filenameentrypath	__class____name__)r   r3   s     r*   __repr__zCorpusReader.__repr__u   so    djj"45jj((112!DJJ4D4D3EFD$**//)D4>>**+4xq99r,   c                      y)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_loadedzCorpusReader.ensure_loaded|   s     	r,   c                     | j                  | j                        5 }|j                         cddd       S # 1 sw Y   yxY w)zN
        Return the contents of the corpus README file, if it exists.
        N)openr   readr   fs     r*   readmezCorpusReader.readme   s+     YYt||$668 %$$   6?c                     | j                  | j                        5 }|j                         cddd       S # 1 sw Y   yxY w)zO
        Return the contents of the corpus LICENSE file, if it exists.
        N)r<   r   r=   r>   s     r*   licensezCorpusReader.license   s+     YYt}}%668 &%%rA   c                     | j                  | j                        5 }|j                         cddd       S # 1 sw Y   yxY w)zT
        Return the contents of the corpus citation.bib file, if it exists.
        N)r<   r   r=   r>   s     r*   citationzCorpusReader.citation   s+     YYt~~&!668 '&&rA   c                     | j                   S )ze
        Return a list of file identifiers for the fileids that make up
        this corpus.
        )r   r9   s    r*   r   zCorpusReader.fileids   s    
 }}r,   c                 8    | j                   j                  |      S )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*   abspathzCorpusReader.abspath   s     zzv&&r,   c                    || j                   }nt        |t              r|g}|D cg c]  }| j                  j	                  |       }}|r5|r3t        t        ||D cg c]  }| j                  |       c}|            S |rt        t        ||            S |r2t        t        ||D cg c]  }| j                  |       c}            S |S c c}w c c}w c c}w )a  
        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   rH   r   zipr    )r   r   include_encodinginclude_fileidr?   pathss         r*   abspathszCorpusReader.abspaths   s    $ ?mmG%iG-45W#W5Eg#FgDMM!$4g#FPQQE7+,,Eg#FgDMM!$4g#FGHHL 6 $G $Gs   "CC*Cc                     || j                   }nt        |t              r|g}g }|D ];  }| j                  |      5 }|j	                  |j                                ddd       = t        |      S # 1 sw Y   RxY w)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<   appendr=   concat)r   r   contentsr?   fps        r*   rawzCorpusReader.raw   sm     ?mmG%iGA1	*   h s    A00A9	c                 |    | j                  |      }| j                  j                  |      j                  |      }|S )a  
        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   rH   r<   )r   filer    streams       r*   r<   zCorpusReader.open   s4     ==&&++H5r,   c                     t        | j                  t              r| j                  j                  |      S | j                  S )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   rW   s     r*   r    zCorpusReader.encoding   s1     dnnd+>>%%d++>>!r,   c                     | j                   S N)r   r9   s    r*   	_get_rootzCorpusReader._get_root   s    zzr,   zO
        The directory where this corpus is stored.

        :type: PathPointer)doc)utf8N)NFFr]   )r5   
__module____qualname____doc__r+   r6   r:   r@   rC   rE   r   rI   rO   rU   r<   r    r^   propertyr   r8   r,   r*   r
   r
      s_    $JX:	' D  
	" Dr,   r
   c                   t     e Zd ZdZd Zd Zd ZddZd fd	Zd Z	d fd	Z
d fd		Zd fd
	Zd fd	Z xZS )CategorizedCorpusReadera  
    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                 *   d| _         d| _        d| _        d| _        d| _        d| _        d|v r|d   | _        |d= n@d|v r|d   | _        |d= n.d|v r|d   | _        |d= d|v r|d   | _        |d= nt        d      d|v sd|v sd|v rt        d      y)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+   z CategorizedCorpusReader.__init__  s    * 			
F""=1DM}%& y)DIy!6!
+DJz"&("("9?+R  F"i6&9Z6=QL  >Rr,   c           	      f   t        t              | _        t        t              | _        | j                  S| j
                  D ]C  }t        j                  | j                  |      j                  d      }| j                  ||       E y | j                  <| j                  j                         D ]  \  }}|D ]  }| j                  ||          y | j                  | j                  | j                        5 }|j                         D ]  }|j                         }|j!                  | j"                  d      \  }}|| j%                         vrt'        d| j                  d|d      |j!                  | j"                        D ]  }| j                  ||         	 d d d        y y # 1 sw Y   y xY w)N   zIn category mapping file z: z
 not found)r   setrl   rm   rn   r   r   r   group_addro   itemsrp   r<   	readlinesstripsplitrq   r   rr   )r   file_idcategory
categoriesr?   lines         r*   _initzCategorizedCorpusReader._initK  sW   $	$	==$==88DMM7;AA!D		'8, ) YY"'+yy'8# *HIIgx0 !+ (9 ZZ#4::&!KKMD::<D*.**T__a*H'GZdlln4(+/::w@  %/$4$4T__$E		'84 %F * '& $&&s   7B%F''F0c                 |    | j                   |   j                  |       | j                  |   j                  |       y r]   )rl   addrm   )r   r}   r~   s      r*   rx   zCategorizedCorpusReader._addf  s0    		'x(		((r,   c                       j                    j                          |t         j                        S t	        |t
              r|g}t        t        j                   fd|D               S )z~
        Return a list of the categories that are defined for this corpus,
        or for the file(s) if it is given.
        c              3   <   K   | ]  }j                   |     y wr]   )rl   ).0dr   s     r*   	<genexpr>z5CategorizedCorpusReader.categories.<locals>.<genexpr>u  s     !@1$))A,   )rl   r   sortedrm   r   r   rv   union)r   r   s   ` r*   r   z"CategorizedCorpusReader.categoriesj  sW    
 99JJL?$))$$gs#iGcii!@!@ABBr,   c                 f    |t                   S t        |t              rP j                   j                          | j                  v rt         j                  |         S t        d|z         j                   j                          t        t        j                   fd|D               S )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   <   K   | ]  }j                   |     y wr]   )rm   )r   cr   s     r*   r   z2CategorizedCorpusReader.fileids.<locals>.<genexpr>  s     %GJqdiilJr   )superr   r   r   rl   r   rm   r   rr   rv   r   )r   r   r4   s   ` r*   r   zCategorizedCorpusReader.fileidsw  s    
 7?$$
C(yy 

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

#))%GJ%GHIIr,   c                 J    ||t        d      || j                  |      S |S )Nz'Specify fileids or categories, not both)rr   r   )r   r   r   s      r*   _resolvez CategorizedCorpusReader._resolve  s3    :#9FGG!<<
++Nr,   c                 B    t         |   | j                  ||            S r]   )r   rU   r   r   r   r   r4   s      r*   rU   zCategorizedCorpusReader.raw  s    w{4==*=>>r,   c                 B    t         |   | j                  ||            S r]   )r   wordsr   r   s      r*   r   zCategorizedCorpusReader.words      w}T]]7J?@@r,   c                 B    t         |   | j                  ||            S r]   )r   sentsr   r   s      r*   r   zCategorizedCorpusReader.sents  r   r,   c                 B    t         |   | j                  ||            S r]   )r   parasr   r   s      r*   r   zCategorizedCorpusReader.paras  r   r,   r]   NN)r5   ra   rb   rc   r+   r   rx   r   r   r   rU   r   r   r   __classcell__)r4   s   @r*   rf   rf     sF    "1f56)CJ&?AAA Ar,   rf   c                   r    e Zd ZdZd Zd Zd Zd ZddZddZ	dd	Z
dd
ZddZd ZddZd ZddZd Zy)SyntaxCorpusReaderaH  
    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                     t               r]   NotImplementedErrorr   ss     r*   _parsezSyntaxCorpusReader._parse      !##r,   c                     t               r]   r   r   s     r*   _wordzSyntaxCorpusReader._word  r   r,   c                     t               r]   r   r   s     r*   _tagzSyntaxCorpusReader._tag  r   r,   c                     t               r]   r   r   rX   s     r*   _read_blockzSyntaxCorpusReader._read_block  r   r,   Nc                     | j                   }t        | j                  |d      D cg c]  \  }}t        |||       c}}      S c c}}w NTr    )_read_parsed_sent_blockrR   rO   StreamBackedCorpusViewr   r   readerr&   r)   s        r*   parsed_sentszSyntaxCorpusReader.parsed_sents  sT    -- $(==$#?#?KFC 'vvD#?
 	
   A
c                       fd}t         j                  |d      D cg c]  \  }}t        |||       c}}      S c c}}w )Nc                 (    j                  |       S r]   )_read_tagged_sent_blockrX   r   r!   s    r*   r   z/SyntaxCorpusReader.tagged_sents.<locals>.reader      //??r,   Tr   rR   rO   r   r   r   r!   r   r&   r)   s   ` `   r*   tagged_sentszSyntaxCorpusReader.tagged_sents  R    	@  $(==$#?#?KFC 'vvD#?
 	
   A
c                     | j                   }t        | j                  |d      D cg c]  \  }}t        |||       c}}      S c c}}w r   )_read_sent_blockrR   rO   r   r   s        r*   r   zSyntaxCorpusReader.sents  sT    && $(==$#?#?KFC 'vvD#?
 	
r   c                       fd}t         j                  |d      D cg c]  \  }}t        |||       c}}      S c c}}w )Nc                 (    j                  |       S r]   )_read_tagged_word_blockr   s    r*   r   z/SyntaxCorpusReader.tagged_words.<locals>.reader  r   r,   Tr   r   r   s   ` `   r*   tagged_wordszSyntaxCorpusReader.tagged_words  r   r   c                     t        | j                  |d      D cg c]  \  }}t        || j                  |       c}}      S c c}}w r   )rR   rO   r   _read_word_block)r   r   r&   r)   s       r*   r   zSyntaxCorpusReader.words  sO     $(==$#?#?KFC 'vt/D/DsS#?
 	
s   "A
c                 \    t        t        j                  | j                  |                  S r]   )r   r   from_iterabler   r   s     r*   r   z#SyntaxCorpusReader._read_word_block  s#    E''(=(=f(EFGGr,   c                 ^    t        t        j                  | j                  ||                  S r]   )r   r   r   r   )r   rX   r!   s      r*   r   z*SyntaxCorpusReader._read_tagged_word_block  s%    E''(D(DVV(TUVVr,   c                     t        t        d | j                  |      D cg c]  }| j                  |       c}            S c c}w r]   )r   filterr   r   r   rX   ts      r*   r   z#SyntaxCorpusReader._read_sent_block  s;    F49I9I&9Q!R9QA$**Q-9Q!RSTT!R   Ac                     t        t        d | j                  |      D cg c]  }| j                  ||       c}            S c c}w r]   )r   r   r   r   )r   rX   r!   r   s       r*   r   z*SyntaxCorpusReader._read_tagged_sent_block  sC    48H8H8PQ8P1$))Av.8PQR
 	
Qs   Ac                     t        t        d | j                  |      D cg c]  }| j                  |       c}            S c c}w r]   )r   r   r   r   r   s      r*   r   z*SyntaxCorpusReader._read_parsed_sent_block  s;    F4$:J:J6:R!S:RQ$++a.:R!STUU!Sr   r]   r   )r5   ra   rb   rc   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r8   r,   r*   r   r     sQ    $$$$
	

	

HWU

Vr,   r   )rc   osr   collectionsr   	itertoolsr   nltk.corpus.reader.util	nltk.datar   r   r   r
   rf   r   r8   r,   r*   <module>r      sM    
 	 #  % L Lh h`VA VA~]V ]Vr,   