
"""simple docstring"""

import os
import pickle
import tempfile
import unittest
from typing import Callable, Optional

import numpy as np

from transformers import (
    BatchEncoding,
    BertTokenizer,
    BertTokenizerFast,
    PreTrainedTokenizer,
    PreTrainedTokenizerFast,
    TensorType,
    TokenSpan,
    is_tokenizers_available,
)
from transformers.models.gpta.tokenization_gpta import GPTaTokenizer
from transformers.testing_utils import CaptureStderr, require_flax, require_tf, require_tokenizers, require_torch, slow


if is_tokenizers_available():
					from tokenizers import Tokenizer
					from tokenizers.models import WordPiece






class        _lowercase   (				unittest.TestCase		):




	def 			_UpperCamelCase    ( self		, UpperCAmelCase_					)		->							List[str]:
			lowerCamelCase     :			Any		=    list(tokenizer_class.max_model_input_sizes.keys()					)
			for model_name in sa_models[:1]:
					lowerCamelCase     :			str		=    tokenizer_class.from_pretrained(UpperCAmelCase_					)
					self.assertIsNotNone(UpperCAmelCase_					)
					self.assertIsInstance(UpperCAmelCase_		, UpperCAmelCase_					)
					self.assertIsInstance(UpperCAmelCase_		, UpperCAmelCase_					)

					for special_tok in tokenizer.all_special_tokens:
							self.assertIsInstance(UpperCAmelCase_		, UpperCAmelCase_					)
							lowerCamelCase     :			Tuple		=    tokenizer.convert_tokens_to_ids(UpperCAmelCase_					)
							self.assertIsInstance(UpperCAmelCase_		, UpperCAmelCase_					)




	def 			_UpperCamelCase    ( self		, UpperCAmelCase_		, UpperCAmelCase_ = None					)		->							Any:
			lowerCamelCase     :			Dict		=    pickle.dumps(UpperCAmelCase_					)
			self.assertIsNotNone(UpperCAmelCase_					)

			lowerCamelCase     :			Any		=    pickle.loads(UpperCAmelCase_					)

			# Ensure is_fast is correctly restored
			self.assertEqual(be_restored.is_fast		, be_original.is_fast					)

			# Ensure encodings are potentially correctly restored
			if be_original.is_fast:
					self.assertIsNotNone(be_restored.encodings					)
			else:
					self.assertIsNone(be_restored.encodings					)

			# Ensure the keys are the same
			for original_v, restored_v in zip(be_original.values()		, be_restored.values()					):
					if equal_op:
							self.assertTrue(equal_op(UpperCAmelCase_		, UpperCAmelCase_					)					)
					else:
							self.assertEqual(UpperCAmelCase_		, UpperCAmelCase_					)




	@slow
	def 			_UpperCamelCase    ( self					)		->							Optional[int]:
			self.check_tokenizer_from_pretrained(UpperCAmelCase_					)




	def 			_UpperCamelCase    ( self					)		->							Any:
			self.assertEqual(TensorType('tf'					)		, TensorType.TENSORFLOW					)
			self.assertEqual(TensorType('pt'					)		, TensorType.PYTORCH					)
			self.assertEqual(TensorType('np'					)		, TensorType.NUMPY					)




	@require_tokenizers
	def 			_UpperCamelCase    ( self					)		->							str:
			import numpy as np

			lowerCamelCase     :			Optional[int]		=    BertTokenizer.from_pretrained('bert-base-cased'					)
			lowerCamelCase     :			Optional[int]		=    BertTokenizerFast.from_pretrained('bert-base-cased'					)

			# Python no tensor
			with self.subTest('BatchEncoding (Python, return_tensors=None)'					):
					self.assert_dump_and_restore(tokenizer_p('Small example to encode'					)					)

			with self.subTest('BatchEncoding (Python, return_tensors=NUMPY)'					):
					self.assert_dump_and_restore(
					    tokenizer_p('Small example to encode'		, return_tensors=TensorType.NUMPY					)		, np.array_equal					)

			with self.subTest('BatchEncoding (Rust, return_tensors=None)'					):
					self.assert_dump_and_restore(tokenizer_r('Small example to encode'					)					)

			with self.subTest('BatchEncoding (Rust, return_tensors=NUMPY)'					):
					self.assert_dump_and_restore(
					    tokenizer_r('Small example to encode'		, return_tensors=TensorType.NUMPY					)		, np.array_equal					)




	@require_tf
	@require_tokenizers
	def 			_UpperCamelCase    ( self					)		->							Tuple:
			import tensorflow as tf

			def tf_array_equals(UpperCAmelCase_		, UpperCAmelCase_					):
					return tf.reduce_all(tf.equal(UpperCAmelCase_		, UpperCAmelCase_					)					)

			lowerCamelCase     :			Union[str, Any]		=    BertTokenizer.from_pretrained('bert-base-cased'					)
			lowerCamelCase     :			int		=    BertTokenizerFast.from_pretrained('bert-base-cased'					)

			with self.subTest('BatchEncoding (Python, return_tensors=TENSORFLOW)'					):
					self.assert_dump_and_restore(
					    tokenizer_p('Small example to encode'		, return_tensors=TensorType.TENSORFLOW					)		, UpperCAmelCase_					)

			with self.subTest('BatchEncoding (Rust, return_tensors=TENSORFLOW)'					):
					self.assert_dump_and_restore(
					    tokenizer_r('Small example to encode'		, return_tensors=TensorType.TENSORFLOW					)		, UpperCAmelCase_					)




	@require_torch
	@require_tokenizers
	def 			_UpperCamelCase    ( self					)		->							Dict:
			import torch

			lowerCamelCase     :			str		=    BertTokenizer.from_pretrained('bert-base-cased'					)
			lowerCamelCase     :			Any		=    BertTokenizerFast.from_pretrained('bert-base-cased'					)

			with self.subTest('BatchEncoding (Python, return_tensors=PYTORCH)'					):
					self.assert_dump_and_restore(
					    tokenizer_p('Small example to encode'		, return_tensors=TensorType.PYTORCH					)		, torch.equal					)

			with self.subTest('BatchEncoding (Rust, return_tensors=PYTORCH)'					):
					self.assert_dump_and_restore(
					    tokenizer_r('Small example to encode'		, return_tensors=TensorType.PYTORCH					)		, torch.equal					)




	@require_tokenizers
	def 			_UpperCamelCase    ( self					)		->							List[str]:
			lowerCamelCase     :			int		=    BertTokenizer.from_pretrained('bert-base-cased'					)
			lowerCamelCase     :			Any		=    BertTokenizerFast.from_pretrained('bert-base-cased'					)

			with self.subTest('Python Tokenizer'					):
					self.assertFalse(tokenizer_p('Small example to_encode'					).is_fast					)

			with self.subTest('Rust Tokenizer'					):
					self.assertTrue(tokenizer_r('Small example to_encode'					).is_fast					)




	@require_tokenizers
	def 			_UpperCamelCase    ( self					)		->							int:
			lowerCamelCase     :			Optional[Any]		=    BertTokenizerFast.from_pretrained('bert-base-cased'					)
			lowerCamelCase     :			str		=    tokenizer_r(['Test', '\xad', 'test']		, is_split_into_words=UpperCAmelCase_					)

			self.assertEqual(encoded.word_to_tokens(0					)		, TokenSpan(start=1		, end=2					)					)
			self.assertEqual(encoded.word_to_tokens(1					)		, UpperCAmelCase_					)
			self.assertEqual(encoded.word_to_tokens(2					)		, TokenSpan(start=2		, end=3					)					)




	def 			_UpperCamelCase    ( self					)		->							Dict:
			lowerCamelCase     :			Dict		=    BatchEncoding({'inputs': [[1, 2, 3], [4, 5, 6]], 'labels': [0, 1]}					)
			lowerCamelCase     :			str		=    batch.convert_to_tensors(tensor_type='np'					)
			self.assertEqual(tensor_batch['inputs'].shape		, (2, 3)					)
			self.assertEqual(tensor_batch['labels'].shape		, (2,)					)
			# test converting the converted
			with CaptureStderr() as cs:
					lowerCamelCase     :			List[str]		=    batch.convert_to_tensors(tensor_type='np'					)
			self.assertFalse(len(cs.err					)		, msg=F"""should have no warning, but got {cs.err}"""					)

			lowerCamelCase     :			Tuple		=    BatchEncoding({'inputs': [1, 2, 3], 'labels': 0}					)
			lowerCamelCase     :			Union[str, Any]		=    batch.convert_to_tensors(tensor_type='np'		, prepend_batch_axis=UpperCAmelCase_					)
			self.assertEqual(tensor_batch['inputs'].shape		, (1, 3)					)
			self.assertEqual(tensor_batch['labels'].shape		, (1,)					)




	@require_torch
	def 			_UpperCamelCase    ( self					)		->							Any:
			lowerCamelCase     :			Dict		=    BatchEncoding({'inputs': [[1, 2, 3], [4, 5, 6]], 'labels': [0, 1]}					)
			lowerCamelCase     :			Optional[Any]		=    batch.convert_to_tensors(tensor_type='pt'					)
			self.assertEqual(tensor_batch['inputs'].shape		, (2, 3)					)
			self.assertEqual(tensor_batch['labels'].shape		, (2,)					)
			# test converting the converted
			with CaptureStderr() as cs:
					lowerCamelCase     :			Optional[int]		=    batch.convert_to_tensors(tensor_type='pt'					)
			self.assertFalse(len(cs.err					)		, msg=F"""should have no warning, but got {cs.err}"""					)

			lowerCamelCase     :			Optional[int]		=    BatchEncoding({'inputs': [1, 2, 3], 'labels': 0}					)
			lowerCamelCase     :			str		=    batch.convert_to_tensors(tensor_type='pt'		, prepend_batch_axis=UpperCAmelCase_					)
			self.assertEqual(tensor_batch['inputs'].shape		, (1, 3)					)
			self.assertEqual(tensor_batch['labels'].shape		, (1,)					)




	@require_tf
	def 			_UpperCamelCase    ( self					)		->							Dict:
			lowerCamelCase     :			List[str]		=    BatchEncoding({'inputs': [[1, 2, 3], [4, 5, 6]], 'labels': [0, 1]}					)
			lowerCamelCase     :			int		=    batch.convert_to_tensors(tensor_type='tf'					)
			self.assertEqual(tensor_batch['inputs'].shape		, (2, 3)					)
			self.assertEqual(tensor_batch['labels'].shape		, (2,)					)
			# test converting the converted
			with CaptureStderr() as cs:
					lowerCamelCase     :			Union[str, Any]		=    batch.convert_to_tensors(tensor_type='tf'					)
			self.assertFalse(len(cs.err					)		, msg=F"""should have no warning, but got {cs.err}"""					)

			lowerCamelCase     :			Dict		=    BatchEncoding({'inputs': [1, 2, 3], 'labels': 0}					)
			lowerCamelCase     :			str		=    batch.convert_to_tensors(tensor_type='tf'		, prepend_batch_axis=UpperCAmelCase_					)
			self.assertEqual(tensor_batch['inputs'].shape		, (1, 3)					)
			self.assertEqual(tensor_batch['labels'].shape		, (1,)					)




	@require_flax
	def 			_UpperCamelCase    ( self					)		->							str:
			lowerCamelCase     :			Union[str, Any]		=    BatchEncoding({'inputs': [[1, 2, 3], [4, 5, 6]], 'labels': [0, 1]}					)
			lowerCamelCase     :			Optional[int]		=    batch.convert_to_tensors(tensor_type='jax'					)
			self.assertEqual(tensor_batch['inputs'].shape		, (2, 3)					)
			self.assertEqual(tensor_batch['labels'].shape		, (2,)					)
			# test converting the converted
			with CaptureStderr() as cs:
					lowerCamelCase     :			Tuple		=    batch.convert_to_tensors(tensor_type='jax'					)
			self.assertFalse(len(cs.err					)		, msg=F"""should have no warning, but got {cs.err}"""					)

			lowerCamelCase     :			Tuple		=    BatchEncoding({'inputs': [1, 2, 3], 'labels': 0}					)
			lowerCamelCase     :			int		=    batch.convert_to_tensors(tensor_type='jax'		, prepend_batch_axis=UpperCAmelCase_					)
			self.assertEqual(tensor_batch['inputs'].shape		, (1, 3)					)
			self.assertEqual(tensor_batch['labels'].shape		, (1,)					)




	def 			_UpperCamelCase    ( self					)		->							int:
			lowerCamelCase     :			Tuple		=    [{'input_ids': np.array([0, 1, 2]					)}, {'input_ids': np.array([0, 1, 2, 3]					)}]
			lowerCamelCase     :			str		=    BertTokenizer.from_pretrained('bert-base-cased'					)

			lowerCamelCase     :			List[str]		=    tokenizer.pad(UpperCAmelCase_		, padding=UpperCAmelCase_					)
			self.assertTrue(isinstance(batch['input_ids']		, np.ndarray					)					)
			self.assertEqual(batch['input_ids'].tolist()		, [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]					)
			lowerCamelCase     :			str		=    tokenizer.pad(UpperCAmelCase_		, padding=UpperCAmelCase_		, return_tensors='np'					)
			self.assertTrue(isinstance(batch['input_ids']		, np.ndarray					)					)
			self.assertEqual(batch['input_ids'].tolist()		, [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]					)




	@require_torch
	def 			_UpperCamelCase    ( self					)		->							Any:
			import torch

			lowerCamelCase     :			Union[str, Any]		=    [{'input_ids': torch.tensor([0, 1, 2]					)}, {'input_ids': torch.tensor([0, 1, 2, 3]					)}]
			lowerCamelCase     :			Dict		=    BertTokenizer.from_pretrained('bert-base-cased'					)

			lowerCamelCase     :			Optional[int]		=    tokenizer.pad(UpperCAmelCase_		, padding=UpperCAmelCase_					)
			self.assertTrue(isinstance(batch['input_ids']		, torch.Tensor					)					)
			self.assertEqual(batch['input_ids'].tolist()		, [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]					)
			lowerCamelCase     :			List[Any]		=    tokenizer.pad(UpperCAmelCase_		, padding=UpperCAmelCase_		, return_tensors='pt'					)
			self.assertTrue(isinstance(batch['input_ids']		, torch.Tensor					)					)
			self.assertEqual(batch['input_ids'].tolist()		, [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]					)




	@require_tf
	def 			_UpperCamelCase    ( self					)		->							int:
			import tensorflow as tf

			lowerCamelCase     :			List[Any]		=    [{'input_ids': tf.constant([0, 1, 2]					)}, {'input_ids': tf.constant([0, 1, 2, 3]					)}]
			lowerCamelCase     :			Optional[int]		=    BertTokenizer.from_pretrained('bert-base-cased'					)

			lowerCamelCase     :			Tuple		=    tokenizer.pad(UpperCAmelCase_		, padding=UpperCAmelCase_					)
			self.assertTrue(isinstance(batch['input_ids']		, tf.Tensor					)					)
			self.assertEqual(batch['input_ids'].numpy().tolist()		, [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]					)
			lowerCamelCase     :			str		=    tokenizer.pad(UpperCAmelCase_		, padding=UpperCAmelCase_		, return_tensors='tf'					)
			self.assertTrue(isinstance(batch['input_ids']		, tf.Tensor					)					)
			self.assertEqual(batch['input_ids'].numpy().tolist()		, [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]					)




	@require_tokenizers
	def 			_UpperCamelCase    ( self					)		->							Any:
			lowerCamelCase     :			int		=    Tokenizer(WordPiece(unk_token='[UNK]'					)					)
			PreTrainedTokenizerFast(tokenizer_object=UpperCAmelCase_					)




	@require_tokenizers
	def 			_UpperCamelCase    ( self					)		->							Tuple:
			lowerCamelCase     :			Optional[Any]		=    Tokenizer(WordPiece(unk_token='[UNK]'					)					)
			with tempfile.TemporaryDirectory() as tmpdirname:
					bert_tokenizer.save(os.path.join(UpperCAmelCase_		, 'tokenizer.json'					)					)
					PreTrainedTokenizerFast(tokenizer_file=os.path.join(UpperCAmelCase_		, 'tokenizer.json'					)					)










