



from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import numpy as np
    import tensorflow as tf

    from transformers import (
        TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        FlaubertConfig,
        TFFlaubertForMultipleChoice,
        TFFlaubertForQuestionAnsweringSimple,
        TFFlaubertForSequenceClassification,
        TFFlaubertForTokenClassification,
        TFFlaubertModel,
        TFFlaubertWithLMHeadModel,
    )
class 				__A					:





   def __init__(self							,    __magic_name__							,    ):
      lowerCamelCase__ :      Union[str, Any]								=		parent
      lowerCamelCase__ :      List[str]								=		13
      lowerCamelCase__ :      str								=		7
      lowerCamelCase__ :      Any								=		True
      lowerCamelCase__ :      List[str]								=		True
      lowerCamelCase__ :      Optional[int]								=		True
      lowerCamelCase__ :      Union[str, Any]								=		True
      lowerCamelCase__ :      str								=		True
      lowerCamelCase__ :      List[Any]								=		False
      lowerCamelCase__ :      List[Any]								=		False
      lowerCamelCase__ :      Optional[Any]								=		False
      lowerCamelCase__ :      str								=		2
      lowerCamelCase__ :      Any								=		99
      lowerCamelCase__ :      Union[str, Any]								=		0
      lowerCamelCase__ :      List[Any]								=		32
      lowerCamelCase__ :      Tuple								=		2
      lowerCamelCase__ :      int								=		4
      lowerCamelCase__ :      Any								=		0.1
      lowerCamelCase__ :      str								=		0.1
      lowerCamelCase__ :      List[Any]								=		512
      lowerCamelCase__ :      List[str]								=		16
      lowerCamelCase__ :      Union[str, Any]								=		2
      lowerCamelCase__ :      Optional[Any]								=		0.02
      lowerCamelCase__ :      Optional[Any]								=		3
      lowerCamelCase__ :      str								=		4
      lowerCamelCase__ :      Any								=		"""last"""
      lowerCamelCase__ :      Optional[int]								=		True
      lowerCamelCase__ :      Dict								=		None
      lowerCamelCase__ :      List[Any]								=		0





   def        _snake_case    (self				):
      lowerCamelCase__ :      Union[str, Any]								=		ids_tensor([self.batch_size, self.seq_length]							,    self.vocab_size				)
      lowerCamelCase__ :      List[str]								=		random_attention_mask([self.batch_size, self.seq_length]							,    dtype=tf.floataa				)

      lowerCamelCase__ :      Dict								=		None
      if self.use_input_lengths:
         lowerCamelCase__ :      Tuple								=		(
             ids_tensor([self.batch_size]							,    vocab_size=2				) + self.seq_length - 2
         )  # small variation of seq_length

      lowerCamelCase__ :      List[str]								=		None
      if self.use_token_type_ids:
         lowerCamelCase__ :      Union[str, Any]								=		ids_tensor([self.batch_size, self.seq_length]							,    self.n_langs				)

      lowerCamelCase__ :      Optional[Any]								=		None
      lowerCamelCase__ :      Optional[Any]								=		None
      lowerCamelCase__ :      Union[str, Any]								=		None
      if self.use_labels:
         lowerCamelCase__ :      int								=		ids_tensor([self.batch_size]							,    self.type_sequence_label_size				)
         lowerCamelCase__ :      Any								=		ids_tensor([self.batch_size, self.seq_length]							,    self.num_labels				)
         lowerCamelCase__ :      Dict								=		ids_tensor([self.batch_size]							,    2							,    dtype=tf.floataa				)
         lowerCamelCase__ :      str								=		ids_tensor([self.batch_size]							,    self.num_choices				)

      lowerCamelCase__ :      Optional[int]								=		FlaubertConfig(
          vocab_size=self.vocab_size							,    n_special=self.n_special							,    emb_dim=self.hidden_size							,    n_layers=self.num_hidden_layers							,    n_heads=self.num_attention_heads							,    dropout=self.hidden_dropout_prob							,    attention_dropout=self.attention_probs_dropout_prob							,    gelu_activation=self.gelu_activation							,    sinusoidal_embeddings=self.sinusoidal_embeddings							,    asm=self.asm							,    causal=self.causal							,    n_langs=self.n_langs							,    max_position_embeddings=self.max_position_embeddings							,    initializer_range=self.initializer_range							,    summary_type=self.summary_type							,    use_proj=self.use_proj							,    bos_token_id=self.bos_token_id							,    )

      return (
          config,
          input_ids,
          token_type_ids,
          input_lengths,
          sequence_labels,
          token_labels,
          is_impossible_labels,
          choice_labels,
          input_mask,
      )





   def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    ):
      lowerCamelCase__ :      Dict								=		TFFlaubertModel(config=__magic_name__				)
      lowerCamelCase__ :      Optional[int]								=		{"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
      lowerCamelCase__ :      List[Any]								=		model(__magic_name__				)

      lowerCamelCase__ :      Dict								=		[input_ids, input_mask]
      lowerCamelCase__ :      Any								=		model(__magic_name__				)
      self.parent.assertEqual(result.last_hidden_state.shape							,    (self.batch_size, self.seq_length, self.hidden_size)				)





   def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    ):
      lowerCamelCase__ :      Any								=		TFFlaubertWithLMHeadModel(__magic_name__				)

      lowerCamelCase__ :      Optional[int]								=		{"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
      lowerCamelCase__ :      Any								=		model(__magic_name__				)

      self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.seq_length, self.vocab_size)				)





   def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    ):
      lowerCamelCase__ :      Any								=		TFFlaubertForQuestionAnsweringSimple(__magic_name__				)

      lowerCamelCase__ :      Union[str, Any]								=		{"""input_ids""": input_ids, """lengths""": input_lengths}

      lowerCamelCase__ :      Dict								=		model(__magic_name__				)

      self.parent.assertEqual(result.start_logits.shape							,    (self.batch_size, self.seq_length)				)
      self.parent.assertEqual(result.end_logits.shape							,    (self.batch_size, self.seq_length)				)





   def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    ):
      lowerCamelCase__ :      Union[str, Any]								=		TFFlaubertForSequenceClassification(__magic_name__				)

      lowerCamelCase__ :      Dict								=		{"""input_ids""": input_ids, """lengths""": input_lengths}

      lowerCamelCase__ :      Union[str, Any]								=		model(__magic_name__				)

      self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.type_sequence_label_size)				)





   def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    ):
      lowerCamelCase__ :      Dict								=		self.num_labels
      lowerCamelCase__ :      List[str]								=		TFFlaubertForTokenClassification(config=__magic_name__				)
      lowerCamelCase__ :      Tuple								=		{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
      lowerCamelCase__ :      Union[str, Any]								=		model(__magic_name__				)
      self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.seq_length, self.num_labels)				)





   def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    ):
      lowerCamelCase__ :      Tuple								=		self.num_choices
      lowerCamelCase__ :      List[Any]								=		TFFlaubertForMultipleChoice(config=__magic_name__				)
      lowerCamelCase__ :      Union[str, Any]								=		tf.tile(tf.expand_dims(__magic_name__							,    1				)							,    (1, self.num_choices, 1)				)
      lowerCamelCase__ :      Union[str, Any]								=		tf.tile(tf.expand_dims(__magic_name__							,    1				)							,    (1, self.num_choices, 1)				)
      lowerCamelCase__ :      Dict								=		tf.tile(tf.expand_dims(__magic_name__							,    1				)							,    (1, self.num_choices, 1)				)
      lowerCamelCase__ :      List[str]								=		{
          """input_ids""": multiple_choice_inputs_ids,
          """attention_mask""": multiple_choice_input_mask,
          """token_type_ids""": multiple_choice_token_type_ids,
      }
      lowerCamelCase__ :      int								=		model(__magic_name__				)
      self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.num_choices)				)





   def        _snake_case    (self				):
      lowerCamelCase__ :      Optional[int]								=		self.prepare_config_and_inputs()
      (
          lowerCamelCase__
      ) :      int								=		config_and_inputs
      lowerCamelCase__ :      Dict								=		{
          """input_ids""": input_ids,
          """token_type_ids""": token_type_ids,
          """langs""": token_type_ids,
          """lengths""": input_lengths,
      }
      return config, inputs_dict







