a
     ˜ëjë$  ã                   @   sb   d Z ddlmZ ddlmZmZmZ G dd„ deƒZddd„ZG d	d
„ d
e	ƒZ
G dd„ deƒZdS )zACorpus reader for the XML version of the British National Corpus.é    )Úconcat)ÚElementTreeÚXMLCorpusReaderÚXMLCorpusViewc                   @   sT   e Zd Z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 )ÚBNCCorpusReadera7  Corpus reader for the XML version of the British National Corpus.

    For access to the complete XML data structure, use the ``xml()``
    method.  For access to simple word lists and tagged word lists, use
    ``words()``, ``sents()``, ``tagged_words()``, and ``tagged_sents()``.

    You can obtain the full version of the BNC corpus at
    https://www.ota.ox.ac.uk/desc/2554

    If you extracted the archive to a directory called `BNC`, then you can
    instantiate the reader as::

        BNCCorpusReader(root='BNC/Texts/', fileids=r'[A-K]/\w*/\w*\.xml')

    Tc                 C   s   t  | ||¡ || _d S ©N)r   Ú__init__Ú_lazy)ÚselfÚrootÚfileidsZlazy© r   úU/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/corpus/reader/bnc.pyr      s    zBNCCorpusReader.__init__NFc                 C   s   |   |dd||¡S )aT  
        :return: the given file(s) as a list of words
            and punctuation symbols.
        :rtype: list(str)

        :param strip_space: If true, then strip trailing spaces from
            word tokens.  Otherwise, leave the spaces on the tokens.
        :param stem: If true, then use word stems instead of word strings.
        FN©Ú_views©r
   r   Ústrip_spaceÚstemr   r   r   Úwords#   s    
zBNCCorpusReader.wordsc                 C   s   |rdnd}|   |d|||¡S )a   
        :return: the given file(s) as a list of tagged
            words and punctuation symbols, encoded as tuples
            ``(word,tag)``.
        :rtype: list(tuple(str,str))

        :param c5: If true, then the tags used will be the more detailed
            c5 tags.  Otherwise, the simplified tags will be used.
        :param strip_space: If true, then strip trailing spaces from
            word tokens.  Otherwise, leave the spaces on the tokens.
        :param stem: If true, then use word stems instead of word strings.
        Úc5ÚposFr   ©r
   r   r   r   r   Útagr   r   r   Útagged_words/   s    zBNCCorpusReader.tagged_wordsc                 C   s   |   |dd||¡S )aˆ  
        :return: the given file(s) as a list of
            sentences or utterances, each encoded as a list of word
            strings.
        :rtype: list(list(str))

        :param strip_space: If true, then strip trailing spaces from
            word tokens.  Otherwise, leave the spaces on the tokens.
        :param stem: If true, then use word stems instead of word strings.
        TNr   r   r   r   r   Úsents?   s    zBNCCorpusReader.sentsc                 C   s    |rdnd}| j |d|||d�S )a  
        :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)))

        :param c5: If true, then the tags used will be the more detailed
            c5 tags.  Otherwise, the simplified tags will be used.
        :param strip_space: If true, then strip trailing spaces from
            word tokens.  Otherwise, leave the spaces on the tokens.
        :param stem: If true, then use word stems instead of word strings.
        r   r   T)Úsentr   r   r   r   r   r   r   r   Útagged_sentsL   s    
ÿzBNCCorpusReader.tagged_sentsc                    s4   | j r
tn| j‰ t‡ ‡‡‡‡fdd„|  |¡D ƒƒS )zPA helper function that instantiates BNCWordViews or the list of words/sentences.c                    s   g | ]}ˆ |ˆˆˆˆƒ‘qS r   r   )Ú.0Úfileid©Úfr   r   r   r   r   r   Ú
<listcomp>a   s   ÿz*BNCCorpusReader._views.<locals>.<listcomp>)r	   ÚBNCWordViewÚ_wordsr   Zabspaths)r
   r   r   r   r   r   r   r   r   r   ]   s    þÿzBNCCorpusReader._viewsc              	   C   sØ   g }t  |¡ ¡ }| d¡D ]ª}g }	t|ƒD ]r}
|
j}|s>d}|sF|rN| ¡ }|r^|
 d|¡}|dkrv||
 d¡f}n|dkr”||
 d|
 d¡¡f}|	 |¡ q,|r¼| t	|j
d |	ƒ¡ q| |	¡ qd|vsÔJ ‚|S )aÐ  
        Helper used to implement the view methods -- returns a list of
        words or a list of sentences, optionally tagged.

        :param fileid: The name of the underlying file.
        :param bracket_sent: If true, include sentence bracketing.
        :param tag: The name of the tagset to use, or None for no tags.
        :param strip_space: If true, strip spaces from word tokens.
        :param stem: If true, then substitute stems for words.
        z.//sÚ Úhwr   r   ÚnN)r   ÚparseÚgetrootÚfindallÚ_all_xmlwords_inÚtextÚstripÚgetÚappendÚBNCSentenceÚattribÚextend)r
   r   Zbracket_sentr   r   r   ÚresultZxmldocZxmlsentr   ZxmlwordÚwordr   r   r   r#   g   s,    zBNCCorpusReader._words)T)NTF)NFTF)NTF)NFTF)NFFTF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r#   r   r   r   r   r      s   






