§
    'ê[f�?  ã                   óŽ   — d Z ddlZddlmZ ddlmZ ddlT ddlmZ ddl	m
Z
 ddlmZ  G d	„ d
e¦  «        Z G d„ de¦  «        ZdS )z‚
Corpus reader for corpora whose documents are xml files.

(note -- not named 'xml' to avoid conflicting w/ standard xml package)
é    N)ÚElementTree)ÚCorpusReader)Ú*)ÚSeekableUnicodeStreamReader)ÚElementWrapper)ÚWordPunctTokenizerc                   ó*   — e Zd ZdZdd„Zdd„Zdd„ZdS )	ÚXMLCorpusReadera  
    Corpus reader for corpora whose documents are xml files.

    Note that the ``XMLCorpusReader`` constructor does not take an
    ``encoding`` argument, because the unicode encoding is specified by
    the XML files themselves.  See the XML specs for more info.
    Fc                 ó@   — || _         t          j        | ||¦  «         d S ©N)Ú_wrap_etreer   Ú__init__)ÚselfÚrootÚfileidsÚ
wrap_etrees       úN/var/www/piapp/venv/lib/python3.11/site-packages/nltk/corpus/reader/xmldocs.pyr   zXMLCorpusReader.__init__!   s%   € Ø%ˆÔÝÔ˜d D¨'Ñ2Ô2Ð2Ð2Ð2ó    Nc                 ó’  — |€%t          | j        ¦  «        dk    r| j        d         }t          |t          ¦  «        st	          d¦  «        ‚|                      |¦  «                             ¦   «         5 }t          j        |¦  «         	                    ¦   «         }d d d ¦  «         n# 1 swxY w Y   | j
        rt          |¦  «        }|S )Né   r   z(Expected a single file identifier string)ÚlenÚ_fileidsÚ
