





'''simple docstring'''

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


__lowerCAmelCase							:     Tuple		     =logging.get_logger(__name__)
__lowerCAmelCase							:     str		     ={"vocab_file": "spiece.model"}

__lowerCAmelCase							:     Union[str, Any]		     ={
    "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",
    }
}

__lowerCAmelCase							:     List[str]		     ={
    "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,
}

__lowerCAmelCase							:     Tuple		     ="▁"






class        UpperCAmelCase     (	UpperCamelCase__    ):
 __lowercase       		=    VOCAB_FILES_NAMES
 __lowercase       		=    PRETRAINED_VOCAB_FILES_MAP
 __lowercase       		=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 def __init__(    self				:Dict						,	lowercase_				:Dict						,	lowercase_				:Optional[Any]=True						,	lowercase_				:Union[str, Any]=True						,	lowercase_				:int=False						,	lowercase_				:Tuple="[CLS]"						,	lowercase_				:Optional[int]="[SEP]"						,	lowercase_				:List[Any]="<unk>"						,	lowercase_				:List[str]="[SEP]"						,	lowercase_				:Tuple="<pad>"						,	lowercase_				:Tuple="[CLS]"						,	lowercase_				:int="[MASK]"						,	lowercase_				:Optional[Dict[str, Any]] = None						,	**lowercase_				:Union[str, Any]						,	)->							None:
     # 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.
     A__    =      (
         AddedToken(_A						,	lstrip=_A						,	rstrip=_A						,	normalized=_A							)
         if isinstance(_A						,	_A							)
         else mask_token
     )

     A__    =      {} if sp_model_kwargs is None else sp_model_kwargs

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

     A__    =      do_lower_case
     A__    =      remove_space
     A__    =      keep_accents
     A__    =      vocab_file

     A__    =      spm.SentencePieceProcessor(**self.sp_model_kwargs							)
     self.sp_model.Load(_A							)
 @property
 def 					UpperCAmelCase_							(    self				:Any							)->							Optional[Any]:
     return len(self.sp_model							)
 def 					UpperCAmelCase_							(    self				:Dict							)->							Any:
     A__    =      {self.convert_ids_to_tokens(_A							): i for i in range(self.vocab_size							)}
     vocab.update(self.added_tokens_encoder							)
     return vocab
 def __getstate__(    self				:Any							)->							List[str]:
     A__    =      self.__dict__.copy()
     A__    =      None
     return state
 def __setstate__(    self				:Optional[int]						,	lowercase_				:Optional[int]							)->							Union[str, Any]:
     A__    =      d

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

     A__    =      spm.SentencePieceProcessor(**self.sp_model_kwargs							)
     self.sp_model.Load(self.vocab_file							)
 def 					UpperCAmelCase_							(    self				:Union[str, Any]						,	lowercase_				:List[str]							)->							str:
     if self.remove_space:
         A__    =      ' '.join(inputs.strip().split()							)
     else:
         A__    =      inputs
     A__    =      outputs.replace("``"						,	"\""							).replace("\'\'"						,	"\""							)

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

     return outputs
 def 					UpperCAmelCase_							(    self				:int						,	lowercase_				:str							)->							List[str]:
     A__    =      self.preprocess_text(_A							)
     A__    =      self.sp_model.encode(_A						,	out_type=_A							)
     A__    =      []
     for piece in pieces:
         if len(_A							) > 1 and piece[-1] == str(","							) and piece[-2].isdigit():
             A__    =      self.sp_model.EncodeAsPieces(piece[:-1].replace(_A						,	""							)							)
             if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
                 if len(cur_pieces[0]							) == 1:
                     A__    =      cur_pieces[1:]
                 else:
                     A__    =      cur_pieces[0][1:]
             cur_pieces.append(piece[-1]							)
             new_pieces.extend(_A							)
         else:
             new_pieces.append(_A							)

     return new_pieces
 def 					UpperCAmelCase_							(    self				:Optional[int]						,	lowercase_				:Union[str, Any]							)->							Union[str, Any]:
     return self.sp_model.PieceToId(_A							)
 def 					UpperCAmelCase_							(    self				:List[str]						,	lowercase_				:Tuple							)->							Tuple:
     return self.sp_model.IdToPiece(_A							)
 def 					UpperCAmelCase_							(    self				:List[Any]						,	lowercase_				:str							)->							str:
     A__    =      []
     A__    =      ''
     A__    =      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(_A							) + token
             A__    =      True
             A__    =      []
         else:
             current_sub_tokens.append(_A							)
             A__    =      False
     out_string += self.sp_model.decode(_A							)
     return out_string.strip()
 def 					UpperCAmelCase_							(    self				:Union[str, Any]						,	lowercase_				:List[int]						,	lowercase_				:Optional[List[int]] = None							)->							List[int]:
     A__    =      [self.sep_token_id]
     A__    =      [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 					UpperCAmelCase_							(    self				:List[Any]						,	lowercase_				:List[int]						,	lowercase_				:Optional[List[int]] = None						,	lowercase_				:bool = False							)->							List[int]:

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

     if token_ids_a is not None:
         return [1] + ([0] * len(_A							)) + [1] + ([0] * len(_A							)) + [1]
     return [1] + ([0] * len(_A							)) + [1]
 def 					UpperCAmelCase_							(    self				:Union[str, Any]						,	lowercase_				:List[int]						,	lowercase_				:Optional[List[int]] = None							)->							List[int]:
     A__    =      [self.sep_token_id]
     A__    =      [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 					UpperCAmelCase_							(    self				:List[str]						,	lowercase_				:str						,	lowercase_				:Optional[str] = None							)->							Tuple[str]:
     if not os.path.isdir(_A							):
         logger.error(F"Vocabulary path ({save_directory}) should be a directory"							)
         return
     A__    =      os.path.join(
         _A						,	(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]							)

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

     return (out_vocab_file,)










'''simple docstring'''

import os
import random
import sys

from . import cryptomath_module as cryptoMath  # noqa: N812
from . import rabin_miller as rabinMiller  # noqa: N812





def 						UpperCamelCase    (							):
    print("Making key files..."	)
    make_key_files("rsa"						,	10_24	)
    print("Key files generation successful."	)





def 						UpperCamelCase    (							_lowerCamelCase : int	):
    print("Generating prime p..."	)
    A__    =      rabinMiller.generate_large_prime(_lowerCamelCase	)
    print("Generating prime q..."	)
    A__    =      rabinMiller.generate_large_prime(_lowerCamelCase	)
    A__    =      p * q

    print("Generating e that is relatively prime to (p - 1) * (q - 1)..."	)
    while True:
        A__    =      random.randrange(2 ** (key_size - 1)						,	2 ** (key_size)	)
        if cryptoMath.gcd(_lowerCamelCase						,	(p - 1) * (q - 1)	) == 1:
            break

    print("Calculating d that is mod inverse of e..."	)
    A__    =      cryptoMath.find_mod_inverse(_lowerCamelCase						,	(p - 1) * (q - 1)	)

    A__    =      (n, e)
    A__    =      (n, d)
    return (public_key, private_key)





def 						UpperCamelCase    (							_lowerCamelCase : str						,	_lowerCamelCase : int	):
    if os.path.exists(F"{name}_pubkey.txt"	) or os.path.exists(F"{name}_privkey.txt"	):
        print("\nWARNING:"	)
        print(
            F"\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n"
            "Use a different name or delete these files and re-run this program."	)
        sys.exit()

    A__,     A__    =      generate_key(_lowerCamelCase	)
    print(F"\nWriting public key to file {name}_pubkey.txt..."	)
    with open(F"{name}_pubkey.txt"						,	"w"	) as out_file:
        out_file.write(F"{key_size},{public_key[0]},{public_key[1]}"	)

    print(F"Writing private key to file {name}_privkey.txt..."	)
    with open(F"{name}_privkey.txt"						,	"w"	) as out_file:
        out_file.write(F"{key_size},{private_key[0]},{private_key[1]}"	)


if __name__ == "__main__":
  main()



