





import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


snake_case__					:							List[Any]													=     logging.get_logger(__name__)

snake_case__					:							Any													=     {'vocab_file': 'sentencepiece.bpe.model'}

snake_case__					:							Union[str, Any]													=     {
    'vocab_file': {
        'moussaKam/mbarthez': 'https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model',
        'moussaKam/barthez': 'https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model',
        'moussaKam/barthez-orangesum-title': (
            'https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model'
        ),
    },
}

snake_case__					:							str													=     {
    'moussaKam/mbarthez': 1024,
    'moussaKam/barthez': 1024,
    'moussaKam/barthez-orangesum-title': 1024,
}

snake_case__					:							Union[str, Any]													=     '▁'





class A_					(			_lowerCamelCase   ):
   lowerCAmelCase__        = VOCAB_FILES_NAMES
   lowerCAmelCase__        = PRETRAINED_VOCAB_FILES_MAP
   lowerCAmelCase__        = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   lowerCAmelCase__        = ["""input_ids""", """attention_mask"""]




   def __init__(self	:Union[str, Any]    ,   _UpperCamelCase	:Union[str, Any]    ,   _UpperCamelCase	:List[Any]="<s>"    ,   _UpperCamelCase	:str="</s>"    ,   _UpperCamelCase	:List[str]="</s>"    ,   _UpperCamelCase	:List[str]="<s>"    ,   _UpperCamelCase	:Tuple="<unk>"    ,   _UpperCamelCase	:List[str]="<pad>"    ,   _UpperCamelCase	:List[Any]="<mask>"    ,   _UpperCamelCase	:Optional[Dict[str, Any]] = None    ,   **_UpperCamelCase	:str    ,   )->					None:
         # Mask token behave like a normal word, i.e. include the space before it
         __A          =				AddedToken(_UpperCamelCase    ,   lstrip=_UpperCamelCase    ,   rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase    ,   _UpperCamelCase ) else mask_token

         __A          =				{} if sp_model_kwargs is None else sp_model_kwargs

         super().__init__(
             bos_token=_UpperCamelCase    ,   eos_token=_UpperCamelCase    ,   unk_token=_UpperCamelCase    ,   sep_token=_UpperCamelCase    ,   cls_token=_UpperCamelCase    ,   pad_token=_UpperCamelCase    ,   mask_token=_UpperCamelCase    ,   sp_model_kwargs=self.sp_model_kwargs    ,   **_UpperCamelCase    ,   )

         __A          =				vocab_file
         __A          =				spm.SentencePieceProcessor(**self.sp_model_kwargs )
         self.sp_model.Load(str(_UpperCamelCase ) )

         __A          =				{'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3}

         __A          =				len(self.sp_model ) - 1
         __A          =				{v: k for k, v in self.fairseq_tokens_to_ids.items()}




   def _lowerCAmelCase      (self	:List[Any]    ,   _UpperCamelCase	:List[int]    ,   _UpperCamelCase	:Optional[List[int]] = None )->					List[int]:

         if token_ids_a is None:
               return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
         __A          =				[self.cls_token_id]
         __A          =				[self.sep_token_id]
         return cls + token_ids_a + sep + sep + token_ids_a + sep




   def _lowerCAmelCase      (self	:Optional[Any]    ,   _UpperCamelCase	:List[int]    ,   _UpperCamelCase	:Optional[List[int]] = None    ,   _UpperCamelCase	:bool = False )->					List[int]:
         if already_has_special_tokens:
               return super().get_special_tokens_mask(
                   token_ids_a=_UpperCamelCase    ,   token_ids_a=_UpperCamelCase    ,   already_has_special_tokens=_UpperCamelCase )

         if token_ids_a is None:
               return [1] + ([0] * len(_UpperCamelCase )) + [1]
         return [1] + ([0] * len(_UpperCamelCase )) + [1, 1] + ([0] * len(_UpperCamelCase )) + [1]




   def _lowerCAmelCase      (self	:Optional[int]    ,   _UpperCamelCase	:List[int]    ,   _UpperCamelCase	:Optional[List[int]] = None )->					List[int]:
         __A          =				[self.sep_token_id]
         __A          =				[self.cls_token_id]

         if token_ids_a is None:
               return len(cls + token_ids_a + sep ) * [0]
         return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]




   @property
   def _lowerCAmelCase      (self	:Tuple )->					Any:
         return len(self.sp_model )




   def _lowerCAmelCase      (self	:str )->					str:
         __A          =				{self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )}
         vocab.update(self.added_tokens_encoder )
         return vocab




   def _lowerCAmelCase      (self	:Any    ,   _UpperCamelCase	:str )->					List[str]:
         return self.sp_model.encode(_UpperCamelCase    ,   out_type=_UpperCamelCase )




   def _lowerCAmelCase      (self	:List[str]    ,   _UpperCamelCase	:Optional[Any] )->					List[Any]:
         if token in self.fairseq_tokens_to_ids:
               return self.fairseq_tokens_to_ids[token]
         __A          =				self.sp_model.PieceToId(_UpperCamelCase )

         return spm_id if spm_id else self.unk_token_id




   def _lowerCAmelCase      (self	:Dict    ,   _UpperCamelCase	:Optional[int] )->					Optional[Any]:
         if index in self.fairseq_ids_to_tokens:
               return self.fairseq_ids_to_tokens[index]
         return self.sp_model.IdToPiece(_UpperCamelCase )




   def _lowerCAmelCase      (self	:Union[str, Any]    ,   _UpperCamelCase	:str )->					List[str]:
         __A          =				[]
         __A          =				''''''
         __A          =				False
         for token in tokens:
               # make sure that special tokens are not decoded using sentencepiece model
               if token in self.all_special_tokens:
                     if not prev_is_special:
                           out_string += " "
                     out_string += self.sp_model.decode(_UpperCamelCase ) + token
                     __A          =				True
                     __A          =				[]
               else:
                     current_sub_tokens.append(_UpperCamelCase )
                     __A          =				False
         out_string += self.sp_model.decode(_UpperCamelCase )
         return out_string.strip()




   def __getstate__(self	:Any )->					Tuple:
         __A          =				self.__dict__.copy()
         __A          =				None
         return state




   def __setstate__(self	:Tuple    ,   _UpperCamelCase	:Optional[Any] )->					Optional[Any]:
         __A          =				d

         # for backward compatibility
         if not hasattr(self    ,   '''sp_model_kwargs''' ):
               __A          =				{}

         __A          =				spm.SentencePieceProcessor(**self.sp_model_kwargs )
         self.sp_model.Load(self.vocab_file )






   def _lowerCAmelCase      (self	:Any    ,   _UpperCamelCase	:str    ,   _UpperCamelCase	:Optional[str] = None )->					Tuple[str]:
         if not os.path.isdir(_UpperCamelCase ):
               logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" )
               return
         __A          =				os.path.join(
             _UpperCamelCase    ,   (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] )

         if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ):
               copyfile(self.vocab_file    ,   _UpperCamelCase )
         elif not os.path.isfile(self.vocab_file ):
               with open(_UpperCamelCase    ,   '''wb''' ) as fi:
                     __A          =				self.sp_model.serialized_model_proto()
                     fi.write(_UpperCamelCase )

         return (out_vocab_file,)












