
'''simple docstring'''







import json
import os
import unittest

from transformers.models.gptsan_japanese.tokenization_gptsan_japanese import (
    VOCAB_FILES_NAMES,
    GPTSanJapaneseTokenizer,
)
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class 						__UpperCAmelCase      (					_lowerCamelCase						,   unittest.TestCase						):
							__lowercase      		=       GPTSanJapaneseTokenizer
							__lowercase      		=       False
							__lowercase      		=       {"""do_clean_text""": False, """add_prefix_space""": False}


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								super().setUp()

								# fmt: off
								_snake_case   						=		['こん', 'こんに', 'にちは', 'ばんは', '世界,㔺界', '、', '。', '<BR>', '<SP>', '<TAB>', '<URL>', '<EMAIL>', '<TEL>', '<DATE>', '<PRICE>', '<BLOCK>', '<KIGOU>', '<U2000U2BFF>', '<|emoji1|>', '<unk>', '<|bagoftoken|>', '<|endoftext|>']
								# fmt: on
								_snake_case   						=		{'emoji': {'\ud83d\ude00': '<|emoji1|>'}, 'emoji_inv': {'<|emoji1|>': '\ud83d\ude00'}}  # 😀
								_snake_case   						=		{'unk_token': '<unk>'}

								_snake_case   						=		os.path.join(self.tmpdirname    ,   VOCAB_FILES_NAMES['vocab_file']  )
								_snake_case   						=		os.path.join(self.tmpdirname    ,   VOCAB_FILES_NAMES['emoji_file']  )
								with open(self.vocab_file    ,   'w'    ,   encoding='utf-8'  ) as vocab_writer:
									vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]  )  )
								with open(self.emoji_file    ,   'w'  ) as emoji_writer:
									emoji_writer.write(json.dumps(lowerCAmelCase_  )  )


							def  lowerCamelCase				(       self    ,   **lowerCAmelCase_  ):





								"""simple docstring"""






								kwargs.update(self.special_tokens_map  )
								return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname    ,   **lowerCAmelCase_  )


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		'こんにちは、世界。 \nこんばんは、㔺界。😀'
								_snake_case   						=		'こんにちは、世界。 \nこんばんは、世界。😀'
								return input_text, output_text


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case						, _snake_case   						=		self.get_input_output_texts(lowerCAmelCase_  )
								_snake_case   						=		tokenizer.encode(lowerCAmelCase_    ,   add_special_tokens=lowerCAmelCase_  )
								_snake_case   						=		tokenizer.decode(lowerCAmelCase_    ,   clean_up_tokenization_spaces=lowerCAmelCase_  )
								return text, ids


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								pass  # TODO add if relevant


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								pass  # TODO add if relevant


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								pass  # TODO add if relevant


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.get_tokenizer()

								# Testing tokenization
								_snake_case   						=		'こんにちは、世界。　こんばんは、㔺界。'
								_snake_case   						=		['こん', 'にちは', '、', '世界', '。', '<SP>', 'こん', 'ばんは', '、', '㔺界', '。']
								_snake_case   						=		tokenizer.tokenize(lowerCAmelCase_  )
								self.assertListEqual(lowerCAmelCase_    ,   lowerCAmelCase_  )

								# Testing conversion to ids without special tokens
								_snake_case   						=		[0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6]
								_snake_case   						=		tokenizer.convert_tokens_to_ids(lowerCAmelCase_  )
								self.assertListEqual(lowerCAmelCase_    ,   lowerCAmelCase_  )

								# Testing conversion to ids with special tokens
								_snake_case   						=		tokens + [tokenizer.unk_token]
								_snake_case   						=		[0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 19]
								_snake_case   						=		tokenizer.convert_tokens_to_ids(lowerCAmelCase_  )
								self.assertListEqual(lowerCAmelCase_    ,   lowerCAmelCase_  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.get_tokenizer()

								# Testing tokenization
								_snake_case   						=		'こんにちは、<|bagoftoken|>世界。こんばんは、<|bagoftoken|>㔺界。'
								_snake_case   						=		'こんにちは、、、、世界。こんばんは、、、、世界。'
								_snake_case   						=		tokenizer.encode(lowerCAmelCase_  )
								_snake_case   						=		tokenizer.decode(lowerCAmelCase_  )
								self.assertEqual(lowerCAmelCase_    ,   lowerCAmelCase_  )


							@slow
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese'  )

								# Testing tokenization
								_snake_case   						=		'こんにちは、世界。'
								_snake_case   						=		'こんばんは、㔺界。😀'
								_snake_case   						=		'こんにちは、世界。こんばんは、世界。😀'
								_snake_case   						=		tokenizer.encode(prefix_text + input_text  )
								_snake_case   						=		tokenizer.encode(''    ,   prefix_text=prefix_text + input_text  )
								_snake_case   						=		tokenizer.encode(lowerCAmelCase_    ,   prefix_text=lowerCAmelCase_  )
								_snake_case   						=		tokenizer.decode(lowerCAmelCase_  )
								_snake_case   						=		tokenizer.decode(lowerCAmelCase_  )
								_snake_case   						=		tokenizer.decode(lowerCAmelCase_  )
								self.assertEqual(lowerCAmelCase_    ,   lowerCAmelCase_  )
								self.assertEqual(lowerCAmelCase_    ,   lowerCAmelCase_  )
								self.assertEqual(lowerCAmelCase_    ,   lowerCAmelCase_  )


							@slow
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese'  )

								# Testing tokenization
								_snake_case   						=		'こんにちは、世界。'
								_snake_case   						=		'こんばんは、㔺界。😀'

								_snake_case   						=		len(tokenizer.encode(lowerCAmelCase_  )  ) - 2
								_snake_case   						=		len(tokenizer.encode(lowerCAmelCase_  )  ) - 2

								_snake_case   						=		[1] + [0] * (len_prefix + len_text + 1)
								_snake_case   						=		[1] * (len_prefix + len_text + 1) + [0]
								_snake_case   						=		[1] + [1] * (len_prefix) + [0] * (len_text + 1)

								_snake_case   						=		tokenizer(prefix_text + input_text  ).token_type_ids
								_snake_case   						=		tokenizer(''    ,   prefix_text=prefix_text + input_text  ).token_type_ids
								_snake_case   						=		tokenizer(lowerCAmelCase_    ,   prefix_text=lowerCAmelCase_  ).token_type_ids
								self.assertListEqual(lowerCAmelCase_    ,   lowerCAmelCase_  )
								self.assertListEqual(lowerCAmelCase_    ,   lowerCAmelCase_  )
								self.assertListEqual(lowerCAmelCase_    ,   lowerCAmelCase_  )


							@slow
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese'  )

								_snake_case   						=		tokenizer.encode('あンいワ'  )
								_snake_case   						=		tokenizer.encode(''    ,   prefix_text='あンいワ'  )
								_snake_case   						=		tokenizer.encode('いワ'    ,   prefix_text='あン'  )

								self.assertEqual(tokenizer.decode(lowerCAmelCase_  )    ,   tokenizer.decode(lowerCAmelCase_  )  )
								self.assertEqual(tokenizer.decode(lowerCAmelCase_  )    ,   tokenizer.decode(lowerCAmelCase_  )  )
								self.assertNotEqual(lowerCAmelCase_    ,   lowerCAmelCase_  )
								self.assertNotEqual(lowerCAmelCase_    ,   lowerCAmelCase_  )
								self.assertEqual(x_token_a[1]    ,   x_token_a[-1]  )  # SEG token
								self.assertEqual(x_token_a[1]    ,   x_token_a[3]  )  # SEG token


							@slow
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese'  )

								_snake_case   						=		[['武田信玄', 'は、'], ['織田信長', 'の配下の、']]
								_snake_case   						=		tokenizer(lowerCAmelCase_    ,   padding=lowerCAmelCase_  )
								_snake_case   						=		tokenizer.batch_encode_plus(lowerCAmelCase_    ,   padding=lowerCAmelCase_  )

								# fmt: off
								_snake_case   						=		[[3_59_93, 86_40, 2_59_48, 3_59_98, 3_06_47, 3_56_75, 3_59_99, 3_59_99], [3_59_93, 1_03_82, 98_68, 3_59_98, 3_06_46, 94_59, 3_06_46, 3_56_75]]
								_snake_case   						=		[[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]]
								_snake_case   						=		[[1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]]
								# fmt: on
								self.assertListEqual(x_token.input_ids    ,   lowerCAmelCase_  )
								self.assertListEqual(x_token.token_type_ids    ,   lowerCAmelCase_  )
								self.assertListEqual(x_token.attention_mask    ,   lowerCAmelCase_  )
								self.assertListEqual(x_token_a.input_ids    ,   lowerCAmelCase_  )
								self.assertListEqual(x_token_a.token_type_ids    ,   lowerCAmelCase_  )
								self.assertListEqual(x_token_a.attention_mask    ,   lowerCAmelCase_  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								pass




							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								pass









import unittest

from transformers import CamembertTokenizer, CamembertTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow
from transformers.utils import is_torch_available

from ...test_tokenization_common import TokenizerTesterMixin


lowerCamelCase       :       Any        =				get_tests_dir('''fixtures/test_sentencepiece.model''')
lowerCamelCase       :       Any        =				get_tests_dir('''fixtures/test_sentencepiece_bpe.model''')

lowerCamelCase       :       Optional[int]        =				'''pt''' if is_torch_available() else '''tf'''

@require_sentencepiece
@require_tokenizers
class 						lowerCAmelCase   (       __a					,				unittest.TestCase	):

					'''simple docstring'''

					_A  :	Dict     =					CamembertTokenizer
					_A  :	Union[str, Any]     =					CamembertTokenizerFast
					_A  :	Union[str, Any]     =					True
					_A  :	Tuple     =					True



					def 					lowerCAmelCase (     self :		Optional[int]							)	->     Optional[int]:


												"""simple docstring"""

												super().setUp()

												# We have a SentencePiece fixture for testing
												__lowercase					:							Union[str, Any]			=   CamembertTokenizer(__a							)
												tokenizer.save_pretrained(self.tmpdirname							)



					def 					lowerCAmelCase (     self :		Optional[int]							)	->     Optional[Any]:


												"""simple docstring"""

												__lowercase					:							Dict			=   """<pad>"""
												__lowercase					:							Dict			=   1

												self.assertEqual(self.get_tokenizer()._convert_token_to_id(__a							)						,   __a							)
												self.assertEqual(self.get_tokenizer()._convert_id_to_token(__a							)						,   __a							)



					def 					lowerCAmelCase (     self :		Optional[Any]							)	->     Tuple:


												"""simple docstring"""

												__lowercase					:							int			=   list(self.get_tokenizer().get_vocab().keys()							)

												self.assertEqual(vocab_keys[0]						,   """<s>NOTUSED"""							)
												self.assertEqual(vocab_keys[1]						,   """<pad>"""							)
												self.assertEqual(vocab_keys[-1]						,   """<mask>"""							)
												self.assertEqual(len(__a							)						,   1004							)



					def 					lowerCAmelCase (     self :		str							)	->     List[Any]:


												"""simple docstring"""

												self.assertEqual(self.get_tokenizer().vocab_size						,   1005							)



					def 					lowerCAmelCase (     self :		Any							)	->     Optional[Any]:


												"""simple docstring"""

												__lowercase					:							Tuple			=   CamembertTokenizer(__a							)
												tokenizer.save_pretrained(self.tmpdirname							)
												__lowercase					:							Tuple			=   CamembertTokenizerFast.from_pretrained(self.tmpdirname							)

												__lowercase					:							List[str]			=   """I was born in 92000, and this is falsé."""

												__lowercase					:							Optional[Any]			=   tokenizer.encode(__a							)
												__lowercase					:							List[Any]			=   rust_tokenizer.encode(__a							)
												self.assertListEqual(__a						,   __a							)

												__lowercase					:							Tuple			=   tokenizer.encode(__a						,   add_special_tokens=__a							)
												__lowercase					:							Union[str, Any]			=   rust_tokenizer.encode(__a						,   add_special_tokens=__a							)
												self.assertListEqual(__a						,   __a							)

												# <unk> tokens are not the same for `rust` than for `slow`.
												# Because spm gives back raw token instead of `unk` in EncodeAsPieces
												# tokens = tokenizer.tokenize(sequence)
												__lowercase					:							Dict			=   tokenizer.convert_ids_to_tokens(__a							)
												__lowercase					:							Any			=   rust_tokenizer.tokenize(__a							)
												self.assertListEqual(__a						,   __a							)



					def 					lowerCAmelCase (     self :		Any							)	->     Dict:


												"""simple docstring"""

												if not self.test_rust_tokenizer:
																			return

												__lowercase					:							List[str]			=   self.get_tokenizer()
												__lowercase					:							Any			=   self.get_rust_tokenizer()

												__lowercase					:							Any			=   """I was born in 92000, and this is falsé."""

												__lowercase					:							Tuple			=   tokenizer.tokenize(__a							)
												__lowercase					:							Optional[Any]			=   rust_tokenizer.tokenize(__a							)
												self.assertListEqual(__a						,   __a							)

												__lowercase					:							Dict			=   tokenizer.encode(__a						,   add_special_tokens=__a							)
												__lowercase					:							Dict			=   rust_tokenizer.encode(__a						,   add_special_tokens=__a							)
												self.assertListEqual(__a						,   __a							)

												__lowercase					:							Any			=   self.get_rust_tokenizer()
												__lowercase					:							str			=   tokenizer.encode(__a							)
												__lowercase					:							Union[str, Any]			=   rust_tokenizer.encode(__a							)
												self.assertListEqual(__a						,   __a							)



					@slow
					def 					lowerCAmelCase (     self :		Optional[int]							)	->     List[Any]:


												"""simple docstring"""

												__lowercase					:							str			=   {"""input_ids""": [[5, 54, 7196, 297, 30, 23, 776, 18, 11, 3215, 3705, 8252, 22, 3164, 1181, 2116, 29, 16, 813, 25, 791, 3314, 20, 3446, 38, 27575, 120, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 468, 17, 11, 9088, 20, 1517, 8, 22804, 18818, 10, 38, 629, 607, 607, 142, 19, 7196, 867, 56, 10326, 24, 2267, 20, 416, 5072, 15612, 233, 734, 7, 2399, 27, 16, 3015, 1649, 7, 24, 20, 4338, 2399, 27, 13, 3400, 14, 13, 6189, 8, 930, 9, 6]], """attention_mask""": [[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, 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, 1, 1, 1, 1, 1]]}  # noqa: E501
												# fmt: on

												# camembert is a french model. So we also use french texts.
												__lowercase					:							List[str]			=   [
												    """Le transformeur est un modèle d'apprentissage profond introduit en 2017, """
												    """utilisé principalement dans le domaine du traitement automatique des langues (TAL).""",
												    """À l'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus """
												    """pour gérer des données séquentielles, telles que le langage naturel, pour des tâches """
												    """telles que la traduction et la synthèse de texte.""",
												]

												self.tokenizer_integration_test_util(
												    expected_encoding=__a						,   model_name="""camembert-base"""						,   revision="""3a0641d9a1aeb7e848a74299e7e4c4bca216b4cf"""						,   sequences=__a						,   )