



"""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    __lowercase     (					_UpperCAmelCase					,    unittest.TestCase):







						"""simple docstring"""







						_A		:							int							       =		DebertaTokenizer
						_A		:							Optional[int]							       =		True
						_A		:							Tuple							       =		DebertaTokenizerFast







						def 						__UpperCamelCase							(self       ):
											super().setUp()

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

											snake_case_					:		Union[str, Any] 					=					os.path.join(self.tmpdirname   ,		VOCAB_FILES_NAMES["""vocab_file"""]       )
											snake_case_					:		str 					=					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(lowercase__       ) + """\n"""       )
											with open(self.merges_file   ,		"""w"""   ,		encoding="""utf-8"""       ) as fp:
																fp.write("""\n""".join(lowercase__       )       )







						def 						__UpperCamelCase							(self   ,		**lowercase__       ):
											kwargs.update(self.special_tokens_map       )
											return self.tokenizer_class.from_pretrained(self.tmpdirname   ,		**lowercase__       )







						def 						__UpperCamelCase							(self   ,		lowercase__       ):
											snake_case_					:		Dict 					=					"""lower newer"""
											snake_case_					:		Tuple 					=					"""lower newer"""
											return input_text, output_text







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		int 					=					self.get_tokenizer()
											snake_case_					:		List[Any] 					=					"""lower newer"""
											snake_case_					:		str 					=					["""l""", """o""", """w""", """er""", """\u0120""", """n""", """e""", """w""", """er"""]
											snake_case_					:		Tuple 					=					tokenizer.tokenize(lowercase__       )
											self.assertListEqual(lowercase__   ,		lowercase__       )

											snake_case_					:		List[str] 					=					tokens + [tokenizer.unk_token]
											snake_case_					:		Any 					=					[0, 1, 2, 15, 10, 9, 3, 2, 15, 19]
											self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase__       )   ,		lowercase__       )







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		str 					=					self.get_tokenizer()
											snake_case_					:		Optional[int] 					=					tokenizer("""Hello"""   ,		"""World"""       )
											snake_case_					:		Dict 					=					[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
											self.assertListEqual(tokd["""token_type_ids"""]   ,		lowercase__       )







						@slow
						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Tuple 					=					self.tokenizer_class.from_pretrained("""microsoft/deberta-base"""       )

											snake_case_					:		Dict 					=					tokenizer.encode("""sequence builders"""   ,		add_special_tokens=lowercase__       )
											snake_case_					:		Optional[int] 					=					tokenizer.encode("""multi-sequence build"""   ,		add_special_tokens=lowercase__       )

											snake_case_					:		Union[str, Any] 					=					tokenizer.encode(
											    """sequence builders"""   ,		add_special_tokens=lowercase__   ,		add_prefix_space=lowercase__       )
											snake_case_					:		int 					=					tokenizer.encode(
											    """sequence builders"""   ,		"""multi-sequence build"""   ,		add_special_tokens=lowercase__   ,		add_prefix_space=lowercase__       )

											snake_case_					:		Union[str, Any] 					=					tokenizer.build_inputs_with_special_tokens(lowercase__       )
											snake_case_					:		Tuple 					=					tokenizer.build_inputs_with_special_tokens(lowercase__   ,		lowercase__       )

											assert encoded_sentence == encoded_text_from_decode
											assert encoded_pair == encoded_pair_from_decode







						@slow
						def 						__UpperCamelCase							(self       ):
											snake_case_					:		str 					=					[self.tokenizer_class]
											if self.test_rust_tokenizer:
																tokenizer_classes.append(self.rust_tokenizer_class       )

											for tokenizer_class in tokenizer_classes:
																snake_case_					:		str 					=					tokenizer_class.from_pretrained("""microsoft/deberta-base"""       )

																snake_case_					:		Optional[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.""",
																]

																snake_case_					:		Optional[Any] 					=					tokenizer(lowercase__   ,		padding=lowercase__       )
																snake_case_					:		List[str] 					=					[tokenizer.decode(lowercase__   ,		skip_special_tokens=lowercase__       ) for seq in encoding["""input_ids"""]]

																# fmt: off
																snake_case_					:		Optional[Any] 					=					{
																    """input_ids""": [
																        [1, 21_18, 1_11_26, 5_65, 35, 83, 2_51_91, 1_63, 1_88_54, 13, 1_21_56, 12, 1_61_01, 2_53_76, 1_38_07, 9, 2_22_05, 2_78_93, 16_35, 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, 21_18, 1_11_26, 5_65, 2_45_36, 80, 4_37_97, 48_78, 73_73, 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_33, 78, 65, 16, 10, 37_24, 15_38, 3_31_83, 1_13_03, 4_37_97, 19_38, 4, 8_70, 2_41_65, 2_91_05, 5, 7_39, 3_26_44, 3_31_83, 1_13_03, 3_61_73, 88, 80, 6_50, 78_21, 4_59_40, 6, 52, 25_59, 5, 18_36, 9, 5, 73_97, 1_31_71, 31, 5, 18_36, 9, 3_26_44, 3_31_83, 1_13_03, 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

																snake_case_					:		str 					=					[
																    """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   ,		lowercase__       )

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









"""simple docstring"""

import pytest

from datasets import Dataset, DatasetDict, Features, NamedSplit, Value
from datasets.io.text import TextDatasetReader

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  List[Any]				,    SCREAMING_SNAKE_CASE__ :  Dict			):



					"""simple docstring"""





					assert isinstance(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			)
					assert dataset.num_rows == 4
					assert dataset.num_columns == 1
					assert dataset.column_names == ["text"]
					for feature, expected_dtype in expected_features.items():
										assert dataset.features[feature].dtype == expected_dtype







@pytest.mark.parametrize("""keep_in_memory"""				,    [False, True]			)
def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  Union[str, Any]				,    SCREAMING_SNAKE_CASE__ :  Optional[Any]				,    SCREAMING_SNAKE_CASE__ :  Optional[Any]			):



					"""simple docstring"""





					snake_case_					:		Optional[Any] 					=					tmp_path / """cache"""
					snake_case_					:		Optional[int] 					=					{"""text""": """string"""}
					with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
										snake_case_					:		Tuple 					=					TextDatasetReader(SCREAMING_SNAKE_CASE__				,    cache_dir=SCREAMING_SNAKE_CASE__				,    keep_in_memory=SCREAMING_SNAKE_CASE__			).read()
					_check_text_dataset(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			)







@pytest.mark.parametrize(
    """features"""				,    [
        None,
        {"""text""": """string"""},
        {"""text""": """int32"""},
        {"""text""": """float32"""},
    ]				,    )
def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  Dict				,    SCREAMING_SNAKE_CASE__ :  List[str]				,    SCREAMING_SNAKE_CASE__ :  Union[str, Any]			):



					"""simple docstring"""





					snake_case_					:		List[Any] 					=					tmp_path / """cache"""
					snake_case_					:		int 					=					{"""text""": """string"""}
					snake_case_					:		Any 					=					features.copy() if features else default_expected_features
					snake_case_					:		List[Any] 					=					(
					    Features({feature: Value(SCREAMING_SNAKE_CASE__			) for feature, dtype in features.items()}			) if features is not None else None
					)
					snake_case_					:		Dict 					=					TextDatasetReader(SCREAMING_SNAKE_CASE__				,    features=SCREAMING_SNAKE_CASE__				,    cache_dir=SCREAMING_SNAKE_CASE__			).read()
					_check_text_dataset(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			)







@pytest.mark.parametrize("""split"""				,    [None, NamedSplit("""train"""			), """train""", """test"""]			)
def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  Tuple				,    SCREAMING_SNAKE_CASE__ :  int				,    SCREAMING_SNAKE_CASE__ :  str			):



					"""simple docstring"""





					snake_case_					:		Union[str, Any] 					=					tmp_path / """cache"""
					snake_case_					:		Optional[Any] 					=					{"""text""": """string"""}
					snake_case_					:		Optional[int] 					=					TextDatasetReader(SCREAMING_SNAKE_CASE__				,    cache_dir=SCREAMING_SNAKE_CASE__				,    split=SCREAMING_SNAKE_CASE__			).read()
					_check_text_dataset(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			)
					assert dataset.split == split if split else "train"







@pytest.mark.parametrize("""path_type"""				,    [str, list]			)
def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  Tuple				,    SCREAMING_SNAKE_CASE__ :  List[str]				,    SCREAMING_SNAKE_CASE__ :  Dict			):



					"""simple docstring"""





					if issubclass(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			):
										snake_case_					:		List[str] 					=					text_path
					elif issubclass(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			):
										snake_case_					:		str 					=					[text_path]
					snake_case_					:		List[str] 					=					tmp_path / """cache"""
					snake_case_					:		List[str] 					=					{"""text""": """string"""}
					snake_case_					:		Dict 					=					TextDatasetReader(SCREAMING_SNAKE_CASE__				,    cache_dir=SCREAMING_SNAKE_CASE__			).read()
					_check_text_dataset(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			)







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  str				,    SCREAMING_SNAKE_CASE__ :  Dict				,    SCREAMING_SNAKE_CASE__ :  List[str]=("train",)			):



					"""simple docstring"""





					assert isinstance(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			)
					for split in splits:
										snake_case_					:		Dict 					=					dataset_dict[split]
										assert dataset.num_rows == 4
										assert dataset.num_columns == 1
										assert dataset.column_names == ["text"]
										for feature, expected_dtype in expected_features.items():
															assert dataset.features[feature].dtype == expected_dtype







@pytest.mark.parametrize("""keep_in_memory"""				,    [False, True]			)
def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  List[Any]				,    SCREAMING_SNAKE_CASE__ :  Tuple				,    SCREAMING_SNAKE_CASE__ :  Any			):



					"""simple docstring"""





					snake_case_					:		int 					=					tmp_path / """cache"""
					snake_case_					:		List[str] 					=					{"""text""": """string"""}
					with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
										snake_case_					:		Optional[Any] 					=					TextDatasetReader({"""train""": text_path}				,    cache_dir=SCREAMING_SNAKE_CASE__				,    keep_in_memory=SCREAMING_SNAKE_CASE__			).read()
					_check_text_datasetdict(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			)







@pytest.mark.parametrize(
    """features"""				,    [
        None,
        {"""text""": """string"""},
        {"""text""": """int32"""},
        {"""text""": """float32"""},
    ]				,    )
def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  int				,    SCREAMING_SNAKE_CASE__ :  int				,    SCREAMING_SNAKE_CASE__ :  List[str]			):



					"""simple docstring"""





					snake_case_					:		Tuple 					=					tmp_path / """cache"""
					# CSV file loses col_1 string dtype information: default now is "int64" instead of "string"
					snake_case_					:		List[str] 					=					{"""text""": """string"""}
					snake_case_					:		int 					=					features.copy() if features else default_expected_features
					snake_case_					:		Tuple 					=					(
					    Features({feature: Value(SCREAMING_SNAKE_CASE__			) for feature, dtype in features.items()}			) if features is not None else None
					)
					snake_case_					:		str 					=					TextDatasetReader({"""train""": text_path}				,    features=SCREAMING_SNAKE_CASE__				,    cache_dir=SCREAMING_SNAKE_CASE__			).read()
					_check_text_datasetdict(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			)







@pytest.mark.parametrize("""split"""				,    [None, NamedSplit("""train"""			), """train""", """test"""]			)
def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  Dict				,    SCREAMING_SNAKE_CASE__ :  Dict				,    SCREAMING_SNAKE_CASE__ :  Any			):



					"""simple docstring"""





					if split:
										snake_case_					:		Union[str, Any] 					=					{split: text_path}
					else:
										snake_case_					:		Union[str, Any] 					=					"""train"""
										snake_case_					:		int 					=					{"""train""": text_path, """test""": text_path}
					snake_case_					:		List[Any] 					=					tmp_path / """cache"""
					snake_case_					:		Tuple 					=					{"""text""": """string"""}
					snake_case_					:		int 					=					TextDatasetReader(SCREAMING_SNAKE_CASE__				,    cache_dir=SCREAMING_SNAKE_CASE__			).read()
					_check_text_datasetdict(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__				,    splits=list(path.keys()			)			)
					assert all(dataset[split].split == split for split in path.keys()			)