import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_bert import BertTokenizer


snake_case__					:							List[Any]													=     logging.get_logger(__name__)

snake_case__					:							Union[str, Any]													=     {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

snake_case__					:							int													=     {
    'vocab_file': {
        'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt',
        'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/vocab.txt',
        'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/vocab.txt',
        'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/vocab.txt',
        'bert-base-multilingual-uncased': (
            'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/vocab.txt'
        ),
        'bert-base-multilingual-cased': 'https://huggingface.co/bert-base-multilingual-cased/resolve/main/vocab.txt',
        'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/vocab.txt',
        'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/vocab.txt',
        'bert-large-uncased-whole-word-masking': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/vocab.txt'
        ),
        'bert-large-cased-whole-word-masking': (
            'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/vocab.txt'
        ),
        'bert-large-uncased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt'
        ),
        'bert-large-cased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt'
        ),
        'bert-base-cased-finetuned-mrpc': (
            'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/vocab.txt'
        ),
        'bert-base-german-dbmdz-cased': 'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/vocab.txt',
        'bert-base-german-dbmdz-uncased': (
            'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/vocab.txt'
        ),
        'TurkuNLP/bert-base-finnish-cased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/vocab.txt'
        ),
        'TurkuNLP/bert-base-finnish-uncased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/vocab.txt'
        ),
        'wietsedv/bert-base-dutch-cased': (
            'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/tokenizer.json',
        'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/tokenizer.json',
        'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/tokenizer.json',
        'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/tokenizer.json',
        'bert-base-multilingual-uncased': (
            'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/tokenizer.json'
        ),
        'bert-base-multilingual-cased': (
            'https://huggingface.co/bert-base-multilingual-cased/resolve/main/tokenizer.json'
        ),
        'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/tokenizer.json',
        'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/tokenizer.json',
        'bert-large-uncased-whole-word-masking': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/tokenizer.json'
        ),
        'bert-large-cased-whole-word-masking': (
            'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/tokenizer.json'
        ),
        'bert-large-uncased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json'
        ),
        'bert-large-cased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json'
        ),
        'bert-base-cased-finetuned-mrpc': (
            'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/tokenizer.json'
        ),
        'bert-base-german-dbmdz-cased': (
            'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/tokenizer.json'
        ),
        'bert-base-german-dbmdz-uncased': (
            'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/tokenizer.json'
        ),
        'TurkuNLP/bert-base-finnish-cased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/tokenizer.json'
        ),
        'TurkuNLP/bert-base-finnish-uncased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/tokenizer.json'
        ),
        'wietsedv/bert-base-dutch-cased': (
            'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/tokenizer.json'
        ),
    },
}

