
    sg$                         d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZmZ  e       rd	d
lmZ ndZ ej"                  e      ZdddZdZdZd	ZdZdZdZ G d de      Zy)z%Tokenization classes for XLNet model.    N)copyfile)ListOptionalTuple   )
AddedToken)PreTrainedTokenizerFast)is_sentencepiece_availablelogging   )XLNetTokenizerzspiece.modelztokenizer.json)
vocab_filetokenizer_fileu   ▁      c                        e Zd ZdZeZdZeZddddddddd	d
ddddgf fd	Z	e
defd       Z	 ddee   deee      dee   fdZ	 ddee   deee      dee   fdZddedee   dee   fdZ xZS )XLNetTokenizerFasta  
    Construct a "fast" XLNet tokenizer (backed by HuggingFace's *tokenizers* library). Based on
    [Unigram](https://huggingface.co/docs/tokenizers/python/latest/components.html?highlight=unigram#models).

    This tokenizer inherits from [`PreTrainedTokenizerFast`] which contains most of the main methods. Users should
    refer to this superclass for more information regarding those methods.

    Args:
        vocab_file (`str`):
            [SentencePiece](https://github.com/google/sentencepiece) file (generally has a .spm extension) that
            contains the vocabulary necessary to instantiate a tokenizer.
        do_lower_case (`bool`, *optional*, defaults to `True`):
            Whether to lowercase the input when tokenizing.
        remove_space (`bool`, *optional*, defaults to `True`):
            Whether to strip the text when tokenizing (removing excess spaces before and after the string).
        keep_accents (`bool`, *optional*, defaults to `False`):
            Whether to keep accents when tokenizing.
        bos_token (`str`, *optional*, defaults to `"<s>"`):
            The beginning of sequence token that was used during pretraining. Can be used a sequence classifier token.

            <Tip>

            When building a sequence using special tokens, this is not the token that is used for the beginning of
            sequence. The token used is the `cls_token`.

            </Tip>

        eos_token (`str`, *optional*, defaults to `"</s>"`):
            The end of sequence token.

            <Tip>

            When building a sequence using special tokens, this is not the token that is used for the end of sequence.
            The token used is the `sep_token`.

            </Tip>

        unk_token (`str`, *optional*, defaults to `"<unk>"`):
            The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
            token instead.
        sep_token (`str`, *optional*, defaults to `"<sep>"`):
            The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for
            sequence classification or for a text and a question for question answering. It is also used as the last
            token of a sequence built with special tokens.
        pad_token (`str`, *optional*, defaults to `"<pad>"`):
            The token used for padding, for example when batching sequences of different lengths.
        cls_token (`str`, *optional*, defaults to `"<cls>"`):
            The classifier token which is used when doing sequence classification (classification of the whole sequence
            instead of per-token classification). It is the first token of the sequence when built with special tokens.
        mask_token (`str`, *optional*, defaults to `"<mask>"`):
            The token used for masking values. This is the token used when training this model with masked language
            modeling. This is the token which the model will try to predict.
        additional_special_tokens (`List[str]`, *optional*, defaults to `["<eop>", "<eod>"]`):
            Additional special tokens used by the tokenizer.

    Attributes:
        sp_model (`SentencePieceProcessor`):
            The *SentencePiece* processor that is used for every conversion (string, tokens and IDs).
    leftNFTz<s>z</s>z<unk>z<sep>z<pad>z<cls>z<mask>z<eop>z<eod>c                     t        |t              rt        |dd      n|}t        |   d|||||||||	|
|||d| d| _        || _        || _        || _        || _	        y )NTF)lstriprstrip)r   r   do_lower_caseremove_spacekeep_accents	bos_token	eos_token	unk_token	sep_token	pad_token	cls_token
mask_tokenadditional_special_tokensr    )

