U
    4Af                     @   s   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 rhd	d
lmZ ndZeeZdddZdZG dd deZdS )z+Tokenization classes for the BARThez model.    N)copyfile)ListOptionalTuple   )
AddedToken)PreTrainedTokenizerFast)is_sentencepiece_availablelogging   )BarthezTokenizerzsentencepiece.bpe.modelztokenizer.json)
vocab_filetokenizer_fileu   ▁c                	       s   e Zd ZdZeZddgZeZd fd
d	Z	e
edddZdee eee  ee dddZdee eee  ee dddZdeee ee dddZ  ZS )BarthezTokenizerFasta
  
    Adapted from [`CamembertTokenizer`] and [`BartTokenizer`]. Construct a "fast" BARThez tokenizer. Based on
    [SentencePiece](https://github.com/google/sentencepiece).

    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.
        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>

        sep_token (`str`, *optional*, defaults to `"</s>"`):
            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.
        cls_token (`str`, *optional*, defaults to `"<s>"`):
            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.
        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.
        pad_token (`str`, *optional*, defaults to `"<pad>"`):
            The token used for padding, for example when batching sequences of different lengths.
        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 `["<s>NOTUSED", "</s>NOTUSED"]`):
            Additional special tokens used by the tokenizer.
    Z	input_idsZattention_maskN<s></s><unk><pad><mask>c
                    sL   t |	trt|	dddn|	}	t j|f||||||||	d|
 || _d S )NTF)lstriprstrip)r   	bos_token	eos_token	unk_token	sep_token	cls_token	pad_token
mask_token)
isinstancestrr   super__init__r   )selfr   r   r   r   r   r   r   r   r   kwargs	__class__ Y/tmp/pip-unpacked-wheel-zw5xktn0/transformers/models/barthez/tokenization_barthez_fast.pyr!   ^   s     
zBarthezTokenizerFast.__init__)returnc                 C   s   | j rtj| j S dS )NF)r   ospathisfile)r"   r&   r&   r'   can_save_slow_tokenizer}   s    z,BarthezTokenizerFast.can_save_slow_tokenizer)token_ids_0token_ids_1r(   c                 C   sD   |dkr| j g| | jg S | j g}| jg}|| | | | | S )a  
        Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
        adding special tokens. A BARThez sequence has the following format:

        - single sequence: `<s> X </s>`
        - pair of sequences: `<s> A </s></s> B </s>`

        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.
        N)cls_token_idsep_token_id)r"   r-   r.   clssepr&   r&   r'    build_inputs_with_special_tokens   s
    z5BarthezTokenizerFast.build_inputs_with_special_tokensc                 C   sP   | j g}| jg}|dkr.t|| | dg S t|| | | | | dg S )ap  
        Create a mask from the two sequences passed to be used in a sequence-pair classification task.

        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 zeros.
        Nr   )r0   r/   len)r"   r-   r.   r2   r1   r&   r&   r'   $create_token_type_ids_from_sequences   s
    z9BarthezTokenizerFast.create_token_type_ids_from_sequences)save_directoryfilename_prefixr(   c                 C   s~   | j stdtj|s0td| d d S tj||rD|d ndtd  }tj	| j
tj	|krxt| 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   )r,   
ValueErrorr)   r*   isdirloggererrorjoinVOCAB_FILES_NAMESabspathr   r   )r"   r6   r7   Zout_vocab_filer&   r&   r'   save_vocabulary   s     z$BarthezTokenizerFast.save_vocabulary)	NNr   r   r   r   r   r   r   )N)N)N)__name__
__module____qualname____doc__r?   Zvocab_files_namesZmodel_input_namesr   Zslow_tokenizer_classr!   propertyboolr,   r   intr   r3   r5   r   r   rA   __classcell__r&   r&   r$   r'   r   '   s:   2           
  
r   )rE   r)   shutilr   typingr   r   r   Ztokenization_utilsr   Ztokenization_utils_fastr   utilsr	   r
   Ztokenization_barthezr   Z
get_loggerrB   r<   r?   ZSPIECE_UNDERLINEr   r&   r&   r&   r'   <module>   s   

