





"""simple docstring"""





import json
import os
import unittest

from transformers import DebertaTokenizer, DebertaTokenizerFast
from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin






class    _lowerCAmelCase  (	a    ,  unittest.TestCase			):
       """simple docstring"""




       __magic_name__	:Optional[Any]						    =   DebertaTokenizer
       __magic_name__	:str						    =   True
       __magic_name__	:Dict						    =   DebertaTokenizerFast






       def 	snake_case		(       self ):



         '''simple docstring'''

         super().setUp()

         # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
         lowerCAmelCase__     :Dict											=				[
             'l',
             'o',
             'w',
             'e',
             'r',
             's',
             't',
             'i',
             'd',
             'n',
             '\u0120',
             '\u0120l',
             '\u0120n',
             '\u0120lo',
             '\u0120low',
             'er',
             '\u0120lowest',
             '\u0120newer',
             '\u0120wider',
             '[UNK]',
         ]
         lowerCAmelCase__     :Union[str, Any]											=				dict(zip(__UpperCAmelCase       ,					range(len(__UpperCAmelCase ) ) ) )
         lowerCAmelCase__     :Dict											=				['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', '']
         lowerCAmelCase__     :int											=				{'unk_token': '[UNK]'}

         lowerCAmelCase__     :List[Any]											=				os.path.join(self.tmpdirname       ,					VOCAB_FILES_NAMES['vocab_file'] )
         lowerCAmelCase__     :Union[str, Any]											=				os.path.join(self.tmpdirname       ,					VOCAB_FILES_NAMES['merges_file'] )
         with open(self.vocab_file       ,					'w'       ,					encoding='utf-8' ) as fp:
           fp.write(json.dumps(__UpperCAmelCase ) + '\n' )
         with open(self.merges_file       ,					'w'       ,					encoding='utf-8' ) as fp:
           fp.write('\n'.join(__UpperCAmelCase ) )






       def 	snake_case		(       self       ,					**__UpperCAmelCase ):



         '''simple docstring'''

         kwargs.update(self.special_tokens_map )
         return self.tokenizer_class.from_pretrained(self.tmpdirname       ,					**__UpperCAmelCase )






       def 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[Any]											=				'lower newer'
         lowerCAmelCase__     :List[str]											=				'lower newer'
         return input_text, output_text






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Dict											=				self.get_tokenizer()
         lowerCAmelCase__     :List[Any]											=				'lower newer'
         lowerCAmelCase__     :Optional[int]											=				['l', 'o', 'w', 'er', '\u0120', 'n', 'e', 'w', 'er']
         lowerCAmelCase__     :Tuple											=				tokenizer.tokenize(__UpperCAmelCase )
         self.assertListEqual(__UpperCAmelCase       ,					__UpperCAmelCase )

         lowerCAmelCase__     :Any											=				tokens + [tokenizer.unk_token]
         lowerCAmelCase__     :str											=				[0, 1, 2, 1_5, 1_0, 9, 3, 2, 1_5, 1_9]
         self.assertListEqual(tokenizer.convert_tokens_to_ids(__UpperCAmelCase )       ,					__UpperCAmelCase )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[Any]											=				self.get_tokenizer()
         lowerCAmelCase__     :Optional[int]											=				tokenizer('Hello'       ,					'World' )
         lowerCAmelCase__     :List[Any]											=				[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
         self.assertListEqual(tokd['token_type_ids']       ,					__UpperCAmelCase )






       @slow
       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Any											=				self.tokenizer_class.from_pretrained('microsoft/deberta-base' )

         lowerCAmelCase__     :Optional[Any]											=				tokenizer.encode('sequence builders'       ,					add_special_tokens=__UpperCAmelCase )
         lowerCAmelCase__     :str											=				tokenizer.encode('multi-sequence build'       ,					add_special_tokens=__UpperCAmelCase )

         lowerCAmelCase__     :List[Any]											=				tokenizer.encode(
             'sequence builders'       ,					add_special_tokens=__UpperCAmelCase       ,					add_prefix_space=__UpperCAmelCase )
         lowerCAmelCase__     :Any											=				tokenizer.encode(
             'sequence builders'       ,					'multi-sequence build'       ,					add_special_tokens=__UpperCAmelCase       ,					add_prefix_space=__UpperCAmelCase )

         lowerCAmelCase__     :Optional[int]											=				tokenizer.build_inputs_with_special_tokens(__UpperCAmelCase )
         lowerCAmelCase__     :str											=				tokenizer.build_inputs_with_special_tokens(__UpperCAmelCase       ,					__UpperCAmelCase )

         assert encoded_sentence == encoded_text_from_decode
         assert encoded_pair == encoded_pair_from_decode






       @slow
       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Any											=				[self.tokenizer_class]
         if self.test_rust_tokenizer:
           tokenizer_classes.append(self.rust_tokenizer_class )

         for tokenizer_class in tokenizer_classes:
           lowerCAmelCase__     :Tuple											=				tokenizer_class.from_pretrained('microsoft/deberta-base' )

           lowerCAmelCase__     :Optional[int]											=				[
               'ALBERT: A Lite BERT for Self-supervised Learning of Language Representations',
               'ALBERT incorporates two parameter reduction techniques',
               'The first one is a factorized embedding parameterization. By decomposing the large vocabulary'
               ' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of'
               ' vocabulary embedding.',
           ]

           lowerCAmelCase__     :Optional[int]											=				tokenizer(__UpperCAmelCase       ,					padding=__UpperCAmelCase )
           lowerCAmelCase__     :Tuple											=				[tokenizer.decode(__UpperCAmelCase       ,					skip_special_tokens=__UpperCAmelCase ) for seq in encoding['input_ids']]

           # fmt: off
           lowerCAmelCase__     :Tuple											=				{
               'input_ids': [
                   [1, 2_1_1_8, 1_1_1_2_6, 5_6_5, 3_5, 8_3, 2_5_1_9_1, 1_6_3, 1_8_8_5_4, 1_3, 1_2_1_5_6, 1_2, 1_6_1_0_1, 2_5_3_7_6, 1_3_8_0_7, 9, 2_2_2_0_5, 2_7_8_9_3, 1_6_3_5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [1, 2_1_1_8, 1_1_1_2_6, 5_6_5, 2_4_5_3_6, 8_0, 4_3_7_9_7, 4_8_7_8, 7_3_7_3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [1, 1_3_3, 7_8, 6_5, 1_6, 1_0, 3_7_2_4, 1_5_3_8, 3_3_1_8_3, 1_1_3_0_3, 4_3_7_9_7, 1_9_3_8, 4, 8_7_0, 2_4_1_6_5, 2_9_1_0_5, 5, 7_3_9, 3_2_6_4_4, 3_3_1_8_3, 1_1_3_0_3, 3_6_1_7_3, 8_8, 8_0, 6_5_0, 7_8_2_1, 4_5_9_4_0, 6, 5_2, 2_5_5_9, 5, 1_8_3_6, 9, 5, 7_3_9_7, 1_3_1_7_1, 3_1, 5, 1_8_3_6, 9, 3_2_6_4_4, 3_3_1_8_3, 1_1_3_0_3, 4, 2]
               ],
               'token_type_ids': [
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
               ],
               'attention_mask': [
                   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
               ]
           }
           # fmt: on

           lowerCAmelCase__     :List[Any]											=				[
               'ALBERT: A Lite BERT for Self-supervised Learning of Language Representations',
               'ALBERT incorporates two parameter reduction techniques',
               'The first one is a factorized embedding parameterization. By decomposing the large vocabulary'
               ' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of'
               ' vocabulary embedding.',
           ]

           self.assertDictEqual(encoding.data       ,					__UpperCAmelCase )

           for expected, decoded in zip(__UpperCAmelCase       ,					__UpperCAmelCase ):
             self.assertEqual(__UpperCAmelCase       ,					__UpperCAmelCase )







"""simple docstring"""





import json
from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_roberta import RobertaTokenizer


__A											=						logging.get_logger(__name__)

__A											=						{"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

__A											=						{
    """vocab_file""": {
        """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/vocab.json""",
        """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/vocab.json""",
        """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json""",
        """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/vocab.json""",
        """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json""",
        """roberta-large-openai-detector""": (
            """https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json"""
        ),
    },
    """merges_file""": {
        """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/merges.txt""",
        """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/merges.txt""",
        """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt""",
        """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/merges.txt""",
        """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt""",
        """roberta-large-openai-detector""": (
            """https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt"""
        ),
    },
    """tokenizer_file""": {
        """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/tokenizer.json""",
        """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/tokenizer.json""",
        """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json""",
        """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json""",
        """roberta-base-openai-detector""": (
            """https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json"""
        ),
        """roberta-large-openai-detector""": (
            """https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json"""
        ),
    },
}

__A											=						{
    """roberta-base""": 512,
    """roberta-large""": 512,
    """roberta-large-mnli""": 512,
    """distilroberta-base""": 512,
    """roberta-base-openai-detector""": 512,
    """roberta-large-openai-detector""": 512,
}






class    _lowerCAmelCase  (	a			):
       """simple docstring"""




       __magic_name__	:str						    =   VOCAB_FILES_NAMES
       __magic_name__	:List[Any]						    =   PRETRAINED_VOCAB_FILES_MAP
       __magic_name__	:Optional[Any]						    =   PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       __magic_name__	:str						    =   ["""input_ids""", """attention_mask"""]
       __magic_name__	:Any						    =   RobertaTokenizer






       def __init__(       self       ,					__UpperCAmelCase=None       ,					__UpperCAmelCase=None       ,					__UpperCAmelCase=None       ,					__UpperCAmelCase="replace"       ,					__UpperCAmelCase="<s>"       ,					__UpperCAmelCase="</s>"       ,					__UpperCAmelCase="</s>"       ,					__UpperCAmelCase="<s>"       ,					__UpperCAmelCase="<unk>"       ,					__UpperCAmelCase="<pad>"       ,					__UpperCAmelCase="<mask>"       ,					__UpperCAmelCase=False       ,					__UpperCAmelCase=True       ,					**__UpperCAmelCase       ,					):



         '''simple docstring'''

         super().__init__(
             __UpperCAmelCase       ,					__UpperCAmelCase       ,					tokenizer_file=__UpperCAmelCase       ,					errors=__UpperCAmelCase       ,					bos_token=__UpperCAmelCase       ,					eos_token=__UpperCAmelCase       ,					sep_token=__UpperCAmelCase       ,					cls_token=__UpperCAmelCase       ,					unk_token=__UpperCAmelCase       ,					pad_token=__UpperCAmelCase       ,					mask_token=__UpperCAmelCase       ,					add_prefix_space=__UpperCAmelCase       ,					trim_offsets=__UpperCAmelCase       ,					**__UpperCAmelCase       ,					)

         lowerCAmelCase__     :Dict											=				json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() )
         if pre_tok_state.get('add_prefix_space'       ,					__UpperCAmelCase ) != add_prefix_space:
           lowerCAmelCase__     :Optional[int]											=				getattr(__UpperCAmelCase       ,					pre_tok_state.pop('type' ) )
           lowerCAmelCase__     :List[Any]											=				add_prefix_space
           lowerCAmelCase__     :str											=				pre_tok_class(**__UpperCAmelCase )

         lowerCAmelCase__     :List[str]											=				add_prefix_space

         lowerCAmelCase__     :str											=				'post_processor'
         lowerCAmelCase__     :Optional[Any]											=				getattr(self.backend_tokenizer       ,					__UpperCAmelCase       ,					__UpperCAmelCase )
         if tokenizer_component_instance:
           lowerCAmelCase__     :Optional[Any]											=				json.loads(tokenizer_component_instance.__getstate__() )

           # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
           if "sep" in state:
             lowerCAmelCase__     :Any											=				tuple(state['sep'] )
           if "cls" in state:
             lowerCAmelCase__     :int											=				tuple(state['cls'] )

           lowerCAmelCase__     :List[Any]											=				False

           if state.get('add_prefix_space'       ,					__UpperCAmelCase ) != add_prefix_space:
             lowerCAmelCase__     :Union[str, Any]											=				add_prefix_space
             lowerCAmelCase__     :Any											=				True

           if state.get('trim_offsets'       ,					__UpperCAmelCase ) != trim_offsets:
             lowerCAmelCase__     :Union[str, Any]											=				trim_offsets
             lowerCAmelCase__     :Optional[int]											=				True

           if changes_to_apply:
             lowerCAmelCase__     :str											=				getattr(__UpperCAmelCase       ,					state.pop('type' ) )
             lowerCAmelCase__     :Any											=				component_class(**__UpperCAmelCase )
             setattr(self.backend_tokenizer       ,					__UpperCAmelCase       ,					__UpperCAmelCase )






       @property
       def 	snake_case		(       self ):



         '''simple docstring'''

         if self._mask_token is None:
           if self.verbose:
             logger.error('Using mask_token, but it is not set yet.' )
           return None
         return str(self._mask_token )






       @mask_token.setter
       def 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :List[Any]											=				AddedToken(__UpperCAmelCase       ,					lstrip=__UpperCAmelCase       ,					rstrip=__UpperCAmelCase ) if isinstance(__UpperCAmelCase       ,					__UpperCAmelCase ) else value
         lowerCAmelCase__     :List[str]											=				value






       def 	snake_case		(       self       ,					*__UpperCAmelCase       ,					**__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[Any]											=				kwargs.get('is_split_into_words'       ,					__UpperCAmelCase )
         assert self.add_prefix_space or not is_split_into_words, (
             F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True "
             "to use it with pretokenized inputs."
         )

         return super()._batch_encode_plus(*__UpperCAmelCase       ,					**__UpperCAmelCase )






       def 	snake_case		(       self       ,					*__UpperCAmelCase       ,					**__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :Any											=				kwargs.get('is_split_into_words'       ,					__UpperCAmelCase )

         assert self.add_prefix_space or not is_split_into_words, (
             F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True "
             "to use it with pretokenized inputs."
         )

         return super()._encode_plus(*__UpperCAmelCase       ,					**__UpperCAmelCase )






       def 	snake_case		(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase = None ):



         '''simple docstring'''

         lowerCAmelCase__     :Union[str, Any]											=				self._tokenizer.model.save(__UpperCAmelCase       ,					name=__UpperCAmelCase )
         return tuple(__UpperCAmelCase )






       def 	snake_case		(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase=None ):



         '''simple docstring'''

         lowerCAmelCase__     :str											=				[self.bos_token_id] + token_ids_a + [self.eos_token_id]
         if token_ids_a is None:
           return output

         return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]






       def 	snake_case		(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase = None ):



         '''simple docstring'''

         lowerCAmelCase__     :List[Any]											=				[self.sep_token_id]
         lowerCAmelCase__     :int											=				[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]
