
'''simple docstring'''



import os
import re
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": {
        "google/fnet-base": "https://huggingface.co/google/fnet-base/resolve/main/spiece.model",
        "google/fnet-large": "https://huggingface.co/google/fnet-large/resolve/main/spiece.model",
    },
}

__A       =      {
    "google/fnet-base": 512,
    "google/fnet-large": 512,
}

__A       =      "▁"





class      A							(    __UpperCAmelCase			):
			lowerCamelCase							:							List[Any]    						=					VOCAB_FILES_NAMES
			lowerCamelCase							:							Optional[Any]    						=					PRETRAINED_VOCAB_FILES_MAP
			lowerCamelCase							:							Optional[int]    						=					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			lowerCamelCase							:							Union[str, Any]    						=					["input_ids", "token_type_ids"]



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




									'''simple docstring'''



									lowercase__								=    (
									    AddedToken(lowercase__    ,       lstrip=lowercase__    ,       rstrip=lowercase__    ,       normalized=lowercase__    )
									    if isinstance(lowercase__    ,       lowercase__    )
									    else mask_token
									)

									lowercase__								=    {} if sp_model_kwargs is None else sp_model_kwargs

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

									lowercase__								=    do_lower_case
									lowercase__								=    remove_space
									lowercase__								=    keep_accents
									lowercase__								=    vocab_file

									lowercase__								=    spm.SentencePieceProcessor(**self.sp_model_kwargs    )
									self.sp_model.Load(lowercase__    )



			@property
			def A__	(   self    )     ->						List[str]:




									'''simple docstring'''



									return len(self.sp_model    )



			def A__	(   self    )     ->						int:




									'''simple docstring'''



									lowercase__								=    {self.convert_ids_to_tokens(lowercase__    ): i for i in range(self.vocab_size    )}
									vocab.update(self.added_tokens_encoder    )
									return vocab



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




									'''simple docstring'''



									lowercase__								=    self.__dict__.copy()
									lowercase__								=    None
									return state



			def __setstate__(   self    ,       lowerCamelCase__    )     ->						Dict:




									'''simple docstring'''



									lowercase__								=    d

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

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



			def A__	(   self    ,       lowerCamelCase__    )     ->						Tuple:




									'''simple docstring'''



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

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

									return outputs



			def A__	(   self    ,       lowerCamelCase__    )     ->						List[str]:




									'''simple docstring'''



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

									return new_pieces



			def A__	(   self    ,       lowerCamelCase__    )     ->						Optional[Any]:




									'''simple docstring'''



									return self.sp_model.PieceToId(lowercase__    )



			def A__	(   self    ,       lowerCamelCase__    )     ->						Optional[Any]:




									'''simple docstring'''



									return self.sp_model.IdToPiece(lowercase__    )



			def A__	(   self    ,       lowerCamelCase__    )     ->						List[str]:




									'''simple docstring'''



									lowercase__								=    []
									lowercase__								=    """"""
									lowercase__								=    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(lowercase__    ) + token
																					lowercase__								=    True
																					lowercase__								=    []
															else:
																					current_sub_tokens.append(lowercase__    )
																					lowercase__								=    False
									out_string += self.sp_model.decode(lowercase__    )
									return out_string.strip()



			def A__	(   self    ,       lowerCamelCase__    ,       lowerCamelCase__ = False    ,       lowerCamelCase__ = None    ,       lowerCamelCase__ = True    ,       **lowerCamelCase__    ,       )     ->						str:




									'''simple docstring'''



									lowercase__								=    kwargs.pop("""use_source_tokenizer"""    ,       lowercase__    )

									lowercase__								=    self.convert_ids_to_tokens(lowercase__    ,       skip_special_tokens=lowercase__    )

									# To avoid mixing byte-level and unicode for byte-level BPT
									# we need to build string separately for added tokens and byte-level tokens
									# cf. https://github.com/huggingface/transformers/issues/1133
									lowercase__								=    []
									lowercase__								=    []
									for token in filtered_tokens:
															if skip_special_tokens and token in self.all_special_ids:
																					continue
															if token in self.added_tokens_encoder:
																					if current_sub_text:
																											sub_texts.append(self.convert_tokens_to_string(lowercase__    )    )
																											lowercase__								=    []
																					sub_texts.append(lowercase__    )
															else:
																					current_sub_text.append(lowercase__    )
									if current_sub_text:
															sub_texts.append(self.convert_tokens_to_string(lowercase__    )    )

									# Mimic the behavior of the Rust tokenizer:
									# No space after <unk>
									if spaces_between_special_tokens:
															lowercase__								=    re.sub(R"""(<unk>) """    ,       R"""\1"""    ,       """ """.join(lowercase__    )    )
									else:
															lowercase__								=    """""".join(lowercase__    )

									lowercase__								=    (
									    clean_up_tokenization_spaces
									    if clean_up_tokenization_spaces is not None
									    else self.clean_up_tokenization_spaces
									)
									if clean_up_tokenization_spaces:
															lowercase__								=    self.clean_up_tokenization(lowercase__    )
															return clean_text
									else:
															return text



			def A__	(   self    ,       lowerCamelCase__    ,       lowerCamelCase__ = None    )     ->						List[int]:




									'''simple docstring'''



									lowercase__								=    [self.sep_token_id]
									lowercase__								=    [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 A__	(   self    ,       lowerCamelCase__    ,       lowerCamelCase__ = None    ,       lowerCamelCase__ = False    )     ->						List[int]:




									'''simple docstring'''



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

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



			def A__	(   self    ,       lowerCamelCase__    ,       lowerCamelCase__ = None    )     ->						List[int]:




									'''simple docstring'''



									lowercase__								=    [self.sep_token_id]
									lowercase__								=    [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 A__	(   self    ,       lowerCamelCase__    ,       lowerCamelCase__ = None    )     ->						Tuple[str]:




									'''simple docstring'''



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

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

									return (out_vocab_file,)








'''simple docstring'''



import os
import sys

from . import rsa_key_generator as rkg

__A       =      128
__A       =      256





def 	_A						(							lowercase__						,						lowercase__ = DEFAULT_BLOCK_SIZE		):
						lowercase__								=    message.encode("""ascii"""		)

						lowercase__								=    []
						for block_start in range(0						,						len(lowercase__		)						,						lowercase__		):
												lowercase__								=    0
												for i in range(lowercase__						,						min(block_start + block_size						,						len(lowercase__		)		)		):
																		block_int += message_bytes[i] * (BYTE_SIZE ** (i % block_size))
												block_ints.append(lowercase__		)
						return block_ints





def 	_A						(							lowercase__						,						lowercase__						,						lowercase__ = DEFAULT_BLOCK_SIZE		):
						lowercase__								=    []
						for block_int in block_ints:
												lowercase__								=    []
												for i in range(block_size - 1						,						-1						,						-1		):
																		if len(lowercase__		) + i < message_length:
																								lowercase__								=    block_int // (BYTE_SIZE**i)
																								lowercase__								=    block_int % (BYTE_SIZE**i)
																								block_message.insert(0						,						chr(lowercase__		)		)
												message.extend(lowercase__		)
						return "".join(lowercase__		)





def 	_A						(							lowercase__						,						lowercase__						,						lowercase__ = DEFAULT_BLOCK_SIZE		):
						lowercase__								=    []
						lowercase__	,  lowercase__								=    key

						for block in get_blocks_from_text(lowercase__						,						lowercase__		):
												encrypted_blocks.append(pow(lowercase__						,						lowercase__						,						lowercase__		)		)
						return encrypted_blocks





def 	_A						(							lowercase__						,						lowercase__						,						lowercase__						,						lowercase__ = DEFAULT_BLOCK_SIZE						,						):
						lowercase__								=    []
						lowercase__	,  lowercase__								=    key
						for block in encrypted_blocks:
												decrypted_blocks.append(pow(lowercase__						,						lowercase__						,						lowercase__		)		)
						return get_text_from_blocks(lowercase__						,						lowercase__						,						lowercase__		)





def 	_A						(							lowercase__		):
						with open(lowercase__		) as fo:
												lowercase__								=    fo.read()
						lowercase__	,  lowercase__	,  lowercase__								=    content.split(""","""		)
						return (int(lowercase__		), int(lowercase__		), int(lowercase__		))





def 	_A						(							lowercase__						,						lowercase__						,						lowercase__						,						lowercase__ = DEFAULT_BLOCK_SIZE						,						):
						lowercase__	,  lowercase__	,  lowercase__								=    read_key_file(lowercase__		)
						if key_size < block_size * 8:
												sys.exit(
												    """ERROR: Block size is {} bits and key size is {} bits. The RSA cipher """
												    """requires the block size to be equal to or greater than the key size. """
												    """Either decrease the block size or use different keys.""".format(
												        block_size * 8						,						lowercase__		)		)

						lowercase__								=    [str(lowercase__		) for i in encrypt_message(lowercase__						,						(n, e)						,						lowercase__		)]

						lowercase__								=    """,""".join(lowercase__		)
						lowercase__								=    f'''{len(lowercase__		)}_{block_size}_{encrypted_content}'''
						with open(lowercase__						,						"""w"""		) as fo:
												fo.write(lowercase__		)
						return encrypted_content





def 	_A						(							lowercase__						,						lowercase__		):
						lowercase__	,  lowercase__	,  lowercase__								=    read_key_file(lowercase__		)
						with open(lowercase__		) as fo:
												lowercase__								=    fo.read()
						lowercase__	,  lowercase__	,  lowercase__								=    content.split("""_"""		)
						lowercase__								=    int(lowercase__		)
						lowercase__								=    int(lowercase__		)

						if key_size < block_size * 8:
												sys.exit(
												    """ERROR: Block size is {} bits and key size is {} bits. The RSA cipher """
												    """requires the block size to be equal to or greater than the key size. """
												    """Did you specify the correct key file and encrypted file?""".format(
												        block_size * 8						,						lowercase__		)		)

						lowercase__								=    []
						for block in encrypted_message.split(""","""		):
												encrypted_blocks.append(int(lowercase__		)		)

						return decrypt_message(lowercase__						,						lowercase__						,						(n, d)						,						lowercase__		)





def 	_A						(							):
						lowercase__								=    """encrypted_file.txt"""
						lowercase__								=    input(r"""Encrypt\Decrypt [e\d]: """		)

						if response.lower().startswith("""e"""		):
												lowercase__								=    """encrypt"""
						elif response.lower().startswith("""d"""		):
												lowercase__								=    """decrypt"""

						if mode == "encrypt":
												if not os.path.exists("""rsa_pubkey.txt"""		):
																		rkg.make_key_files("""rsa"""						,						1024		)

												lowercase__								=    input("""\nEnter message: """		)
												lowercase__								=    """rsa_pubkey.txt"""
												print(f'''Encrypting and writing to {filename}...'''		)
												lowercase__								=    encrypt_and_write_to_file(lowercase__						,						lowercase__						,						lowercase__		)

												print("""\nEncrypted text:"""		)
												print(lowercase__		)

						elif mode == "decrypt":
												lowercase__								=    """rsa_privkey.txt"""
												print(f'''Reading from {filename} and decrypting...'''		)
												lowercase__								=    read_from_file_and_decrypt(lowercase__						,						lowercase__		)
												print("""writing decryption to rsa_decryption.txt..."""		)
												with open("""rsa_decryption.txt"""						,						"""w"""		) as dec:
																		dec.write(lowercase__		)

												print("""\nDecryption:"""		)
												print(lowercase__		)


if __name__ == "__main__":
				main()






