U
    5Af'                     @   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 RemBERT model.    N)copyfile)ListOptionalTuple   )
AddedToken)PreTrainedTokenizerFast)is_sentencepiece_availablelogging   )RemBertTokenizerzsentencepiece.modelztokenizer.json)
vocab_filetokenizer_fileu   ▁c                       s   e Zd ZdZe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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 )RemBertTokenizerFasta  
    Construct a "fast" RemBert 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 or not to lowercase the input when tokenizing.
        remove_space (`bool`, *optional*, defaults to `True`):
            Whether or not to strip the text when tokenizing (removing excess spaces before and after the string).
        keep_accents (`bool`, *optional*, defaults to `False`):
            Whether or not to keep accents when tokenizing.
        bos_token (`str`, *optional*, defaults to `"[CLS]"`):
            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 `"[SEP]"`):
            The end of sequence token. .. note:: 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`.
        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.
    NTF[CLS][SEP]<unk><pad>[MASK]c                    sd   t |trt|dddn|}t j|f||||||||	|
||d| || _|| _|| _|| _d S )NTF)lstriprstrip)r   do_lower_caseremove_spacekeep_accents	bos_token	eos_token	unk_token	sep_token	pad_token	cls_token
mask_token)	
isinstancestrr   super__init__r   r   r   r   )selfr   r   r   r   r   r   r   r   r   r   r   r    kwargs	__class__ Y/tmp/pip-unpacked-wheel-zw5xktn0/transformers/models/rembert/tokenization_rembert_fast.pyr$   X   s,    zRemBertTokenizerFast.__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,RemBertTokenizerFast.can_save_slow_tokenizer)token_ids_0token_ids_1r+   c                 C   s8   | j g}| jg}|dkr$|| | S || | | | S )a  
        Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
        adding special tokens. A RemBERT sequence has the following format:

        - single sequence: `[CLS] X [SEP]`
        - pair of sequences: `[CLS] A [SEP] B [SEP]`

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

        Returns:
            `List[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens.
        Nsep_token_idcls_token_idr%   r1   r2   sepclsr)   r)   r*    build_inputs_with_special_tokens   s
    z5RemBertTokenizerFast.build_inputs_with_special_tokens)r1   r2   already_has_special_tokensr+   c                    sv   |r&|dk	rt d fdd|D S |dk	r\dgdgt|  dg dgt|  dg S dgdgt|  dg S )a  
        Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
        special tokens using the tokenizer `prepare_for_model` method.

        Args:
            token_ids_0 (`List[int]`):
                List of ids.
            token_ids_1 (`List[int]`, *optional*, defaults to `None`):
                Optional second list of IDs for sequence pairs.
            already_has_special_tokens (`bool`, *optional*, defaults to `False`):
                Set to True if the token list is already formatted with special tokens for the model

        Returns:
            `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
        NzYou should not supply a second sequence if the provided sequence of ids is already formatted with special tokens for the model.c                    s$   g | ]}| j  jfkrd ndqS )r   r   r3   ).0xr/   r)   r*   
<listcomp>   s     z@RemBertTokenizerFast.get_special_tokens_mask.<locals>.<listcomp>r   r   )
ValueErrorlen)r%   r1   r2   r:   r)   r/   r*   get_special_tokens_mask   s    .z,RemBertTokenizerFast.get_special_tokens_maskc                 C   sV   | j g}| jg}|dkr.t|| | dg S t|| | dg t|| dg  S )a  
        Creates a mask from the two sequences passed to be used in a sequence-pair classification task. A RemBERT
        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, only returns the first portion of the mask (0s).

        Args:
            token_ids_0 (`List[int]`):
                List of ids.
            token_ids_1 (`List[int]`, *optional*, defaults to `None`):
                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).
        Nr   r   )r4   r5   r?   r6   r)   r)   r*   $create_token_type_ids_from_sequences   s
    z9RemBertTokenizerFast.create_token_type_ids_from_sequences)save_directoryfilename_prefixr+   c                 C   sn   t j|s td| d S t j||r4|d ndtd  }t j| j	t j|krht
| j	| |fS )Nz*Vocabulary path ({}) should be a directory- r   )r,   r-   isdirloggererrorformatjoinVOCAB_FILES_NAMESabspathr   r   )r%   rB   rC   Zout_vocab_filer)   r)   r*   save_vocabulary   s     z$RemBertTokenizerFast.save_vocabulary)NNTTFr   r   r   r   r   r   r   )N)NF)N)N)__name__
__module____qualname____doc__rK   Zvocab_files_namesr   Zslow_tokenizer_classr$   propertyboolr0   r   intr   r9   r@   rA   r"   r   rM   __classcell__r)   r)   r'   r*   r   &   sN   .            (  
    
    
r   )rQ   r,   shutilr   typingr   r   r   Ztokenization_utilsr   Ztokenization_utils_fastr   utilsr	   r
   Ztokenization_rembertr   Z
get_loggerrN   rG   rK   ZSPIECE_UNDERLINEr   r)   r)   r)   r*   <module>   s   

