a
     ˜ëj…#  ã                   @   s|   d Z ddlZddlZddlZddlmZ ddlT ddlm	Z	 ddl
T ddlT ddlmZ G dd„ deƒZG d	d
„ d
eƒZdS )zN
A reader for corpora that contain chunked (and optionally tagged)
documents.
é    N©Útagstr2tree)Ú*)ÚBracketParseCorpusReader)ÚTreec                   @   s�   e Zd ZdZdeeddd�eddfdd	„Zdd
d„Zddd„Z	d dd„Z
d!dd„Zd"dd„Zd#dd„Zd$dd„Zd%dd„Zd&dd„Zdd„ ZdS )'ÚChunkedCorpusReadera&  
    Reader for chunked (and optionally tagged) corpora.  Paragraphs
    are split using a block reader.  They are then tokenized into
    sentences using a sentence tokenizer.  Finally, these sentences
    are parsed into chunk trees using a string-to-chunktree conversion
    function.  Each of these steps can be performed using a default
    function or a custom function.  By default, paragraphs are split
    on blank lines; sentences are listed one per line; and sentences
    are parsed into chunk trees using ``nltk.chunk.tagstr2tree``.
    Ú Ú
T)ZgapsÚutf8Nc	           	      C   s"   t  | |||¡ ||||f| _dS )z’
        :param root: The root directory for this corpus.
        :param fileids: A list or regexp specifying the fileids in this corpus.
        N)ÚCorpusReaderÚ__init__Ú_cv_args)	ÚselfÚrootÚfileidsÚ	extensionÚstr2chunktreeÚsent_tokenizerÚpara_block_readerÚencodingÚtagset© r   úY/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/corpus/reader/chunked.pyr   &   s    zChunkedCorpusReader.__init__c                    s   t ‡ fdd„ˆ  |d¡D ƒƒS )z~
        :return: the given file(s) as a list of words
            and punctuation symbols.
        :rtype: list(str)
        c              	      s,   g | ]$\}}t ||d d d d gˆ j¢R Ž ‘qS )r   ©ÚChunkedCorpusViewr   ©Ú.0ÚfÚenc©r   r   r   Ú
<listcomp>A   s   ÿz-ChunkedCorpusReader.words.<locals>.<listcomp>T©ÚconcatZabspaths©r   r   r   r   r   Úwords:   s
    

þÿzChunkedCorpusReader.wordsc                    s   t ‡ fdd„ˆ  |d¡D ƒƒS )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))
        c              	      s,   g | ]$\}}t ||d dd d gˆ j¢R Ž ‘qS ©r   é   r   r   r   r   r   r    O   s   ÿz-ChunkedCorpusReader.sents.<locals>.<listcomp>Tr!   r#   r   r   r   ÚsentsG   s
    

þÿzChunkedCorpusReader.sentsc                    s   t ‡ fdd„ˆ  |d¡D ƒƒS )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)))
        c              	      s,   g | ]$\}}t ||d ddd gˆ j¢R Ž ‘qS r%   r   r   r   r   r   r    ]   s   ÿz-ChunkedCorpusReader.paras.<locals>.<listcomp>Tr!   r#   r   r   r   ÚparasU   s
    

þÿzChunkedCorpusReader.parasc                    s    t ‡ ‡fdd„ˆ  |d¡D ƒƒS )z¾
        :return: the given file(s) as a list of tagged
            words and punctuation symbols, encoded as tuples
            ``(word,tag)``.
        :rtype: list(tuple(str,str))
        c              	      s2   g | ]*\}}t ||d dddgˆ j¢R dˆiŽ‘qS ©r&   r   Útarget_tagsetr   r   ©r   r   r   r   r    k   s   ýÿÿÿz4ChunkedCorpusReader.tagged_words.<locals>.<listcomp>Tr!   ©r   r   r   r   r+   r   Útagged_wordsc   s
    
üÿz ChunkedCorpusReader.tagged_wordsc                    s    t ‡ ‡fdd„ˆ  |d¡D ƒƒS )z­
        :return: the given file(s) as a list of
            sentences, each encoded as a list of ``(word,tag)`` tuples.

        :rtype: list(list(tuple(str,str)))
        c              	      s2   g | ]*\}}t ||d d ddgˆ j¢R dˆiŽ‘qS r)   r   r   r+   r   r   r    {   s   ýÿÿÿz4ChunkedCorpusReader.tagged_sents.<locals>.<listcomp>Tr!   r,   r   r+   r   Útagged_sentss   s
    
üÿz ChunkedCorpusReader.tagged_sentsc                    s    t ‡ ‡fdd„ˆ  |d¡D ƒƒS )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,tag)`` tuples.
        :rtype: list(list(list(tuple(str,str))))
        c              	      s2   g | ]*\}}t ||d d d dgˆ j¢R dˆiŽ‘qS r)   r   r   r+   r   r   r    ‹   s   ýÿÿÿz4ChunkedCorpusReader.tagged_paras.<locals>.<listcomp>Tr!   r,   r   r+   r   Útagged_parasƒ   s
    