isinstanceÚstrÚ	TypeErrorÚabspathÚopenr   ÚparseÚgetrootr   r   )r   ÚfileidÚfpÚelts       r   ÚxmlzXMLCorpusReader.xml%   sô   € àˆ>�c $¤-Ñ0Ô0°AÒ5Ð5Ø”] 1Ô%ˆFÝ˜&¥#Ñ&Ô&ð 	HÝÐFÑGÔGÐGà�\Š\˜&Ñ!Ô!×&Ò&Ñ(Ô(ð 	2¨BÝÔ# BÑ'Ô'×/Ò/Ñ1Ô1ˆCð	2ð 	2ð 	2ñ 	2ô 	2ð 	2ð 	2ð 	2ð 	2ð 	2ð 	2øøøð 	2ð 	2ð 	2ð 	2ð Ôð 	&Ý  Ñ%Ô%ˆCàˆ
s   Á3'B&Â&B*Â-B*c                 ó   — |                       |¦  «        }|                      |¦  «        }t          ¦   «         }	 |                     ¦   «         }n#  |                     ¦   «         }Y nxY wg }|D ]_}|j        }|�Tt          |t          ¦  «        r|                     |¦  «        }| 	                    |¦  «        }	| 
                    |	¦  «         Œ`|S )aE  
        Returns all of the words and punctuation symbols in the specified file
        that were in text nodes -- ie, tags are ignored. Like the xml() method,
        fileid can only specify one file.

        :return: the given file's text nodes as a list of words and punctuation symbols
        :rtype: list(str)
        )r#   Úencodingr   ÚgetiteratorÚiterÚtextr   ÚbytesÚdecodeÚtokenizeÚextend)
r   r    r"   r%   Úword_tokenizerÚiteratorÚoutÚnoder(   Útokss
             r   ÚwordszXMLCorpusReader.words4   sÐ   € ð �hŠh�vÑÔˆØ—=’= Ñ(Ô(ˆÝ+Ñ-Ô-ˆð	"Ø—’Ñ(Ô(ˆHˆHøð	"Ø—x’x‘z”zˆHˆHˆHøøøØˆàð 	!ð 	!ˆDØ”9ˆDØÐÝ˜d¥EÑ*Ô*ð 1ØŸ;š; xÑ0Ô0�DØ%×.Ò.¨tÑ4Ô4�Ø—
’
˜4Ñ Ô Ð øØˆ
s   ºA ÁA')Fr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r2   © r   r   r
   r
      sZ   € € € € € ðð ð3ð 3ð 3ð 3ðð ð ð ðð ð ð ð ð r   r
   c                   óò   — e Zd ZdZdZdZdd„Zd„ Zd„ Ze	 
                    de	j        e	j        z  ¦  «        Ze	 
                    d	¦  «        Ze	 
                    d
e	j        e	j        z  ¦  «        Zd„ Zdd„ZdS )ÚXMLCorpusViewam  
    A corpus view that selects out specified elements from an XML
    file, and provides a flat list-like interface for accessing them.
    (Note: ``XMLCorpusView`` is not used by ``XMLCorpusReader`` itself,
    but may be used by subclasses of ``XMLCorpusReader``.)

    Every XML corpus view has a "tag specification", indicating what
    XML elements should be included in the view; and each (non-nested)
    element that matches this specification corresponds to one item in
    the view.  Tag specifications are regular expressions over tag
    paths, where a tag path is a list of element tag names, separated
    by '/', indicating the ancestry of the element.  Some examples:

      - ``'foo'``: A top-level element whose tag is ``foo``.
      - ``'foo/bar'``: An element whose tag is ``bar`` and whose parent
        is a top-level element whose tag is ``foo``.
      - ``'.*/foo'``: An element whose tag is ``foo``, appearing anywhere
        in the xml tree.
      - ``'.*/(foo|bar)'``: An wlement whose tag is ``foo`` or ``bar``,
        appearing anywhere in the xml tree.

    The view items are generated from the selected XML elements via
    the method ``handle_elt()``.  By default, this method returns the
    element as-is (i.e., as an ElementTree object); but it can be
    overridden, either via subclassing or via the ``elt_handler``
    constructor parameter.
    Fi   Nc                 óÖ   — |r|| _         t                               |dz   ¦  «        | _        	 ddi| _        	 |                      |¦  «        }t                               | ||¬¦  «         dS )aW  
        Create a new corpus view based on a specified XML file.

        Note that the ``XMLCorpusView`` constructor does not take an
        ``encoding`` argument, because the unicode encoding is
        specified by the XML files themselves.

        :type tagspec: str
        :param tagspec: A tag specification, indicating what XML
            elements should be included in the view.  Each non-nested
            element that matches this specification corresponds to one
            item in the view.

        :param elt_handler: A function used to transform each element
            to a value for the view.  If no handler is specified, then
            ``self.handle_elt()`` is called, which returns the element
            as an ElementTree object.  The signature of elt_handler is::

                elt_handler(elt, tagspec) -> value
        z\Zr   r7   )r%   N)Ú
handle_eltÚreÚcompileÚ_tagspecÚ_tag_contextÚ_detect_encodingÚStreamBackedCorpusViewr   )r   r    ÚtagspecÚelt_handlerr%   s        r   r   zXMLCorpusView.__init__u   st   € ð* ð 	*Ø)ˆDŒOåŸ
š
 7¨U¡?Ñ3Ô3ˆŒØ9à ˜GˆÔð	ð
 ×(Ò(¨Ñ0Ô0ˆÝ×'Ò'¨¨f¸xÐ'ÑHÔHÐHÐHÐHr   c                 ó°  — t          |t          ¦  «        rW	 |                     ¦   «         }|                     ¦   «         }|                     ¦   «          nU# |                     ¦   «          w xY wt          |d¦  «        5 }|                     ¦   «         }d d d ¦  «         n# 1 swxY w Y   |                     t          j        ¦  «        rdS |                     t          j        ¦  «        rdS |                     t          j	        ¦  «        rdS |                     t          j
        ¦  «        rdS |                     t          j        ¦  «        rdS t                               d|¦  «        }|r'|                     d¦  «                             ¦   «         S t                               d	|¦  «        }|r'|                     d¦  «                             ¦   «         S dS )
NÚrbz	utf-16-bez	utf-16-lez	utf-32-bez	utf-32-lezutf-8s!   \s*<\?xml\b.*\bencoding="([^"]+)"r   s!   \s*<\?xml\b.*\bencoding='([^']+)')r   ÚPathPointerr   ÚreadlineÚcloseÚ
startswithÚcodecsÚBOM_UTF16_BEÚBOM_UTF16_LEÚBOM_UTF32_BEÚBOM_UTF32_LEÚBOM_UTF8r<   ÚmatchÚgroupr*   )r   r    ÚinfileÚsÚms        r   r@   zXMLCorpusView._detect_encoding™   sÇ  € Ý�f�kÑ*Ô*ð 	&ðØŸš™œ�Ø—O’OÑ%Ô%�à—’‘”��ø�—’‘”��øøøå�f˜dÑ#Ô#ð & vØ—O’OÑ%Ô%�ð&ð &ð &ñ &ô &ð &ð &ð &ð &ð &ð &øøøð &ð &ð &ð &à�<Š<�Ô+Ñ,Ô,ð 	Ø�;Ø�<Š<�Ô+Ñ,Ô,ð 	Ø�;Ø�<Š<�Ô+Ñ,Ô,ð 	Ø�;Ø�<Š<�Ô+Ñ,Ô,ð 	Ø�;Ø�<Š<�œÑ(Ô(ð 	Ø�7Ý�HŠHÐ:¸AÑ>Ô>ˆØð 	'Ø—7’7˜1‘:”:×$Ò$Ñ&Ô&Ð&Ý�HŠHÐ:¸AÑ>Ô>ˆØð 	'Ø—7’7˜1‘:”:×$Ò$Ñ&Ô&Ð&àˆws   —(A ÁA*Á=BÂB"Â%B"c                 ó   — |S )a  
        Convert an element into an appropriate value for inclusion in
        the view.  Unless overridden by a subclass or by the
        ``elt_handler`` constructor argument, this method simply
        returns ``elt``.

        :return: The view value corresponding to ``elt``.

        :type elt: ElementTree
        :param elt: The element that should be converted.

        :type context: str
        :param context: A string composed of element tags separated by
            forward slashes, indicating the XML context of the given
            element.  For example, the string ``'foo/bar/baz'``
            indicates that the element is a ``baz`` element whose
            parent is a ``bar`` element and whose grandparent is a
            top-level ``foo`` element.
        r7   )r   r"   Úcontexts      r   r;   zXMLCorpusView.handle_elt¶   s	   € ð( ˆ
r   a;  
        [^<]*
        (
          ((<!--.*?-->)                         |  # comment
           (<![CDATA[.*?]])                     |  # raw character data
           (<!DOCTYPE\s+[^\[]*(\[[^\]]*])?\s*>) |  # doctype decl
           (<[^!>][^>]*>))                         # tag or PI
          [^<]*)*
        \Zz<\s*(?:/\s*)?([^\s>]+)a6  
        # Include these so we can skip them:
        (?P<COMMENT>        <!--.*?-->                          )|
        (?P<CDATA>          <![CDATA[.*?]]>                     )|
        (?P<PI>             <\?.*?\?>                           )|
        (?P<DOCTYPE>        <!DOCTYPE\s+[^\[^>]*(\[[^\]]*])?\s*>)|
        # These are the ones we actually care about:
        (?P<EMPTY_ELT_TAG>  <\s*[^>/\?!\s][^>]*/\s*>            )|
        (?P<START_TAG>      <\s*[^>/\?!\s][^>]*>                )|
        (?P<END_TAG>        <\s*/[^>/\?!\s][^>]*>               )c                 ó~  — d}t          |t          ¦  «        r|                     ¦   «         }	 |                     | j        ¦  «        }||z  }| j                             |¦  «        r|S t                               d|¦  «         	                    d¦  «        dk    rd|                     ¦   «         t          |¦  «        t                               d|¦  «                             ¦   «         z
  z
  }t          d|z  ¦  «        ‚|st          d¦  «        ‚|                     d¦  «        }|dk    r“| j                             |d	|…         ¦  «        rqt          |t          ¦  «        r+|                     |¦  «         |                     |¦  «         n'|                     t          |¦  «        |z
   d
¦  «         |d	|…         S �Œ’)a{  
        Read a string from the given stream that does not contain any
        un-closed tags.  In particular, this function first reads a
        block from the stream of size ``self._BLOCK_SIZE``.  It then
        checks if that block contains an un-closed tag.  If it does,
        then this function either backtracks to the last '<', or reads
        another block.
        Ú Tz[<>]r   Ú>zUnexpected ">" near char %sz&Unexpected end of file: tag not closedÚ<Nr   )r   r   ÚtellÚreadÚ_BLOCK_SIZEÚ_VALID_XML_RErP   r<   ÚsearchrQ   r   ÚendÚ
ValueErrorÚrfindÚseekÚchar_seek_forward)r   ÚstreamÚfragmentÚstartposÚ	xml_blockÚposÚlast_open_brackets          r   Ú_read_xml_fragmentz XMLCorpusView._read_xml_fragmentñ   s»  € ð ˆå�fÕ9Ñ:Ô:ð 	%Ø—{’{‘}”}ˆHð	8àŸš DÔ$4Ñ5Ô5ˆIØ˜	Ñ!ˆHð Ô!×'Ò'¨Ñ1Ô1ð  Ø�õ �yŠy˜ Ñ*Ô*×0Ò0°Ñ3Ô3°sÒ:Ð:Ø—k’k‘m”mÝ˜‘M”M¥B§I¢I¨f°hÑ$?Ô$?×$CÒ$CÑ$EÔ$EÑEñ�õ !Ð!>ÀÑ!DÑEÔEÐEð ð KÝ Ð!IÑJÔJÐJð
 !)§¢¨sÑ 3Ô 3ÐØ  1Ò$Ð$ØÔ%×+Ò+¨HÐ5GÐ6GÐ5GÔ,HÑIÔIð 8Ý! &Õ*EÑFÔFð MØŸš HÑ-Ô-Ð-Ø×0Ò0Ð1BÑCÔCÐCÐCàŸš¥c¨(¡m¤mÐ6GÑ&GÐ$HÈ!ÑLÔLÐLØ#Ð$6Ð%6Ð$6Ô7Ð7ñ?	8r   c                 ó¨	  ‡— |€| j         }‰€| j        Št          | j                             |                     ¦   «         ¦  «        ¦  «        }|€J ‚g }d}d}d}|g k    s|��t          |t          ¦  «        r|                     ¦   «         }	|                      |¦  «        }
|
s|€�nËt          d¦  «        ‚| j
                             |
¦  «        D �]î}| j        rPt          d                     d                     |¦  «        dd…         |                     ¦   «         ¦  «        ¦  «         |                     d¦  «        r©| j                             |                     ¦   «         ¦  «                             d¦  «        }|                     |¦  «         |€Qt(                               |d                     |¦  «        ¦  «        r#|                     ¦   «         }t-          |¦  «        }�Œ|                     d	¦  «        rõ| j                             |                     ¦   «         ¦  «                             d¦  «        }|st          d
|z  ¦  «        ‚||d         k    rt          d|d         › d|› d�¦  «        ‚|�b|t-          |¦  «        k    rO||
||                     ¦   «         …         z  }|                     |d                     |¦  «        f¦  «         dx}}d}|                     ¦   «          �Œ"|                     d¦  «        r·| j                             |                     ¦   «         ¦  «                             d¦  «        }|€vt(                               |d                     |¦  «        dz   |z   ¦  «        rB|                     |                     ¦   «         d                     |¦  «        dz   |z   f¦  «         �Œð|�¦|g k    r||
|d…         z  }d}n�| j        rt          d¦  «         t          |t          ¦  «        r+|                     |	¦  «         |                     |¦  «         n'|                     t-          |
¦  «        |z
   d¦  «         |d|dz
  …         }dx}}d}|g k    �°|�­|                     ¦   «         }|| j        v r!t7          |¦  «        | j        |         k    sJ ‚nt7          |¦  «        | j        |<   ˆfd„|D ¦   «         S )z¼
        Read from ``stream`` until we find at least one element that
        matches ``tagspec``, and return the result of applying
        ``elt_handler`` to each element found.
        NrX   zUnexpected end of filez	{:>25} {}ú/iìÿÿÿÚ	START_TAGr   ÚEND_TAGzUnmatched tag </%s>éÿÿÿÿzUnmatched tag <z>...</rY   ÚEMPTY_ELT_TAGr   z/                                    (backtrack)c           
      óz   •— g | ]7\  }} ‰t          j        |                     d d¦  «        ¦  «        |¦  «        ‘Œ8S )ÚasciiÚxmlcharrefreplace)r   Ú
fromstringÚencode)Ú.0r"   rV   rC   s      €r   ú
<listcomp>z,XMLCorpusView.read_block.<locals>.<listcomp>‡  s[   ø€ ð 
ð 
ð 
ñ
 ��gð	 ˆKÝÔ& s§z¢z°'Ð;NÑ'OÔ'OÑPÔPØñô ð
ð 
ð 
r   )r>   r;   Úlistr?   Úgetr[   r   r   rk   ra   Ú
_XML_PIECEÚfinditerÚ_DEBUGÚprintÚformatÚjoinrQ   Ú_XML_TAG_NAMErP   Úappendr<   Ústartr   r`   Úpoprc   rd   Útuple)r   re   rB   rC   rV   ÚeltsÚ	elt_startÚ	elt_depthÚelt_textrg   Úxml_fragmentÚpieceÚnameri   s      `          r   Ú
read_blockzXMLCorpusView.read_block"  sÐ  ø€ ð ˆ?Ø”mˆGØÐØœ/ˆKõ �tÔ(×,Ò,¨V¯[ª[©]¬]Ñ;Ô;Ñ<Ô<ˆØÐ"Ð"Ð"àˆàˆ	Øˆ	Øˆà�bŠjˆj˜IÑ1Ý˜&Õ"=Ñ>Ô>ð )Ø!Ÿ;š;™=œ=�Ø×2Ò2°6Ñ:Ô:ˆLð  ð ?ØÐ$Ùå$Ð%=Ñ>Ô>Ð>ð œ×1Ò1°,Ñ?Ô?ð "Yñ "Y�Ø”;ð VÝ˜+×,Ò,¨S¯XªX°gÑ->Ô->¸s¸t¸tÔ-DÀeÇkÂkÁmÄmÑTÔTÑUÔUÐUà—;’;˜{Ñ+Ô+ð YØÔ-×3Ò3°E·K²K±M´MÑBÔB×HÒHÈÑKÔK�Dà—N’N 4Ñ(Ô(Ð(à Ð(ÝŸ8š8 G¨S¯XªX°gÑ->Ô->Ñ?Ô?ð 5Ø(-¯ª©¬˜IÝ(+¨G©¬˜Iùà—[’[ Ñ+Ô+ð YØÔ-×3Ò3°E·K²K±M´MÑBÔB×HÒHÈÑKÔK�Dà"ð GÝ(Ð)>ÀÑ)EÑFÔFÐFØ˜w rœ{Ò*Ð*Ý(Ð)U¸7À2¼;Ð)UÐ)UÈdÐ)UÐ)UÐ)UÑVÔVÐVà Ð,°½cÀ'¹l¼lÒ1JÐ1JØ  L°¸U¿YºY¹[¼[Ð1HÔ$IÑI˜ØŸš X¨s¯xªx¸Ñ/@Ô/@Ð$AÑBÔBÐBØ04Ð4˜	 IØ#%˜à—K’K‘M”M�M‘Mà—[’[ Ñ1Ô1ð YØÔ-×3Ò3°E·K²K±M´MÑBÔB×HÒHÈÑKÔK�DØ Ð(ÝŸ8š8 G¨S¯XªX°gÑ->Ô->ÀÑ-DÀtÑ-KÑLÔLð YØ ŸKšK¨¯ª©¬¸¿ºÀÑ8IÔ8IÈCÑ8OÐRVÑ8VÐ(WÑXÔXÐXùàÐ$ð ˜2’:�:Ø ¨Y¨Z¨ZÔ 8Ñ8�HØ !�I�Ið ”{ð 8ÝÐ6Ñ7Ô7Ð7Ý! &Õ*EÑFÔFð IØŸš HÑ-Ô-Ð-Ø×0Ò0°Ñ;Ô;Ð;Ð;àŸš¥c¨,Ñ&7Ô&7¸)Ñ&CÐ$DÀaÑHÔHÐHØ% o¨	°A© oÔ6�GØ,0Ð0�I 	Ø!�HðO �bŠj‰j˜IÑ1ðT �kŠk‰mŒmˆØ�$Ô#Ð#Ð#Ý˜‘>”> TÔ%6°sÔ%;Ò;Ð;Ð;Ð;Ð;å%*¨7¡^¤^ˆDÔ˜cÑ"ð
ð 
ð 
ð 
ð
 #'ð
ñ 
ô 
ð 	
r   r   )NN)r3   r4   r5   r6   r}   r]   r   r@   r;   r<   r=   ÚDOTALLÚVERBOSEr^   r�   r{   rk   r�   r7   r   r   r9   r9   Q   sò   € € € € € ðð ð< €Fð €Kð"Ið "Ið "Ið "IðHð ð ð:ð ð ð0 —J’Jð	ð 	Œ	�B”JÑñô €Mð —J’JÐ8Ñ9Ô9€Mð —’ð		Eð 	Œ	�B”JÑñô €Jð,8ð ,8ð ,8ðbk
ð k
ð k
ð k
ð k
ð k
r   r9   )r6   rJ   Ú	xml.etreer   Únltk.corpus.reader.apir   Únltk.corpus.reader.utilÚ	nltk.datar   Únltk.internalsr   Únltk.tokenizer   r
   rA   r9   r7   r   r   ú<module>r–      sê   ððð ð €€€Ø !Ð !Ð !Ð !Ð !Ð !à /Ð /Ð /Ð /Ð /Ð /Ø %Ð %Ð %Ð %Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø )Ð )Ð )Ð )Ð )Ð )Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,ð6ð 6ð 6ð 6ð 6�lñ 6ô 6ð 6ðr|
ð |
ð |
ð |
ð |
Ð*ñ |
ô |
ð |
ð |
ð |
r   