r   Nc                 C   s:   |d u rg }| D ]$}|j dv r*| |¡ qt||ƒ q|S )N)ÚcÚw)r   r.   r*   )Úeltr2   Úchildr   r   r   r*   �   s    
r*   c                   @   s   e Zd ZdZdd„ ZdS )r/   z‹
    A list of words, augmented by an attribute ``num`` used to record
    the sentence identifier (the ``n`` attribute from the XML).
    c                 C   s   || _ t | |¡ d S r   )ÚnumÚlistr   )r
   r<   Úitemsr   r   r   r   ž   s    zBNCSentence.__init__N)r4   r5   r6   r7   r   r   r   r   r   r/   ˜   s   r/   c                   @   s@   e Zd ZdZh d£Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dS )r"   zN
    A stream backed corpus view specialized for use with the BNC corpus.
    >   ZpbÚshiftZunclearZalignZvocalÚpauseZgapÚeventc                 C   s|   |r
d}nd}|| _ || _|| _|| _d| _d| _d| _d| _t 	| ||¡ |  
¡  |  | jd| j¡ |  ¡  ddi| _dS )aG  
        :param fileid: The name of the underlying file.
        :param sent: If true, include sentence bracketing.
        :param tag: The name of the tagset to use, or None for no tags.
        :param strip_space: If true, strip spaces from word tokens.
        :param stem: If true, then substitute stems for words.
        z.*/sz.*/s/(.*/)?(c|w)Nz.*/teiHeader$r   r   )Ú_sentÚ_tagÚ_strip_spaceÚ_stemÚtitleÚauthorÚeditorÚrespsr   r   Ú_openZ
read_blockÚ_streamÚhandle_headerÚcloseZ_tag_context)r
   r   r   r   r   r   Ztagspecr   r   r   r   ¸   s     zBNCWordView.__init__c                 C   s”   |  d¡}|r$d dd„ |D ƒ¡| _|  d¡}|rHd dd„ |D ƒ¡| _|  d¡}|rld dd„ |D ƒ¡| _|  d	¡}|r�d
 dd„ |D ƒ¡| _d S )NztitleStmt/titleÚ
c                 s   s   | ]}|j  ¡ V  qd S r   ©r+   r,   )r   rF   r   r   r   Ú	<genexpr>Ü   ó    z,BNCWordView.handle_header.<locals>.<genexpr>ztitleStmt/authorc                 s   s   | ]}|j  ¡ V  qd S r   rO   )r   rG   r   r   r   rP   à   rQ   ztitleStmt/editorc                 s   s   | ]}|j  ¡ V  qd S r   rO   )r   rH   r   r   r   rP   ä   rQ   ztitleStmt/respStmtz

c                 s   s"   | ]}d   dd„ |D ƒ¡V  qdS )rN   c                 s   s   | ]}|j  ¡ V  qd S r   rO   )r   Zresp_eltr   r   r   rP   é   rQ   z6BNCWordView.handle_header.<locals>.<genexpr>.<genexpr>N)Újoin)r   Úrespr   r   r   rP   è   s   )r)   rR   rF   rG   rH   rI   )r
   r:   ÚcontextÚtitlesZauthorsZeditorsrI   r   r   r   rL   Ø   s    




ÿzBNCWordView.handle_headerc                 C   s   | j r|  |¡S |  |¡S d S r   )rB   Úhandle_sentÚhandle_word)r
   r:   rT   r   r   r   Ú
handle_eltì   s    
zBNCWordView.handle_eltc                 C   sr   |j }|sd}| js| jr"| ¡ }| jr4| d|¡}| jdkrN|| d¡f}n | jdkrn|| d| d¡¡f}|S )Nr$   r%   r   r   )r+   rD   rE   r,   r-   rC   )r
   r:   r3   r   r   r   rW   ò   s    

zBNCWordView.handle_wordc                    sv   g }|D ]\}|j dv r.|‡ fdd„|D ƒ7 }q|j dv rJ| ˆ  |¡¡ q|j ˆ jvrtd|j  ƒ‚qt|jd |ƒS )N)ÚmwÚhiZcorrÚtruncc                    s   g | ]}ˆ   |¡‘qS r   )rW   )r   r9   ©r
   r   r   r!     rQ   z+BNCWordView.handle_sent.<locals>.<listcomp>)r9   r8   zUnexpected element %sr&   )r   r.   rW   Útags_to_ignoreÚ
ValueErrorr/   r0   )r
   r:   r   r;   r   r\   r   rV      s    

zBNCWordView.handle_sentN)
r4   r5   r6   r7   r]   r   rL   rX   rW   rV   r   r   r   r   r"   £   s    r"   )N)r7   Znltk.corpus.reader.utilr   Znltk.corpus.reader.xmldocsr   r   r   r   r*   r=   r/   r"   r   r   r   r   Ú<module>   s   
