



import string

import numpy





def 		a__			(		_UpperCamelCase			: int ,_UpperCamelCase			: int							):
   return b if a == 0 else greatest_common_divisor(b % a ,_UpperCamelCase							)



class  __lowerCAmelCase     :
 lowerCAmelCase__       					=  string.ascii_uppercase + string.digits
 # This cipher takes alphanumerics into account
 # i.e. a total of 36 characters

 # take x and return x % len(key_string)
 lowerCAmelCase__       					=  numpy.vectorize(lambda lowerCAmelCase__   : x % 3_6   )

 lowerCAmelCase__       					=  numpy.vectorize(lowerCAmelCase__   )




 def __init__(						self	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=     self.modulus(__UpperCAmelCase				)  # mod36 calc's on the encrypt key
    self.check_determinant()  # validate the determinant of the encryption key
    __lowerCamelCase													=     encrypt_key.shape[0]




 def        lowerCamelCase      (						self	,				__UpperCAmelCase				):



    '''simple docstring'''
    return self.key_string.index(__UpperCAmelCase				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase				):



    '''simple docstring'''
    return self.key_string[round(__UpperCAmelCase				)]




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=     round(numpy.linalg.det(self.encrypt_key				)				)

    if det < 0:
       __lowerCamelCase													=     det % len(self.key_string				)

    __lowerCamelCase													=     len(self.key_string				)
    if greatest_common_divisor(__UpperCAmelCase	,				len(self.key_string				)				) != 1:
       __lowerCamelCase													=     (
           F"""determinant modular {req_l} of encryption key({det}) """
           F"""is not co prime w.r.t {req_l}.\nTry another key."""
       )
       raise ValueError(__UpperCAmelCase				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=     [char for char in text.upper() if char in self.key_string]

    __lowerCamelCase													=     chars[-1]
    while len(__UpperCAmelCase				) % self.break_key != 0:
       chars.append(__UpperCAmelCase				)

    return "".join(__UpperCAmelCase				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=     self.process_text(text.upper()				)
    __lowerCamelCase													=     ''''''

    for i in range(0	,				len(__UpperCAmelCase				) - self.break_key + 1	,				self.break_key				):
       __lowerCamelCase													=     text[i : i + self.break_key]
       __lowerCamelCase													=     [self.replace_letters(__UpperCAmelCase				) for char in batch]
       __lowerCamelCase													=     numpy.array([vec]				).T
       __lowerCamelCase													=     self.modulus(self.encrypt_key.dot(__UpperCAmelCase				)				).T.tolist()[
           0
       ]
       __lowerCamelCase													=     ''''''.join(
           self.replace_digits(__UpperCAmelCase				) for num in batch_encrypted				)
       encrypted += encrypted_batch

    return encrypted




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=     round(numpy.linalg.det(self.encrypt_key				)				)

    if det < 0:
       __lowerCamelCase													=     det % len(self.key_string				)
    __lowerCamelCase													=     None
    for i in range(len(self.key_string				)				):
       if (det * i) % len(self.key_string				) == 1:
          __lowerCamelCase													=     i
          break

    __lowerCamelCase													=     (
        det_inv
        * numpy.linalg.det(self.encrypt_key				)
        * numpy.linalg.inv(self.encrypt_key				)
    )

    return self.to_int(self.modulus(__UpperCAmelCase				)				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=     self.make_decrypt_key()
    __lowerCamelCase													=     self.process_text(text.upper()				)
    __lowerCamelCase													=     ''''''

    for i in range(0	,				len(__UpperCAmelCase				) - self.break_key + 1	,				self.break_key				):
       __lowerCamelCase													=     text[i : i + self.break_key]
       __lowerCamelCase													=     [self.replace_letters(__UpperCAmelCase				) for char in batch]
       __lowerCamelCase													=     numpy.array([vec]				).T
       __lowerCamelCase													=     self.modulus(decrypt_key.dot(__UpperCAmelCase				)				).T.tolist()[0]
       __lowerCamelCase													=     ''''''.join(
           self.replace_digits(__UpperCAmelCase				) for num in batch_decrypted				)
       decrypted += decrypted_batch

    return decrypted





def 		a__			(		):
   __lowerCamelCase													=     int(input('''Enter the order of the encryption key: '''							)							)
   __lowerCamelCase													=     []

   print('''Enter each row of the encryption key with space separated integers'''							)
   for _ in range(_UpperCamelCase							):
      __lowerCamelCase													=     [int(_UpperCamelCase							) for x in input().split()]
      hill_matrix.append(_UpperCamelCase							)

   __lowerCamelCase													=     HillCipher(numpy.array(_UpperCamelCase							)							)

   print('''Would you like to encrypt or decrypt some text? (1 or 2)'''							)
   __lowerCamelCase													=     input('''\n1. Encrypt\n2. Decrypt\n'''							)
   if option == "1":
      __lowerCamelCase													=     input('''What text would you like to encrypt?: '''							)
      print('''Your encrypted text is:'''							)
      print(hc.encrypt(_UpperCamelCase							)							)
   elif option == "2":
      __lowerCamelCase													=     input('''What text would you like to decrypt?: '''							)
      print('''Your decrypted text is:'''							)
      print(hc.decrypt(_UpperCamelCase							)							)


if __name__ == "__main__":
  import doctest

  doctest.testmod()

  main()











import os
import unicodedata
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


a_			=					logging.get_logger(__name__)
a_			=					{"""vocab_file""": """spiece.model"""}

a_			=					{
    """vocab_file""": {
        """albert-base-v1""": """https://huggingface.co/albert-base-v1/resolve/main/spiece.model""",
        """albert-large-v1""": """https://huggingface.co/albert-large-v1/resolve/main/spiece.model""",
        """albert-xlarge-v1""": """https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model""",
        """albert-xxlarge-v1""": """https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model""",
        """albert-base-v2""": """https://huggingface.co/albert-base-v2/resolve/main/spiece.model""",
        """albert-large-v2""": """https://huggingface.co/albert-large-v2/resolve/main/spiece.model""",
        """albert-xlarge-v2""": """https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model""",
        """albert-xxlarge-v2""": """https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model""",
    }
}

a_			=					{
    """albert-base-v1""": 512,
    """albert-large-v1""": 512,
    """albert-xlarge-v1""": 512,
    """albert-xxlarge-v1""": 512,
    """albert-base-v2""": 512,
    """albert-large-v2""": 512,
    """albert-xlarge-v2""": 512,
    """albert-xxlarge-v2""": 512,
}

a_			=					"""▁"""



class  __lowerCAmelCase     (	lowerCAmelCase__   ):
 lowerCAmelCase__       					=  VOCAB_FILES_NAMES
 lowerCAmelCase__       					=  PRETRAINED_VOCAB_FILES_MAP
 lowerCAmelCase__       					=  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES




 def __init__(						self	,				__UpperCAmelCase	,				__UpperCAmelCase=True	,				__UpperCAmelCase=True	,				__UpperCAmelCase=False	,				__UpperCAmelCase="[CLS]"	,				__UpperCAmelCase="[SEP]"	,				__UpperCAmelCase="<unk>"	,				__UpperCAmelCase="[SEP]"	,				__UpperCAmelCase="<pad>"	,				__UpperCAmelCase="[CLS]"	,				__UpperCAmelCase="[MASK]"	,				__UpperCAmelCase = None	,				**__UpperCAmelCase	,				):



    '''simple docstring'''
    # Mask token behave like a normal word, i.e. include the space before it and
    # is included in the raw text, there should be a match in a non-normalized sentence.
    __lowerCamelCase													=    (
        AddedToken(__UpperCAmelCase	,				lstrip=__UpperCAmelCase	,				rstrip=__UpperCAmelCase	,				normalized=__UpperCAmelCase				)
        if isinstance(__UpperCAmelCase	,				__UpperCAmelCase				)
        else mask_token
    )

    __lowerCamelCase													=    {} if sp_model_kwargs is None else sp_model_kwargs

    super().__init__(
        do_lower_case=__UpperCAmelCase	,				remove_space=__UpperCAmelCase	,				keep_accents=__UpperCAmelCase	,				bos_token=__UpperCAmelCase	,				eos_token=__UpperCAmelCase	,				unk_token=__UpperCAmelCase	,				sep_token=__UpperCAmelCase	,				pad_token=__UpperCAmelCase	,				cls_token=__UpperCAmelCase	,				mask_token=__UpperCAmelCase	,				sp_model_kwargs=self.sp_model_kwargs	,				**__UpperCAmelCase	,				)

    __lowerCamelCase													=    do_lower_case
    __lowerCamelCase													=    remove_space
    __lowerCamelCase													=    keep_accents
    __lowerCamelCase													=    vocab_file

    __lowerCamelCase													=    spm.SentencePieceProcessor(**self.sp_model_kwargs				)
    self.sp_model.Load(__UpperCAmelCase				)




 @property
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    return len(self.sp_model				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    {self.convert_ids_to_tokens(__UpperCAmelCase				): i for i in range(self.vocab_size				)}
    vocab.update(self.added_tokens_encoder				)
    return vocab




 def __getstate__(						self				):



    '''simple docstring'''
    __lowerCamelCase													=    self.__dict__.copy()
    __lowerCamelCase													=    None
    return state




 def __setstate__(						self	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=    d

    # for backward compatibility
    if not hasattr(self	,				'''sp_model_kwargs'''				):
       __lowerCamelCase													=    {}

    __lowerCamelCase													=    spm.SentencePieceProcessor(**self.sp_model_kwargs				)
    self.sp_model.Load(self.vocab_file				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase				):



    '''simple docstring'''
    if self.remove_space:
       __lowerCamelCase													=    ''' '''.join(inputs.strip().split()				)
    else:
       __lowerCamelCase													=    inputs
    __lowerCamelCase													=    outputs.replace('''``'''	,				'''"'''				).replace('''\'\''''	,				'''"'''				)

    if not self.keep_accents:
       __lowerCamelCase													=    unicodedata.normalize('''NFKD'''	,				__UpperCAmelCase				)
       __lowerCamelCase													=    ''''''.join([c for c in outputs if not unicodedata.combining(__UpperCAmelCase				)]				)
    if self.do_lower_case:
       __lowerCamelCase													=    outputs.lower()

    return outputs




 def        lowerCamelCase      (						self	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=    self.preprocess_text(__UpperCAmelCase				)
    __lowerCamelCase													=    self.sp_model.encode(__UpperCAmelCase	,				out_type=__UpperCAmelCase				)
    __lowerCamelCase													=    []
    for piece in pieces:
       if len(__UpperCAmelCase				) > 1 and piece[-1] == str(''','''				) and piece[-2].isdigit():
          __lowerCamelCase													=    self.sp_model.EncodeAsPieces(piece[:-1].replace(__UpperCAmelCase	,				''''''				)				)
          if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
             if len(cur_pieces[0]				) == 1:
                __lowerCamelCase													=    cur_pieces[1:]
             else:
                __lowerCamelCase													=    cur_pieces[0][1:]
          cur_pieces.append(piece[-1]				)
          new_pieces.extend(__UpperCAmelCase				)
       else:
          new_pieces.append(__UpperCAmelCase				)

    return new_pieces




 def        lowerCamelCase      (						self	,				__UpperCAmelCase				):



    '''simple docstring'''
    return self.sp_model.PieceToId(__UpperCAmelCase				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase				):



    '''simple docstring'''
    return self.sp_model.IdToPiece(__UpperCAmelCase				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=    []
    __lowerCamelCase													=    ''''''
    __lowerCamelCase													=    False
    for token in tokens:
       # make sure that special tokens are not decoded using sentencepiece model
       if token in self.all_special_tokens:
          if not prev_is_special:
             out_string += " "
          out_string += self.sp_model.decode(__UpperCAmelCase				) + token
          __lowerCamelCase													=    True
          __lowerCamelCase													=    []
       else:
          current_sub_tokens.append(__UpperCAmelCase				)
          __lowerCamelCase													=    False
    out_string += self.sp_model.decode(__UpperCAmelCase				)
    return out_string.strip()




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase = None				):



    '''simple docstring'''
    __lowerCamelCase													=    [self.sep_token_id]
    __lowerCamelCase													=    [self.cls_token_id]
    if token_ids_a is None:
       return cls + token_ids_a + sep
    return cls + token_ids_a + sep + token_ids_a + sep




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase = None	,				__UpperCAmelCase = False				):



    '''simple docstring'''

    if already_has_special_tokens:
       return super().get_special_tokens_mask(
           token_ids_a=__UpperCAmelCase	,				token_ids_a=__UpperCAmelCase	,				already_has_special_tokens=__UpperCAmelCase				)

    if token_ids_a is not None:
       return [1] + ([0] * len(__UpperCAmelCase				)) + [1] + ([0] * len(__UpperCAmelCase				)) + [1]
    return [1] + ([0] * len(__UpperCAmelCase				)) + [1]




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase = None				):



    '''simple docstring'''
    __lowerCamelCase													=    [self.sep_token_id]
    __lowerCamelCase													=    [self.cls_token_id]

    if token_ids_a is None:
       return len(cls + token_ids_a + sep				) * [0]
    return len(cls + token_ids_a + sep				) * [0] + len(token_ids_a + sep				) * [1]




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase = None				):



    '''simple docstring'''
    if not os.path.isdir(__UpperCAmelCase				):
       logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""				)
       return
    __lowerCamelCase													=    os.path.join(
        __UpperCAmelCase	,				(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']				)

    if os.path.abspath(self.vocab_file				) != os.path.abspath(__UpperCAmelCase				) and os.path.isfile(self.vocab_file				):
       copyfile(self.vocab_file	,				__UpperCAmelCase				)
    elif not os.path.isfile(self.vocab_file				):
       with open(__UpperCAmelCase	,				'''wb'''				) as fi:
          __lowerCamelCase													=    self.sp_model.serialized_model_proto()
          fi.write(__UpperCAmelCase				)

    return (out_vocab_file,)