'''simple docstring'''







from __future__ import annotations






def       lowerCAmelCase__       (				lowerCamelCase     :			str  ,lowerCamelCase     :			dict[str, str]   ):
       _A  , _A  , _A     :	int      =					'', '', ''
       _A     :	Optional[int]      =					[]

       for character in message_part:
              tmp.append(character_to_number[character]   )

       for each in tmp:
              one += each[0]
              two += each[1]
              three += each[2]

       return one + two + three






def       lowerCAmelCase__       (				lowerCamelCase     :			str  ,lowerCamelCase     :			dict[str, str]   ):
       _A  , _A     :	Tuple      =					'', ''
       _A     :	Tuple      =					[]

       for character in message_part:
              this_part += character_to_number[character]

       for digit in this_part:
              tmp += digit
              if len(lowerCamelCase   ) == len(lowerCamelCase   ):
                     result.append(lowerCamelCase   )
                     _A     :	str      =					''

       return result[0], result[1], result[2]






def       lowerCAmelCase__       (				lowerCamelCase     :			str  ,lowerCamelCase     :			str   ):
       # Validate message and alphabet, set to upper and remove spaces
       _A     :	str      =					alphabet.replace(' '  ,''   ).upper()
       _A     :	Optional[Any]      =					message.replace(' '  ,''   ).upper()

       # Check length and characters
       if len(lowerCamelCase   ) != 27:
              raise KeyError('Length of alphabet has to be 27.'   )
       for each in message:
              if each not in alphabet:
                     raise ValueError('Each message character has to be included in alphabet!'   )

    # Generate dictionares
       _A     :	int      =					(
           '111',
           '112',
           '113',
           '121',
           '122',
           '123',
           '131',
           '132',
           '133',
           '211',
           '212',
           '213',
           '221',
           '222',
           '223',
           '231',
           '232',
           '233',
           '311',
           '312',
           '313',
           '321',
           '322',
           '323',
           '331',
           '332',
           '333',
       )
       _A     :	Optional[int]      =					{}
       _A     :	Any      =					{}
       for letter, number in zip(lowerCamelCase  ,lowerCamelCase   ):
              _A     :	str      =					number
              _A     :	List[str]      =					letter

       return message, alphabet, character_to_number, number_to_character






