U
    4Af                     @   sx   d Z ddl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 dd	lmZ eeZd
ddZG dd de	ZdS )z#Fast Tokenization classes for Bert.    N)ListOptionalTuple)normalizers   )PreTrainedTokenizerFast)logging   )BertTokenizerz	vocab.txtztokenizer.json)
vocab_filetokenizer_filec                
       st   e Zd ZdZeZeZd fd	d
	Z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 )BertTokenizerFasta9
  
    Construct a "fast" BERT tokenizer (backed by HuggingFace's *tokenizers* library). Based on WordPiece.

    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`):
            File containing the vocabulary.
        do_lower_case (`bool`, *optional*, defaults to `True`):
            Whether or not to lowercase the input when tokenizing.
        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.
        clean_text (`bool`, *optional*, defaults to `True`):
            Whether or not to clean the text before tokenization by removing any control characters and replacing all
            whitespaces by the classic one.
        tokenize_chinese_chars (`bool`, *optional*, defaults to `True`):
            Whether or not to tokenize Chinese characters. This should likely be deactivated for Japanese (see [this
            issue](https://github.com/huggingface/transformers/issues/328)).
        strip_accents (`bool`, *optional*):
            Whether or not to strip all accents. If this option is not specified, then it will be determined by the
            value for `lowercase` (as in the original BERT).
        wordpieces_prefix (`str`, *optional*, defaults to `"##"`):
            The prefix for subwords.
    NT[UNK][SEP][PAD][CLS][MASK]c                    s   t  j|f||||||||	|
d	| t| jj }|d||ksj|d|
|
ksj|d|	|	krtt	|
d}||d< |
|d< |	|d< |f || j_|| _d S )N)	r   do_lower_case	unk_token	sep_token	pad_token	cls_token
mask_tokentokenize_chinese_charsstrip_accentsZ	lowercaser   Zhandle_chinese_charstype)super__init__jsonloadsZbackend_tokenizerZ
normalizer__getstate__getgetattrr   popr   )selfr   r   r   r   r   r   r   r   r   r   kwargsZnormalizer_stateZnormalizer_class	__class__ S/tmp/pip-unpacked-wheel-zw5xktn0/transformers/models/bert/tokenization_bert_fast.pyr   K   s8    zBertTokenizerFast.__init__c                 C   s0   | j g| | jg }|dk	r,||| jg 7 }|S )a  
        Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
        adding special tokens. A BERT 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*):
                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$   token_ids_0token_ids_1outputr(   r(   r)    build_inputs_with_special_tokensu   s    z2BertTokenizerFast.build_inputs_with_special_tokens)r,   r-   returnc                 C   sV   | j g}| jg}|dkr.t|| | dg S t|| | dg t|| dg  S )a  
        Create a mask from the two sequences passed to be used in a sequence-pair classification task. A BERT 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).
        Nr   r	   )r+   r*   len)r$   r,   r-   sepclsr(   r(   r)   $create_token_type_ids_from_sequences   s
    z6BertTokenizerFast.create_token_type_ids_from_sequences)save_directoryfilename_prefixr0   c                 C   s   | j jj||d}t|S )N)name)
_tokenizermodelsavetuple)r$   r5   r6   filesr(   r(   r)   save_vocabulary   s    z!BertTokenizerFast.save_vocabulary)
NNTr   r   r   r   r   TN)N)N)N)__name__
__module____qualname____doc__VOCAB_FILES_NAMESZvocab_files_namesr
   Zslow_tokenizer_classr   r/   r   intr   r4   strr   r=   __classcell__r(   r(   r&   r)   r       s,   '          *
  
r   )rA   r   typingr   r   r   Z
tokenizersr   Ztokenization_utils_fastr   utilsr   Ztokenization_bertr
   Z
get_loggerr>   loggerrB   r   r(   r(   r(   r)   <module>   s   

