
    gA                          d Z ddlZddl ddl ddl  G d de      Z G d dee      Z	 G d d	e	      Z
 G d
 de      Zy)z;
A reader for corpora that consist of plaintext documents.
    N)*c                   Z    e Zd ZdZeZ	  e       dedfdZddZ	ddZ
ddZd Zd	 Zd
 Zy)PlaintextCorpusReadera  
    Reader for corpora that consist of plaintext documents.  Paragraphs
    are assumed to be split using blank lines.  Sentences and words can
    be tokenized using the default tokenizers, or by custom tokenizers
    specified as parameters to the constructor.

    This corpus reader can be customized (e.g., to skip preface
    sections of specific document formats) by creating a subclass and
    overriding the ``CorpusView`` class variable.
    Nutf8c                 ^    t         j                  | |||       || _        || _        || _        y)a  
        Construct a new plaintext corpus reader for a set of documents
        located at the given root directory.  Example usage:

            >>> root = '/usr/local/share/nltk_data/corpora/webtext/'
            >>> reader = PlaintextCorpusReader(root, '.*\.txt') # doctest: +SKIP

        :param root: The root directory for this corpus.
        :param fileids: A list or regexp specifying the fileids in this corpus.
        :param word_tokenizer: Tokenizer for breaking sentences or
            paragraphs into words.
        :param sent_tokenizer: Tokenizer for breaking paragraphs
            into words.
        :param para_block_reader: The block reader used to divide the
            corpus into paragraph blocks.
        N)CorpusReader__init___word_tokenizer_sent_tokenizer_para_block_reader)selfrootfileidsword_tokenizersent_tokenizerpara_block_readerencodings          Q/var/www/openai/venv/lib/python3.12/site-packages/nltk/corpus/reader/plaintext.pyr	   zPlaintextCorpusReader.__init__%   s0    2 	dD'8<--"3    c                     t        | j                  |dd      D cg c]$  \  }}}| j                  || j                  |      & c}}}      S c c}}}w )z~
        :return: the given file(s) as a list of words
            and punctuation symbols.
        :rtype: list(str)
        Tr   )concatabspaths
CorpusView_read_word_blockr   r   pathencfileids        r   wordszPlaintextCorpusReader.wordsC   s\      ,0==$+M+M'T3 d&;&;cJ+M
 	
s   )Ac                    | j                   	 t               | _         t        | j	                  |dd      D cg c]$  \  }}}| j                  || j                  |      & c}}}      S #  t        d      xY wc c}}}w )z
        :return: the given file(s) as a list of
            sentences or utterances, each encoded as a list of word
            strings.
        :rtype: list(list(str))
        %No sentence tokenizer for this corpusTr   )r   PunktTokenizer
ValueErrorr   r   r   _read_sent_blockr   s        r   sentszPlaintextCorpusReader.sentsP        'J'5'7$  ,0==$+M+M'T3 d&;&;cJ+M
 	
J !HII   A+ )A;+A8c                    | j                   	 t               | _         t        | j	                  |dd      D cg c]$  \  }}}| j                  || j                  |      & c}}}      S #  t        d      xY wc c}}}w )z
        :return: the given file(s) as a list of
            paragraphs, each encoded as a list of sentences, which are
            in turn encoded as lists of word strings.
        :rtype: list(list(list(str)))
        r"   Tr   )r   r#   r$   r   r   r   _read_para_blockr   s        r   paraszPlaintextCorpusReader.parasd   r'   r(   c                     g }t        d      D ]:  }|j                  | j                  j                  |j	                                      < |S N   )rangeextendr
   tokenizereadliner   streamr    is       r   r   z&PlaintextCorpusReader._read_word_blockx   s>    rALL--66v7HIJ r   c           	          g }| j                  |      D ]S  }|j                  | j                  j                  |      D cg c]  }| j                  j                  |       c}       U |S c c}w N)r   r0   r   r1   r
   r   r4   r&   parasents        r   r%   z&PlaintextCorpusReader._read_sent_block~   s    ++F3DLL !% 4 4 = =d C C ((11$7 C 4    "A,
c           	          g }| j                  |      D ]S  }|j                  | j                  j                  |      D cg c]  }| j                  j                  |       c}       U |S c c}w r7   )r   appendr   r1   r
   r   r4   r+   r9   r:   s        r   r*   z&PlaintextCorpusReader._read_para_block   r;   r<   r7   )__name__