def       lowerCAmelCase__       (				lowerCamelCase     :			str  ,lowerCamelCase     :			str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ."  ,lowerCamelCase     :			int = 5   ):
       _A  , _A  , _A  , _A     :	Union[str, Any]      =					__prepare(
           lowerCamelCase  ,lowerCamelCase   )
       _A  , _A     :	Any      =					'', ''

       for i in range(0  ,len(lowerCamelCase   ) + 1  ,lowerCamelCase   ):
              encrypted_numeric += __encrypt_part(
                  message[i : i + period]  ,lowerCamelCase   )

       for i in range(0  ,len(lowerCamelCase   )  ,3   ):
              encrypted += number_to_character[encrypted_numeric[i : i + 3]]

       return encrypted






def       lowerCAmelCase__       (				lowerCamelCase     :			str  ,lowerCamelCase     :			str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ."  ,lowerCamelCase     :			int = 5   ):
       _A  , _A  , _A  , _A     :	Optional[Any]      =					__prepare(
           lowerCamelCase  ,lowerCamelCase   )
       _A     :	Tuple      =					[]
       _A     :	List[str]      =					''

       for i in range(0  ,len(lowerCamelCase   ) + 1  ,lowerCamelCase   ):
              _A  , _A  , _A     :	Tuple      =					__decrypt_part(message[i : i + period]  ,lowerCamelCase   )

              for j in range(0  ,len(lowerCamelCase   )   ):
                     decrypted_numeric.append(a[j] + b[j] + c[j]   )

       for each in decrypted_numeric:
              decrypted += number_to_character[each]

       return decrypted


if __name__ == "__main__":
   A      :     Dict										= '''DEFEND THE EAST WALL OF THE CASTLE.'''
   A      :     Any										= encrypt_message(msg, '''EPSDUCVWYM.ZLKXNBTFGORIJHAQ''')
   A      :     List[Any]										= decrypt_message(encrypted, '''EPSDUCVWYM.ZLKXNBTFGORIJHAQ''')
   print(f"""Encrypted: {encrypted}\nDecrypted: {decrypted}""")






