§
    íóÉjU+  ã                   ó‚   — d Z ddlZdddddddd	œiZd
„ Zdd„Zdd„Z G d„ d¦  «        Zd„ Z e¦   «          dd„Zdd„Z	dS )ut  
HuggingFace datasets integration for NLTK.

Provides a PathPointer subclass that reads directly from the HuggingFace
datasets cache, and a ``download()`` function that populates that cache.

Usage::

    import nltk
    nltk.download('stopwords', hf=True)            # download to HF cache
    nltk.corpus.stopwords.words('portuguese')      # HF fallback if not in ~/nltk_data
    nltk.corpus.stopwords.words('portuguese', hf=True)  # HF cache directly

Registry schema
---------------
Each entry in ``REGISTRY`` must declare:

``repo`` (str)
    HuggingFace dataset repo id, e.g. ``"nltk-data-hub/stopwords"``.

``split`` (str)
    HF split name to load, e.g. ``"stopwords"``, ``"train"``.

``structure`` (str)
    How the corpus is organised on HF:

    ``"multi_config"``
        One HF config per NLTK fileid.  No assumption about what that
        dimension represents (language, category, author, etc.).
        ``fileid`` â†’ config name.

    ``"flat"``
        Single config, flat table.  A ``fileid_column`` value is used
        to select rows for a given fileid.

    ``"single"``
        Single config, no sub-selection.  The whole split is the corpus.

``content_type`` (str)
    How rows are serialised to the byte/text stream NLTK readers expect:

    ``"word_list"``
        Each row is one entry; ``text_column`` holds the string.
        Serialised as one entry per line.

    ``"raw_text"``
        Rows have a ``text_column`` with full document text.  When a
        fileid is given, rows are filtered by ``fileid_column``.
        Serialised as the raw text string.

    (Add new types here as more corpora are onboarded.)

``cache_probe`` (str)
    A single parquet path inside the repo used to detect whether the
    corpus has already been downloaded locally.  No network request is
    made; ``huggingface_hub.try_to_load_from_cache`` inspects the local
    filesystem only.

Optional keys (required by certain content types):

``text_column``    column that holds the main text / word value.
``fileid_column``  column that identifies which NLTK fileid a row belongs to.
``label_column``   column for classification labels (future use).
é    NÚ	stopwordsznltk-data-hub/stopwordsÚmulti_configÚ	word_listÚwordzdata/english/stopwords.parquet)ÚrepoÚsplitÚ	structureÚcontent_typeÚtext_columnÚcache_probec                 ó¶   — t                                | ¦  «        }|€dS 	 ddlm}  ||d         |d         d¬¦  «        }|duo|d	k    S # t          $ r Y dS w xY w)
zHReturn True if the corpus parquet exists in the local HF datasets cache.NFr   )Útry_to_load_from_cacher   r   Údataset)Úrepo_idÚfilenameÚ	repo_typeÚno_connection)ÚREGISTRYÚgetÚhuggingface_hubr   Ú	Exception)Ú	corpus_idÚinfor   Úresults       ú{/home/lighthouse/badge-speaker-benchmark/.benchmark-venv-community/lib/python3.11/site-packages/nltk/huggingface/dataset.pyÚ
_is_cachedr   _   s–   € å�<Š<˜	Ñ"Ô"€DØ€|Øˆuð
Ø:Ð:Ð:Ð:Ð:Ð:à'Ð'Ø˜”LØ˜-Ô(Øð
ñ 
ô 
ˆð
 ˜TÐ!Ð? f°Ò&?Ð?øÝð ð ð Øˆuˆuðøøøs    )A
 Á

AÁAc                 ó6  — |                      dd¦  «        }|dk    r!d                     | |d                  ¦  «        S |dk    rD|d         }| |         }t          |¦  «        dk    rd                     |¦  «        n|r|d         ndS t          d	|›d
�¦  «        ‚)uz  
    Convert an HF dataset ``ds`` to the byte/text content that an NLTK
    corpus reader would find in a plain file.

    :param ds: ``datasets.Dataset`` already filtered/selected for this corpus.
    :param info: REGISTRY entry for the corpus.
    :param fileid: the NLTK fileid being requested (used by some types).
    :returns: str â€” file content as NLTK expects it.
    r
   Úraw_textr   ú
r   é   r   Ú zcontent_type=zL is not implemented. Add a handler in nltk.huggingface.dataset._serialise().)r   ÚjoinÚlenÚNotImplementedError)Údsr   Úfileidr
   ÚcolÚtextss         r   Ú