isinstancestrr   super__init___pad_token_type_idr   r   r   r   )selfr   r   r   r   r   r   r   r   r   r   r    r!   r"   kwargs	__class__s                  d/var/www/html/venv/lib/python3.12/site-packages/transformers/models/xlnet/tokenization_xlnet_fast.pyr'   zXLNetTokenizerFast.__init__p   s    $ KUU_adJeZ
4Fku
 	
!)'%%!&?	
 	
" #$*(($    returnc                 p    | j                   r)t        j                  j                  | j                         S dS )NF)r   ospathisfile)r)   s    r,   can_save_slow_tokenizerz*XLNetTokenizerFast.can_save_slow_tokenizer   s$    26//rww~~doo.LuLr-   token_ids_0token_ids_1c                 f    | j                   g}| j                  g}|||z   |z   S ||z   |z   |z   |z   S )a  
        Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
        adding special tokens. An XLNet sequence has the following format:

        - single sequence: `X <sep> <cls>`
        - pair of sequences: `A <sep> B <sep> <cls>`

        Args:
            token_ids_0 (`List[int]`):
                List of IDs to which the special tokens will be added.
            token_ids_1 (`List[int]`, *optional*):
                Optional second list of IDs for sequence pairs.

        Returns:
            `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
        )sep_token_idcls_token_id)r)   r4   r5   sepclss        r,    build_inputs_with_special_tokensz3XLNetTokenizerFast.build_inputs_with_special_tokens   sP    &   !  !$s**S ;.4s::r-   c                     | j                   g}dg}|t        ||z         dgz  |z   S t        ||z         dgz  t        ||z         dgz  z   |z   S )a  
        Create a mask from the two sequences passed to be used in a sequence-pair classification task. An XLNet
        sequence pair mask has the following format:

        ```
        0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
        | first sequence    | second sequence |
        ```

        If `token_ids_1` is `None`, this method only returns the first portion of the mask (0s).

        Args:
            token_ids_0 (`List[int]`):
                List of IDs.
            token_ids_1 (`List[int]`, *optional*):
                Optional second list of IDs for sequence pairs.

        Returns:
            `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s).
        r   r   r   )r7   len)r)   r4   r5   r9   cls_segment_ids        r,   $create_token_type_ids_from_sequencesz7XLNetTokenizerFast.create_token_type_ids_from_sequences   sm    .   !{S()QC/.@@;$%+c+2C.Ds.JJ^[[r-   save_directoryfilename_prefixc                    | j                   st        d      t        j                  j	                  |      st
        j                  d| d       y t        j                  j                  ||r|dz   ndt        d   z         }t        j                  j                  | j                        t        j                  j                  |      k7  rt        | j                  |       |fS )NzhYour fast tokenizer does not have the necessary information to save the vocabulary for a slow tokenizer.zVocabulary path (z) should be a directory- r   )r3   
ValueErrorr0   r1   isdirloggererrorjoinVOCAB_FILES_NAMESabspathr   r   )r)   r@   rA   out_vocab_files       r,   save_vocabularyz"XLNetTokenizerFast.save_vocabulary   s    ++ 
 ww}}^,LL,^,<<STUo_s22QbcoQpp
 77??4??+rww~/NNT__n5  r-   )N)__name__
__module____qualname____doc__rJ   vocab_files_namespadding_sider   slow_tokenizer_classr'   propertyboolr3   r   intr   r;   r?   r%   r   rM   __classcell__)r+   s   @r,   r   r   /   s   :x *L) #*G"4)%V M M M JN;9;3;DI3F;	c;4 JN\9\3;DI3F\	c\<!c !HSM !]bcf]g !r-   r   )rQ   r0   shutilr   typingr   r   r   tokenization_utilsr   tokenization_utils_fastr	   utilsr
   r   tokenization_xlnetr   
get_loggerrN   rG   rJ   SPIECE_UNDERLINESEG_ID_ASEG_ID_B
SEG_ID_CLS
SEG_ID_SEP
SEG_ID_PADr   r#   r-   r,   <module>rf      s    , 	  ( ( , > 8 2N 
		H	%#1EUV    


x!0 x!r-   