@require_tf
class 				__A					( A_					, A_					, unittest.TestCase      ):
   UpperCamelCase					:str     =			(
       (
           TFFlaubertModel,
           TFFlaubertWithLMHeadModel,
           TFFlaubertForSequenceClassification,
           TFFlaubertForQuestionAnsweringSimple,
           TFFlaubertForTokenClassification,
           TFFlaubertForMultipleChoice,
       )
       if is_tf_available()
       else ()
   )
   UpperCamelCase					:str     =			(
       (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
   )  # TODO (PVP): Check other models whether language generation is also applicable
   UpperCamelCase					:Optional[Any]     =			(
       {
           '''feature-extraction''': TFFlaubertModel,
           '''fill-mask''': TFFlaubertWithLMHeadModel,
           '''question-answering''': TFFlaubertForQuestionAnsweringSimple,
           '''text-classification''': TFFlaubertForSequenceClassification,
           '''token-classification''': TFFlaubertForTokenClassification,
           '''zero-shot''': TFFlaubertForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   UpperCamelCase					:Union[str, Any]     =			False
   UpperCamelCase					:Tuple     =			False





   def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
      if (
          pipeline_test_casse_name == "QAPipelineTests"
          and tokenizer_name is not None
          and not tokenizer_name.endswith("""Fast"""				)
      ):
         # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
         # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
         # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
         return True

      return False





   def        _snake_case    (self				):
      lowerCamelCase__ :      Optional[Any]								=		TFFlaubertModelTester(self				)
      lowerCamelCase__ :      Optional[int]								=		ConfigTester(self							,    config_class=__magic_name__							,    emb_dim=37				)





   def        _snake_case    (self				):
      self.config_tester.run_common_tests()





   def        _snake_case    (self				):
      lowerCamelCase__ :      List[str]								=		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_model(*__magic_name__				)





   def        _snake_case    (self				):
      lowerCamelCase__ :      Optional[Any]								=		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_lm_head(*__magic_name__				)





   def        _snake_case    (self				):
      lowerCamelCase__ :      Tuple								=		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_qa(*__magic_name__				)





   def        _snake_case    (self				):
      lowerCamelCase__ :      Tuple								=		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_sequence_classif(*__magic_name__				)





   def        _snake_case    (self				):
      lowerCamelCase__ :      str								=		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_for_token_classification(*__magic_name__				)





   def        _snake_case    (self				):
      lowerCamelCase__ :      Optional[int]								=		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_for_multiple_choice(*__magic_name__				)





   @slow
   def        _snake_case    (self				):
      for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         lowerCamelCase__ :      Any								=		TFFlaubertModel.from_pretrained(__magic_name__				)
         self.assertIsNotNone(__magic_name__				)







@require_tf
@require_sentencepiece
@require_tokenizers
class 				__A					( unittest.TestCase      ):





   @slow
   def        _snake_case    (self				):
      lowerCamelCase__ :      List[str]								=		TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased"""				)

      lowerCamelCase__ :      Tuple								=		tf.convert_to_tensor(
          [[0, 158, 735, 2592, 1424, 6727, 82, 1]]							,    dtype=tf.intaa							,    )  # "J'aime flaubert !"

      lowerCamelCase__ :      Any								=		model(__magic_name__				)[0]
      lowerCamelCase__ :      Any								=		tf.TensorShape((1, 8, 512)				)
      self.assertEqual(output.shape							,    __magic_name__				)
      # compare the actual values for a slice.
      lowerCamelCase__ :      Optional[int]								=		tf.convert_to_tensor(
          [
              [
                  [-1.8_76_87_73, -1.56_65_55, 0.27_07_24_18],
                  [-1.6_92_00_38, -0.5_87_35_05, 1.9_32_95_99],
                  [-2.9_56_39_85, -1.6_99_38_35, 1.7_97_20_52],
              ]
          ]							,    dtype=tf.floataa							,    )

      self.assertTrue(np.allclose(output[:, :3, :3].numpy()							,    expected_slice.numpy()							,    atol=1E-4				)				)








from __future__ import annotations

import string
from itertools import cycle, product
from pathlib import Path

_lowercase     							=						(
    string.ascii_letters + string.digits + string.punctuation + string.whitespace
)
_lowercase     							=						[ord(letter) for letter in string.ascii_lowercase]
_lowercase     							=						{ord(char) for char in VALID_CHARS}

_lowercase     							=						["the", "be", "to", "of", "and", "in", "that", "have"]






def      _A				(UpperCamelCase		:      list[int]      ,			UpperCamelCase		:      tuple[int, ...] )  ->str | None:



			'''simple docstring'''







			lowerCamelCase__ :      str								=		""
			lowerCamelCase__ :      int
			lowerCamelCase__ :      int
			lowerCamelCase__ :      int

			for keychar, cipherchar in zip(cycle(UpperCamelCase )      ,			UpperCamelCase ):
						lowerCamelCase__ :      str								=		cipherchar ^ keychar
						if decodedchar not in VALID_INTS:
									return None
						decoded += chr(UpperCamelCase )

			return decoded






def      _A				(UpperCamelCase		:      list[int] )  ->list[str]:



			'''simple docstring'''







			lowerCamelCase__ :      list[str]								=		[]
			for key in product(UpperCamelCase      ,			repeat=3 ):
						lowerCamelCase__ :      Union[str, Any]								=		try_key(UpperCamelCase      ,			UpperCamelCase )
						if encoded is not None:
									possibles.append(UpperCamelCase )
			return possibles






def      _A				(UpperCamelCase		:      list[str]      ,			UpperCamelCase		:      str )  ->list[str]:



			'''simple docstring'''







			return [possible for possible in possibles if common_word in possible.lower()]






def      _A				(UpperCamelCase		:      str = "p059_cipher.txt" )  ->int:



			'''simple docstring'''







			lowerCamelCase__ :      list[int]
			lowerCamelCase__ :      list[str]
			lowerCamelCase__ :      str
			lowerCamelCase__ :      str
			lowerCamelCase__ :      str								=		Path(UpperCamelCase ).parent.joinpath(UpperCamelCase ).read_text(encoding="""utf-8""" )

			lowerCamelCase__ :      str								=		[int(UpperCamelCase ) for number in data.strip().split(""",""" )]

			lowerCamelCase__ :      Optional[Any]								=		filter_valid_chars(UpperCamelCase )
			for common_word in COMMON_WORDS:
						lowerCamelCase__ :      Optional[int]								=		filter_common_word(UpperCamelCase      ,			UpperCamelCase )
						if len(UpperCamelCase ) == 1:
									break

			lowerCamelCase__ :      Union[str, Any]								=		possibles[0]
			return sum(ord(UpperCamelCase ) for char in decoded_text )


if __name__ == "__main__":
				print(F'''{solution() = }''')