_serialiser)   v   sº   € ð —8’8˜N¨JÑ7Ô7€Là�{Ò"Ð"Ø�yŠy˜˜D Ô/Ô0Ñ1Ô1Ð1à�zÒ!Ð!Ø�=Ô!ˆØ�3”ˆÝ#& u¡:¤:°¢> >ˆt�yŠy˜ÑÔÐÀEÐ8Q¸¸a¼¸ÈrÐRå
ð	B˜ð 	Bð 	Bð 	Bñô ð ó    c                 óZ  ‡‡— ddl m} |                      dd¦  «        }|dk    r+‰€t          d¦  «        ‚ || d         ‰| d	         ¬
¦  «        S |dk    r> || d         | d	         ¬
¦  «        }‰�!| d         Š|                     ˆˆfd„¦  «        }|S  || d         | d	         ¬
¦  «        S )zä
    Load the appropriate HF dataset slice for *fileid*, respecting structure.

    :param info: REGISTRY entry.
    :param fileid: NLTK fileid (may be None for single-structure corpora).
    :returns: ``datasets.Dataset``.
    r   ©Úload_datasetr	   Úsingler   Nz`fileid is required for multi_config corpora. Pass the config name (e.g. a language or category).r   r   ©r   ÚflatÚfileid_columnc                 ó   •— | ‰         ‰k    S ©N© )Úrowr'   r&   s    €€r   ú<lambda>z"_load_hf_dataset.<locals>.<lambda>¨   s   ø€  s¨3¤x°6Ò'9€ r*   )Údatasetsr-   r   Ú
ValueErrorÚfilter)r   r&   r-   r	   r%   r'   s    `   @r   Ú_load_hf_datasetr:   �   sí   øø€ ð &Ð%Ð%Ð%Ð%Ð%à—’˜ hÑ/Ô/€Ià�NÒ"Ð"Øˆ>ÝðFñô ð ð ˆ|˜D œL¨&¸¸W¼ÐFÑFÔFÐFà�FÒÐØˆ\˜$˜vœ,¨d°7¬mÐ<Ñ<Ô<ˆØÐØ�Ô'ˆCØ—’Ð9Ð9Ð9Ð9Ð9Ñ:Ô:ˆBØˆ	ð ˆ<˜˜Vœ¨D°¬MÐ:Ñ:Ô:Ð:r*   c                   óV   — e Zd ZdZdd„Zdd„Zd„ Zd„ Zd„ Ze	d„ ¦   «         Z
d	„ Zd
„ ZdS )ÚHFDatasetPathPointeraK  
    A ``PathPointer``-compatible object backed by a HuggingFace dataset
    stored in the local HF datasets cache (~/.cache/huggingface/datasets/).

    Satisfies the NLTK PathPointer interface (``open`` / ``file_size`` /
    ``join``) so that existing corpus readers work unchanged after
    ``nltk.download(..., hf=True)``.
    Nc                 ó"   — || _         || _        d S r3   ©r   r&   )Úselfr   r&   s      r   Ú__init__zHFDatasetPathPointer.__init__¾   s   € Ø"ˆŒØˆŒˆˆr*   c                 óú   — t           | j                 }t          || j        ¬¦  «        }t	          ||| j        ¬¦  «        }|rt          j        |¦  «        S t          j        |                     d¦  «        ¦  «        S )z>Return a stream of file content as NLTK corpus readers expect.©r&   zutf-8)	r   r   r:   r&   r)   ÚioÚStringIOÚBytesIOÚencode)r?   Úencodingr   r%   Úcontents        r   ÚopenzHFDatasetPathPointer.openÄ   sl   € å˜œÔ'ˆÝ˜d¨4¬;Ð7Ñ7Ô7ˆÝ˜R ¨d¬kÐ:Ñ:Ô:ˆØð 	(Ý”;˜wÑ'Ô'Ð'ÝŒz˜'Ÿ.š.¨Ñ1Ô1Ñ2Ô2Ð2r*   c                 ó   — dS )Nr   r4   ©r?   s    r   Ú	file_sizezHFDatasetPathPointer.file_sizeÍ   s   € Øˆqr*   c                 ó,   — t          | j        |¦  «        S r3   )r<   r   )r?   r&   s     r   r"   zHFDatasetPathPointer.joinÐ   s   € Ý# D¤N°FÑ;Ô;Ð;r*   c                 óÂ  — t                                | j        ¦  «        }|�t          | j        ¦  «        sg S |                     dd¦  «        }	 |dk    r$ddlm} t           ||d         ¦  «        ¦  «        S |dk    rGdd	lm}  ||d         |d
         ¬¦  «        }t          |                     |d         ¦  «        ¦  «        S |d
         gS # t          $ r g cY S w xY w)z8Return sorted list of fileids available for this corpus.Nr	   r.   r   r   ©Úget_dataset_config_namesr   r0   r,   r   r/   r1   )
