'''simple docstring'''






import os
import shutil
import tempfile
from unittest import TestCase
from unittest.mock import patch

import numpy as np
from datasets import Dataset

from transformers.models.realm.configuration_realm import RealmConfig
from transformers.models.realm.retrieval_realm import _REALM_BLOCK_RECORDS_FILENAME, RealmRetriever
from transformers.models.realm.tokenization_realm import VOCAB_FILES_NAMES, RealmTokenizer





class     A__							(       UpperCamelCase__   ):





   """simple docstring"""





   def 			_lowerCAmelCase		( self     :		Optional[Any]			)       ->    Dict:







      """simple docstring"""




      _UpperCAmelCase							:	str							=   tempfile.mkdtemp()
      _UpperCAmelCase							:	Union[str, Any]							=   5

      # Realm tok
      _UpperCAmelCase							:	Optional[int]							=   [
          "[UNK]",
          "[CLS]",
          "[SEP]",
          "[PAD]",
          "[MASK]",
          "test",
          "question",
          "this",
          "is",
          "the",
          "first",
          "second",
          "third",
          "fourth",
          "fifth",
          "record",
          "want",
          "##want",
          "##ed",
          "wa",
          "un",
          "runn",
          "##ing",
          ",",
          "low",
          "lowest",
      ]
      _UpperCAmelCase							:	List[Any]							=   os.path.join(self.tmpdirname						,     "realm_tokenizer"			)
      os.makedirs(__a						,     exist_ok=__a			)
      _UpperCAmelCase							:	Union[str, Any]							=   os.path.join(__a						,     VOCAB_FILES_NAMES["vocab_file"]			)
      with open(self.vocab_file						,     "w"						,     encoding="utf-8"			) as vocab_writer:
         vocab_writer.write("".join([x + "\n" for x in vocab_tokens]			)			)

      _UpperCAmelCase							:	Union[str, Any]							=   os.path.join(self.tmpdirname						,     "realm_block_records"			)
      os.makedirs(__a						,     exist_ok=__a			)




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







      """simple docstring"""




      return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname						,     "realm_tokenizer"			)			)




   def 			_lowerCAmelCase		( self     :		Any			)       ->    Union[str, Any]:







      """simple docstring"""




      shutil.rmtree(self.tmpdirname			)




   def 			_lowerCAmelCase		( self     :		List[Any]			)       ->    int:







      """simple docstring"""




      _UpperCAmelCase							:	str							=   RealmConfig(num_block_records=self.num_block_records			)
      return config




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







      """simple docstring"""




      _UpperCAmelCase							:	str							=   Dataset.from_dict(
          {
              "id": ["0", "1"],
              "question": ["foo", "bar"],
              "answers": [["Foo", "Bar"], ["Bar"]],
          }			)
      return dataset




   def 			_lowerCAmelCase		( self     :		Any			)       ->    str:







      """simple docstring"""




      _UpperCAmelCase							:	List[str]							=   np.array(
          [
              B"This is the first record",
              B"This is the second record",
              B"This is the third record",
              B"This is the fourth record",
              B"This is the fifth record",
              B"This is a longer longer longer record",
          ]						,     dtype=__a						,     )
      return block_records




   def 			_lowerCAmelCase		( self     :		Union[str, Any]			)       ->    List[str]:







      """simple docstring"""




      _UpperCAmelCase							:	Any							=   RealmRetriever(
          block_records=self.get_dummy_block_records()						,     tokenizer=self.get_tokenizer()						,     )
      return retriever




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







      """simple docstring"""




      _UpperCAmelCase							:	Union[str, Any]							=   self.get_config()
      _UpperCAmelCase							:	Dict							=   self.get_dummy_retriever()
      _UpperCAmelCase							:	Any							=   retriever.tokenizer

      _UpperCAmelCase							:	Dict							=   np.array([0, 3]						,     dtype="long"			)
      _UpperCAmelCase							:	Any							=   tokenizer(["Test question"]			).input_ids
      _UpperCAmelCase							:	List[str]							=   tokenizer(
          ["the fourth"]						,     add_special_tokens=__a						,     return_token_type_ids=__a						,     return_attention_mask=__a						,     ).input_ids
      _UpperCAmelCase							:	Tuple							=   config.reader_seq_len

      _UpperCAmelCase							,       _UpperCAmelCase							,       _UpperCAmelCase							,       _UpperCAmelCase							:	int							=   retriever(
          __a						,     __a						,     answer_ids=__a						,     max_length=__a						,     return_tensors="np"			)

      self.assertEqual(len(__a			)						,     2			)
      self.assertEqual(len(__a			)						,     2			)
      self.assertEqual(len(__a			)						,     2			)
      self.assertEqual(concat_inputs.input_ids.shape						,     (2, 1_0)			)
      self.assertEqual(concat_inputs.attention_mask.shape						,     (2, 1_0)			)
      self.assertEqual(concat_inputs.token_type_ids.shape						,     (2, 1_0)			)
      self.assertEqual(concat_inputs.special_tokens_mask.shape						,     (2, 1_0)			)
      self.assertEqual(
          tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[0]			)						,     ["[CLS]", "test", "question", "[SEP]", "this", "is", "the", "first", "record", "[SEP]"]						,     )
      self.assertEqual(
          tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[1]			)						,     ["[CLS]", "test", "question", "[SEP]", "this", "is", "the", "fourth", "record", "[SEP]"]						,     )




   def 			_lowerCAmelCase		( self     :		List[Any]			)       ->    Any:







      """simple docstring"""




      _UpperCAmelCase							:	Optional[int]							=   self.get_config()
      _UpperCAmelCase							:	str							=   self.get_dummy_retriever()
      _UpperCAmelCase							:	Optional[int]							=   retriever.tokenizer

      _UpperCAmelCase							:	str							=   np.array([0, 3, 5]						,     dtype="long"			)
      _UpperCAmelCase							:	List[Any]							=   tokenizer(["Test question"]			).input_ids
      _UpperCAmelCase							:	Optional[int]							=   tokenizer(
          ["the fourth", "longer longer"]						,     add_special_tokens=__a						,     return_token_type_ids=__a						,     return_attention_mask=__a						,     ).input_ids
      _UpperCAmelCase							:	Optional[int]							=   config.reader_seq_len

      _UpperCAmelCase							,       _UpperCAmelCase							,       _UpperCAmelCase							,       _UpperCAmelCase							:	List[Any]							=   retriever(
          __a						,     __a						,     answer_ids=__a						,     max_length=__a						,     return_tensors="np"			)

      self.assertEqual([False, True, True]						,     __a			)
      self.assertEqual([[-1, -1, -1], [6, -1, -1], [6, 7, 8]]						,     __a			)
      self.assertEqual([[-1, -1, -1], [7, -1, -1], [7, 8, 9]]						,     __a			)




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







      """simple docstring"""




      _UpperCAmelCase							:	str							=   self.get_dummy_retriever()
      retriever.save_pretrained(os.path.join(self.tmpdirname						,     "realm_block_records"			)			)

      # Test local path
      _UpperCAmelCase							:	List[str]							=   retriever.from_pretrained(os.path.join(self.tmpdirname						,     "realm_block_records"			)			)
      self.assertEqual(retriever.block_records[0]						,     B"This is the first record"			)

      # Test mocked remote path
      with patch("transformers.models.realm.retrieval_realm.hf_hub_download"			) as mock_hf_hub_download:
         _UpperCAmelCase							:	List[Any]							=   os.path.join(
             os.path.join(self.tmpdirname						,     "realm_block_records"			)						,     _REALM_BLOCK_RECORDS_FILENAME			)
         _UpperCAmelCase							:	Optional[int]							=   RealmRetriever.from_pretrained("google/realm-cc-news-pretrained-openqa"			)

      self.assertEqual(retriever.block_records[0]						,     B"This is the first record"			)