üÿz ChunkedCorpusReader.tagged_parasc                    s    t ‡ ‡fdd„ˆ  |d¡D ƒƒS )av  
        :return: the given file(s) as a list of tagged
            words and chunks.  Words are encoded as ``(word, tag)``
            tuples (if the corpus has tags) or word strings (if the
            corpus has no tags).  Chunks are encoded as depth-one
            trees over ``(word,tag)`` tuples or word strings.
        :rtype: list(tuple(str,str) and Tree)
        c              	      s2   g | ]*\}}t ||d ddd gˆ j¢R dˆiŽ‘qS r)   r   r   r+   r   r   r    �   s   ýÿÿÿz5ChunkedCorpusReader.chunked_words.<locals>.<listcomp>Tr!   r,   r   r+   r   Úchunked_words“   s
    	
üÿz!ChunkedCorpusReader.chunked_wordsc                    s    t ‡ ‡fdd„ˆ  |d¡D ƒƒS )a6  
        :return: the given file(s) as a list of
            sentences, each encoded as a shallow Tree.  The leaves
            of these trees are encoded as ``(word, tag)`` tuples (if
            the corpus has tags) or word strings (if the corpus has no
            tags).
        :rtype: list(Tree)
        c              	      s2   g | ]*\}}t ||d d dd gˆ j¢R dˆiŽ‘qS r)   r   r   r+   r   r   r    ¯   s   ýÿÿÿz5ChunkedCorpusReader.chunked_sents.<locals>.<listcomp>Tr!   r,   r   r+   r   Úchunked_sents¥   s
    	
üÿz!ChunkedCorpusReader.chunked_sentsc                    s    t ‡ ‡fdd„ˆ  |d¡D ƒƒS )ao  
        :return: the given file(s) as a list of
            paragraphs, each encoded as a list of sentences, which are
            in turn encoded as a shallow Tree.  The leaves of these
            trees are encoded as ``(word, tag)`` tuples (if the corpus
            has tags) or word strings (if the corpus has no tags).
        :rtype: list(list(Tree))
        c              	      s2   g | ]*\}}t ||d d d d gˆ j¢R dˆiŽ‘qS )r&   r*   r   r   r+   r   r   r    Á   s   ýÿÿÿz5ChunkedCorpusReader.chunked_paras.<locals>.<listcomp>Tr!   r,   r   r+   r   Úchunked_paras·   s
    	
üÿz!ChunkedCorpusReader.chunked_parasc                 C   s   dd„ t |ƒD ƒS )Nc                 S   s   g | ]}t |ƒ‘qS r   r   )r   Útr   r   r   r    Ê   ó    z3ChunkedCorpusReader._read_block.<locals>.<listcomp>)Úread_blankline_block)r   Ústreamr   r   r   Ú_read_blockÉ   s    zChunkedCorpusReader._read_block)N)N)N)NN)NN)NN)NN)NN)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ZRegexpTokenizerr5   r   r$   r'   r(   r-   r.   r/   r0   r1   r2   r7   r   r   r   r   r      s$   
÷









r   c                   @   s&   e Zd Zddd„Zdd„ Zdd„ ZdS )	r   Nc                 C   sJ   t j| ||d� || _|| _|| _|| _|| _|| _|	| _|
| _	|| _
d S )N)r   )ÚStreamBackedCorpusViewr   Ú_taggedÚ_group_by_sentÚ_group_by_paraÚ_chunkedÚ_str2chunktreeÚ_sent_tokenizerÚ_para_block_readerÚ_source_tagsetÚ_target_tagset)r   Zfileidr   ZtaggedZgroup_by_sentZgroup_by_paraÚchunkedr   r   r   Úsource_tagsetr*   r   r   r   r   Î   s    zChunkedCorpusView.__init__c                 C   s˜   g }|   |¡D ]„}g }| j |¡D ]R}| j|| j| jd�}| jsJ|  |¡}| jsX| 	¡ }| j
rj| |¡ q"| |¡ q"| jrˆ| |¡ q| |¡ q|S )N)rG   r*   )rC   rB   ÚtokenizerA   rD   rE   r=   Ú_untagr@   Zleavesr>   ÚappendÚextendr?   )r   r6   ÚblockZpara_strÚparaZsent_strÚsentr   r   r   Ú
read_blockç   s(    ý
zChunkedCorpusView.read_blockc                 C   sL   t |ƒD ]>\}}t|tƒr&|  |¡ qt|tƒr>|d ||< qtdƒ‚q|S )Nr   z"expected child to be Tree or tuple)Ú	enumerateÚ
isinstancer   rI   ÚtupleÚ
ValueError)r   ÚtreeÚiÚchildr   r   r   rI   	  s    


zChunkedCorpusView._untag)NN)r8   r9   r:   r   rO   rI   r   r   r   r   r   Í   s
     ô
"r   )r;   ÚcodecsÚos.pathÚosZnltkZ
nltk.chunkr   Znltk.corpus.reader.apiZ nltk.corpus.reader.bracket_parser   Znltk.corpus.reader.utilZnltk.tokenizeZ	nltk.treer   r   r   r<   r   r   r   r   r   Ú<module>	   s    4