r   r   r   r   r7   rP   Úsortedr-   Úuniquer   )r?   r   r	   rP   r-   r%   s         r   ÚfileidszHFDatasetPathPointer.fileidsÕ   s  € å�|Š|˜DœNÑ+Ô+ˆØˆ<�z¨$¬.Ñ9Ô9ˆ<ØˆIØ—H’H˜[¨(Ñ3Ô3ˆ	ð	Ø˜NÒ*Ð*Ø=Ð=Ð=Ð=Ð=Ð=åÐ6Ð6°t¸F´|ÑDÔDÑEÔEÐEØ˜FÒ"Ð"Ø1Ð1Ð1Ð1Ð1Ð1à!�\ $ v¤,°d¸7´mÐDÑDÔD�Ý˜bŸiši¨¨_Ô(=Ñ>Ô>Ñ?Ô?Ð?Ø˜”M�?Ð"øÝð 	ð 	ð 	ØˆIˆIˆIð	øøøs   Á)C Á9AC ÃC ÃCÃCc                 ó~   — t                                | j        i ¦  «                             d| j        ¦  «        }d|› �S )Nr   zhf://)r   r   r   )r?   r   s     r   ÚpathzHFDatasetPathPointer.pathë   s5   € å�|Š|˜DœN¨BÑ/Ô/×3Ò3°F¸D¼NÑKÔKˆØ�tˆ~ˆ~Ðr*   c                 ó@   — | j         r| j        › d| j         › �n| j        S )Nú/)r&   rU   rK   s    r   Ú__str__zHFDatasetPathPointer.__str__ð   s(   € Ø/3¬{ÐI�$”)Ð+Ð+˜dœkÐ+Ð+Ð+ÀÄ	ÐIr*   c                 ó(   — d| j         ›d| j        ›d�S )NzHFDatasetPathPointer(z, ú)r>   rK   s    r   Ú__repr__zHFDatasetPathPointer.__repr__ó   s   € ØK t¤~ÐKÐK¸4¼;ÐKÐKÐKÐKr*   r3   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r@   rI   rL   r"   rS   ÚpropertyrU   rX   r[   r4   r*   r   r<   r<   ´   s´   € € € € € ðð ðð ð ð ð3ð 3ð 3ð 3ðð ð ð<ð <ð <ð
ð ð ð, ðð ñ „XððJð Jð JðLð Lð Lð Lð Lr*   r<   c                  ój   — 	 ddl m}  |                      t          ¦  «         d S # t          $ r Y d S w xY w)Nr   ©ÚPathPointer)Ú	nltk.datarc   Úregisterr<   r   rb   s    r   Ú_register_path_pointerrf   ø   sV   € ðØ)Ð)Ð)Ð)Ð)Ð)à×ÒÕ1Ñ2Ô2Ð2Ð2Ð2øÝð ð ð Øˆˆðøøøs   ‚ $ ¤
2±2Fc                 ó~  ‡	‡
‡— t                                | ¦  «        Š	‰	€'t          d| ›dt          t           ¦  «        › �¦  «        ‚ddlmŠ |rd|ini Š
‰	                     dd¦  «        }|d	k    r†dd
lm}  |‰	d         ¦  «        }ˆ	ˆ
ˆfd„|D ¦   «         }|s[t          d„ |                     ¦   «         D ¦   «         ¦  «        }t          d| › d‰	d         › dt          |¦  «        › d|d›d�	¦  «         |S  ‰‰	d         fd‰	d         i‰
¤Ž}|s-t          d| › d‰	d         › dt          |¦  «        d›d�¦  «         |S )af  
    Download an NLTK corpus from HuggingFace into the HF datasets cache
    (``~/.cache/huggingface/datasets/``).

    :param corpus_id: NLTK corpus id, e.g. ``'stopwords'``.
    :param token: optional HuggingFace API token for private repos.
    :param quiet: suppress progress output.
    :raises ValueError: if *corpus_id* is not in the HF registry.
    NúCorpus z- is not available on HuggingFace.