'''simple docstring'''





import math





def 	lowerCAmelCase_				(					)    ->      None:


     '''simple docstring'''




     UpperCAmelCase_           =   input("Enter message: "			)
     UpperCAmelCase_           =   int(input(f"""Enter key [2-{len(snake_case_			) - 1}]: """			)			)
     UpperCAmelCase_           =   input("Encryption/Decryption [e/d]: "			)

     if mode.lower().startswith("e"			):
          UpperCAmelCase_           =   encrypt_message(snake_case_					,    snake_case_			)
     elif mode.lower().startswith("d"			):
          UpperCAmelCase_           =   decrypt_message(snake_case_					,    snake_case_			)

     # Append pipe symbol (vertical bar) to identify spaces at the end.
     print(f"""Output:\n{text + "|"}"""			)





def 	lowerCAmelCase_				(					snake_case_       : int					,    snake_case_       : str			)    ->      str:


     '''simple docstring'''




     UpperCAmelCase_           =   [""] * key
     for col in range(snake_case_			):
          UpperCAmelCase_           =   col
          while pointer < len(snake_case_			):
               cipher_text[col] += message[pointer]
               pointer += key
     return "".join(snake_case_			)





def 	lowerCAmelCase_				(					snake_case_       : int					,    snake_case_       : str			)    ->      str:


     '''simple docstring'''




     UpperCAmelCase_           =   math.ceil(len(snake_case_			) / key			)
     UpperCAmelCase_           =   key
     UpperCAmelCase_           =   (num_cols * num_rows) - len(snake_case_			)
     UpperCAmelCase_           =   [""] * num_cols
     UpperCAmelCase_           =   0
     UpperCAmelCase_           =   0

     for symbol in message:
          plain_text[col] += symbol
          col += 1

          if (
              (col == num_cols)
              or (col == num_cols - 1)
              and (row >= num_rows - num_shaded_boxes)
          ):
               UpperCAmelCase_           =   0
               row += 1

     return "".join(snake_case_			)


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    main()