__module____qualname____doc__StreamBackedCorpusViewr   WordPunctTokenizerread_blankline_blockr	   r    r&   r+   r   r%   r*    r   r   r   r      sH    	 (JE *+.4<

(
(		r   r   c                       e Zd ZdZd Zy) CategorizedPlaintextCorpusReaderzy
    A reader for plaintext corpora whose documents are divided into
    categories based on their file identifiers.
    c                 b    t         j                  | |       t        j                  | g|i | y)a  
        Initialize the corpus reader.  Categorization arguments
        (``cat_pattern``, ``cat_map``, and ``cat_file``) are passed to
        the ``CategorizedCorpusReader`` constructor.  The remaining arguments
        are passed to the ``PlaintextCorpusReader`` constructor.
        N)CategorizedCorpusReaderr	   r   r   argskwargss      r   r	   z)CategorizedPlaintextCorpusReader.__init__   s,     	 ((v6&&t=d=f=r   N)r@   rA   rB   rC   r	   rG   r   r   rI   rI      s    
>r   rI   c                       e Zd Zd Zy)*PortugueseCategorizedPlaintextCorpusReaderc                 L    t         j                  | |       t        d      |d<   y )N
portugueser   )rK   r	   r#   rL   s      r   r	   z3PortugueseCategorizedPlaintextCorpusReader.__init__   s"    ((v6#1,#? r   N)r@   rA   rB   r	   rG   r   r   rP   rP      s    @r   rP   c                   2    e Zd ZdZd Zd Zd ZddZddZy)	EuroparlCorpusReadera  
    Reader for Europarl corpora that consist of plaintext documents.
    Documents are divided into chapters instead of paragraphs as
    for regular plaintext documents. Chapters are separated using blank
    lines. Everything is inherited from ``PlaintextCorpusReader`` except
    that:

    - Since the corpus is pre-processed and pre-tokenized, the
      word tokenizer should just split the line at whitespaces.
    - For the same reason, the sentence tokenizer should just
      split the paragraph at line breaks.
    - There is a new 'chapters()' method that returns chapters instead
      instead of paragraphs.
    - The 'paras()' method inherited from PlaintextCorpusReader is
      made non-functional to remove any confusion between chapters
      and paragraphs for Europarl.
    c                     g }t        d      D ]/  }|j                  |j                         j                                1 |S r-   )r/   r0   r2   splitr3   s       r   r   z%EuroparlCorpusReader._read_word_block   s6    rALL*0023 r   c                     g }| j                  |      D ]=  }|j                  |j                         D cg c]  }|j                          c}       ? |S c c}w r7   )r   r0   
splitlinesrV   r8   s        r   r%   z%EuroparlCorpusReader._read_sent_block   Q    ++F3DLL4??3DE3D4$**,3DEF 4 F   A
c                     g }| j                  |      D ]=  }|j                  |j                         D cg c]  }|j                          c}       ? |S c c}w r7   )r   r>   rX   rV   r?   s        r   r*   z%EuroparlCorpusReader._read_para_block   rY   rZ   Nc                     t        | j                  |d      D cg c]#  \  }}| j                  || j                  |      % c}}      S c c}}w )z
        :return: the given file(s) as a list of
            chapters, each encoded as a list of sentences, which are
            in turn encoded as lists of word strings.
        :rtype: list(list(list(str)))
        Tr   )r   r   r   r*   )r   r   r   r   s       r   chapterszEuroparlCorpusReader.chapters   sW      &*]]7D%A%AMVS (=(=L%A
 	
s   (A
c                     t        d      )NzVThe Europarl corpus reader does not support paragraphs. Please use chapters() instead.)NotImplementedError)r   r   s     r   r+   zEuroparlCorpusReader.paras   s    !d
 	
r   r7   )	r@   rA   rB   rC   r   r%   r*   r]   r+   rG   r   r   rT   rT      s     $

r   rT   )rC   	nltk.datanltknltk.corpus.reader.apinltk.corpus.reader.utilnltk.tokenizer   r   rK   rI   rP   rT   rG   r   r   <module>re      sS     $ % ~L ~B>'>@U >(@1Q @6
0 6
r   