snake_case__					:							int													=     {
    'bert-base-uncased': 512,
    'bert-large-uncased': 512,
    'bert-base-cased': 512,
    'bert-large-cased': 512,
    'bert-base-multilingual-uncased': 512,
    'bert-base-multilingual-cased': 512,
    'bert-base-chinese': 512,
    'bert-base-german-cased': 512,
    'bert-large-uncased-whole-word-masking': 512,
    'bert-large-cased-whole-word-masking': 512,
    'bert-large-uncased-whole-word-masking-finetuned-squad': 512,
    'bert-large-cased-whole-word-masking-finetuned-squad': 512,
    'bert-base-cased-finetuned-mrpc': 512,
    'bert-base-german-dbmdz-cased': 512,
    'bert-base-german-dbmdz-uncased': 512,
    'TurkuNLP/bert-base-finnish-cased-v1': 512,
    'TurkuNLP/bert-base-finnish-uncased-v1': 512,
    'wietsedv/bert-base-dutch-cased': 512,
}

snake_case__					:							str													=     {
    'bert-base-uncased': {'do_lower_case': True},
    'bert-large-uncased': {'do_lower_case': True},
    'bert-base-cased': {'do_lower_case': False},
    'bert-large-cased': {'do_lower_case': False},
    'bert-base-multilingual-uncased': {'do_lower_case': True},
    'bert-base-multilingual-cased': {'do_lower_case': False},
    'bert-base-chinese': {'do_lower_case': False},
    'bert-base-german-cased': {'do_lower_case': False},
    'bert-large-uncased-whole-word-masking': {'do_lower_case': True},
    'bert-large-cased-whole-word-masking': {'do_lower_case': False},
    'bert-large-uncased-whole-word-masking-finetuned-squad': {'do_lower_case': True},
    'bert-large-cased-whole-word-masking-finetuned-squad': {'do_lower_case': False},
    'bert-base-cased-finetuned-mrpc': {'do_lower_case': False},
    'bert-base-german-dbmdz-cased': {'do_lower_case': False},
    'bert-base-german-dbmdz-uncased': {'do_lower_case': True},
    'TurkuNLP/bert-base-finnish-cased-v1': {'do_lower_case': False},
    'TurkuNLP/bert-base-finnish-uncased-v1': {'do_lower_case': True},
    'wietsedv/bert-base-dutch-cased': {'do_lower_case': False},
}