Available: r   r,   Útokenr	   r.   r   rO   r   c           	      óB   •— i | ]}| ‰‰d          |fd‰d         i‰¤Ž“ŒS )r   r   r4   )Ú.0Úcfgr   Úkwargsr-   s     €€€r   ú
<dictcomp>zdownload.<locals>.<dictcomp>#  sL   ø€ ð 
ð 
ð 
àð ��˜d 6œl¨CÐOÐO°t¸G´}ÐOÈÐOÐOð
ð 
ð 
r*   c              3   ó4   K  — | ]}t          |¦  «        V — Œd S r3   )r#   )rk   Úds     r   ú	<genexpr>zdownload.<locals>.<genexpr>(  s(   è è € Ð8Ð8 1�˜A™œÐ8Ð8Ð8Ð8Ð8Ð8r*   z[nltk_hf] 'z' downloaded from z (z
 configs, ú,z rows)r   )r   r   r8   rQ   r7   r-   rP   ÚsumÚvaluesÚprintr#   )r   ri   Úquietr	   rP   Úconfigsr   Útotalr%   r   rm   r-   s            @@@r   Údownloadry   	  s  øøø€ õ �<Š<˜	Ñ"Ô"€DØ€|Ýð-�ið -ð -Ý ¥Ñ*Ô*ð-ð -ñ
ô 
ð 	
ð
 &Ð%Ð%Ð%Ð%Ð%à!&Ð.ˆg�uÐÐ¨B€FØ—’˜ hÑ/Ô/€Ià�NÒ"Ð"Ø5Ð5Ð5Ð5Ð5Ð5à*Ð*¨4°¬<Ñ8Ô8ˆð
ð 
ð 
ð 
ð 
ð 
àð
ñ 
ô 
ˆð ð 	ÝÐ8Ð8¨¯ª©¬Ð8Ñ8Ô8Ñ8Ô8ˆEÝð<˜ið <ð <¸4À¼<ð <ð <Ý˜‘L”Lð<ð <Ø,1Ð;ð<ð <ð <ñô ð ð ˆð ˆ\˜$˜vœ,ÐFÐF¨d°7¬mÐF¸vÐFÐFˆØð 	Ýð&˜ið &ð &¸4À¼<ð &ð &Ý˜‘G”GÐ%ð&ð &ð &ñô ð ð ˆ	r*   c                 óð   — t                                | ¦  «        }|€t          d| ›d�¦  «        ‚t          | ¦  «        st          d| ›d| ›d�¦  «        ‚t	          ||¬¦  «        }t          |||¬¦  «        S )u}  
    Load data for *corpus_id* directly from the HF datasets cache and return
    it as a string in the format NLTK corpus readers expect.

    :param corpus_id: NLTK corpus id, e.g. ``'stopwords'``.
    :param fileid: sub-resource identifier (config name, category, fileid, â€¦).
    :returns: str.
    :raises LookupError: if the corpus is not in the registry or not cached.
    Nrh   z) is not in the HuggingFace NLTK registry.z4 not found in HF datasets cache. Run: nltk.download(z
, hf=True)rB   )r   r   ÚLookupErrorr   r:   r)   )r   r&   r   r%   s       r   Ú	load_datar|   9  sª   € õ �<Š<˜	Ñ"Ô"€DØ€|ÝØL�iÐLÐLÐLñ
ô 
ð 	
õ �iÑ Ô ð 
Ýð:�ið :ð :Ø"+ð:ð :ð :ñ
ô 
ð 	
õ 
˜$ vÐ	.Ñ	.Ô	.€BÝ�b˜$ vÐ.Ñ.Ô.Ð.r*   r3   )NF)
r_   rC   r   r   r)   r:   r<   rf   ry   r|   r4   r*   r   ú<module>r}      s  ðð?ð ?ðB 
€	€	€	ð Ø)ØØ#Ø#ØØ7ðð ð	€ð"ð ð ð.ð ð ð ð4;ð ;ð ;ð ;ðH@Lð @Lð @Lð @Lð @Lñ @Lô @Lð @LðHð ð ð Ð Ñ Ô Ð ð-ð -ð -ð -ð`/ð /ð /ð /ð /ð /r*   