class A_					(			_lowerCamelCase   ):
   lowerCAmelCase__        = VOCAB_FILES_NAMES
   lowerCAmelCase__        = PRETRAINED_VOCAB_FILES_MAP
   lowerCAmelCase__        = PRETRAINED_INIT_CONFIGURATION
   lowerCAmelCase__        = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   lowerCAmelCase__        = BertTokenizer




   def __init__(self	:List[str]    ,   _UpperCamelCase	:List[str]=None    ,   _UpperCamelCase	:Optional[Any]=None    ,   _UpperCamelCase	:str=True    ,   _UpperCamelCase	:Optional[Any]="[UNK]"    ,   _UpperCamelCase	:Tuple="[SEP]"    ,   _UpperCamelCase	:List[Any]="[PAD]"    ,   _UpperCamelCase	:int="[CLS]"    ,   _UpperCamelCase	:Optional[int]="[MASK]"    ,   _UpperCamelCase	:Union[str, Any]=True    ,   _UpperCamelCase	:str=None    ,   **_UpperCamelCase	:List[str]    ,   )->					str:
         super().__init__(
             _UpperCamelCase    ,   tokenizer_file=_UpperCamelCase    ,   do_lower_case=_UpperCamelCase    ,   unk_token=_UpperCamelCase    ,   sep_token=_UpperCamelCase    ,   pad_token=_UpperCamelCase    ,   cls_token=_UpperCamelCase    ,   mask_token=_UpperCamelCase    ,   tokenize_chinese_chars=_UpperCamelCase    ,   strip_accents=_UpperCamelCase    ,   **_UpperCamelCase    ,   )

         __A          =				json.loads(self.backend_tokenizer.normalizer.__getstate__() )
         if (
             normalizer_state.get('''lowercase'''    ,   _UpperCamelCase ) != do_lower_case
             or normalizer_state.get('''strip_accents'''    ,   _UpperCamelCase ) != strip_accents
             or normalizer_state.get('''handle_chinese_chars'''    ,   _UpperCamelCase ) != tokenize_chinese_chars
         ):
               __A          =				getattr(_UpperCamelCase    ,   normalizer_state.pop('''type''' ) )
               __A          =				do_lower_case
               __A          =				strip_accents
               __A          =				tokenize_chinese_chars
               __A          =				normalizer_class(**_UpperCamelCase )

         __A          =				do_lower_case




   def _lowerCAmelCase      (self	:Any    ,   _UpperCamelCase	:int    ,   _UpperCamelCase	:List[str]=None )->					List[Any]:
         __A          =				[self.cls_token_id] + token_ids_a + [self.sep_token_id]

         if token_ids_a:
               output += token_ids_a + [self.sep_token_id]

         return output




   def _lowerCAmelCase      (self	:List[str]    ,   _UpperCamelCase	:List[int]    ,   _UpperCamelCase	:Optional[List[int]] = None )->					List[int]:
         __A          =				[self.sep_token_id]
         __A          =				[self.cls_token_id]
         if token_ids_a is None:
               return len(cls + token_ids_a + sep ) * [0]
         return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]






   def _lowerCAmelCase      (self	:Any    ,   _UpperCamelCase	:str    ,   _UpperCamelCase	:Optional[str] = None )->					Tuple[str]:
         __A          =				self._tokenizer.model.save(_UpperCamelCase    ,   name=_UpperCamelCase )
         return tuple(_UpperCamelCase )





