


"""simple docstring"""







import io
import os
import unicodedata
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


lowerCAmelCase__      				=   logging.get_logger(__name__)

lowerCAmelCase__      				=   '''▁'''

lowerCAmelCase__      				=   {'''vocab_file''': '''vocab.txt''', '''sentencepiece_model_ckpt''': '''sentencepiece.bpe.model'''}

lowerCAmelCase__      				=   {
    '''sentencepiece_model_file''': '''sentencepiece.bpe.model''',
    '''vocab_file''': '''vocab.txt''',
}

lowerCAmelCase__      				=   {
    '''vocab_file''': {
        '''ernie-m-base''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt''',
        '''ernie-m-large''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt''',
    },
    '''sentencepiece_model_file''': {
        '''ernie-m-base''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model''',
        '''ernie-m-large''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model''',
    },
}

lowerCAmelCase__      				=   {
    '''ernie-m-base''': 514,
    '''ernie-m-large''': 514,
}

lowerCAmelCase__      				=   {
    '''ernie-m-base''': {'''do_lower_case''': False},
    '''ernie-m-large''': {'''do_lower_case''': False},
}


class       SCREAMING_SNAKE_CASE__				(		lowercase				):
       """simple docstring"""


       a  :				List[str]   =["input_ids"]

       a  :				Dict   =VOCAB_FILES_NAMES
       a  :				Optional[Any]   =PRETRAINED_INIT_CONFIGURATION
       a  :				Any   =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       a  :				Optional[int]   =PRETRAINED_VOCAB_FILES_MAP
       a  :				Tuple   =RESOURCE_FILES_NAMES

       def __init__(						self		,     snake_case__		,     snake_case__=None		,     snake_case__=False		,     snake_case__="utf8"		,     snake_case__="[UNK]"		,     snake_case__="[SEP]"		,     snake_case__="[PAD]"		,     snake_case__="[CLS]"		,     snake_case__="[MASK]"		,     snake_case__ = None		,     **snake_case__		,     ):






         """simple docstring"""





         lowerCAmelCase     :     int								=							{} if sp_model_kwargs is None else sp_model_kwargs
         super().__init__(
             do_lower_case=snake_case__		,     unk_token=snake_case__		,     sep_token=snake_case__		,     pad_token=snake_case__		,     cls_token=snake_case__		,     mask_token=snake_case__		,     vocab_file=snake_case__		,     encoding=snake_case__		,     sp_model_kwargs=self.sp_model_kwargs		,     **snake_case__		,     )
         lowerCAmelCase     :     Union[str, Any]								=							do_lower_case
         lowerCAmelCase     :     Tuple								=							sentencepiece_model_ckpt
         lowerCAmelCase     :     Tuple								=							spm.SentencePieceProcessor(**self.sp_model_kwargs						)
         self.sp_model.Load(snake_case__						)

         # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning
         if vocab_file is not None:
           lowerCAmelCase     :     Dict								=							self.load_vocab(filepath=snake_case__						)
         else:
           lowerCAmelCase     :     List[str]								=							{self.sp_model.id_to_piece(snake_case__						): id for id in range(self.sp_model.get_piece_size()						)}
         lowerCAmelCase     :     Dict								=							{v: k for k, v in self.vocab.items()}

       def        lowercase__							(						self		,     snake_case__						):






         """simple docstring"""





         if text is None:
           return None

         lowerCAmelCase     :     Optional[int]								=							self.tokenize(snake_case__						)
         lowerCAmelCase						,  lowerCAmelCase     :     Optional[int]								=							"", []

         for i, ch in enumerate(snake_case__						):
           if ch in self.SP_CHAR_MAPPING:
             lowerCAmelCase     :     Optional[Any]								=							self.SP_CHAR_MAPPING.get(snake_case__						)
           else:
             lowerCAmelCase     :     Tuple								=							unicodedata.normalize("NFKC"		,     snake_case__						)
           if self.is_whitespace(snake_case__						):
             continue
           normalized_text += ch
           char_mapping.extend([i] * len(snake_case__						)						)

         lowerCAmelCase						,  lowerCAmelCase						,  lowerCAmelCase     :     Union[str, Any]								=							normalized_text, [], 0

         if self.do_lower_case:
           lowerCAmelCase     :     Any								=							text.lower()

         for token in split_tokens:
           if token[:1] == "▁":
             lowerCAmelCase     :     Optional[int]								=							token[1:]
           lowerCAmelCase     :     Tuple								=							text[offset:].index(snake_case__						) + offset
           lowerCAmelCase     :     Optional[Any]								=							start + len(snake_case__						)

           token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1)						)
           lowerCAmelCase     :     Tuple								=							end
         return token_mapping

       @property
       def        lowercase__							(						self						):






         """simple docstring"""





         return len(self.vocab						)

       def        lowercase__							(						self						):






         """simple docstring"""





         return dict(self.vocab		,     **self.added_tokens_encoder						)

       def __getstate__(						self						):






         """simple docstring"""





         lowerCAmelCase     :     List[str]								=							self.__dict__.copy()
         lowerCAmelCase     :     int								=							None
         return state

       def __setstate__(						self		,     snake_case__						):






         """simple docstring"""





         lowerCAmelCase     :     List[str]								=							d

         # for backward compatibility
         if not hasattr(self		,     "sp_model_kwargs"						):
           lowerCAmelCase     :     Optional[int]								=							{}

         lowerCAmelCase     :     int								=							spm.SentencePieceProcessor(**self.sp_model_kwargs						)
         self.sp_model.Load(self.sentencepiece_model_ckpt						)

       def        lowercase__							(						self		,     snake_case__						):






         """simple docstring"""





         return "".join((self.SP_CHAR_MAPPING.get(snake_case__		,     snake_case__						) for c in text)						)

       def        lowercase__							(						self		,     snake_case__		,     snake_case__=False		,     snake_case__=64		,     snake_case__=0.1						):






         """simple docstring"""





         if self.sp_model_kwargs.get("enable_sampling"						) is True:
           lowerCAmelCase     :     List[Any]								=							True
         if self.sp_model_kwargs.get("alpha"						) is not None:
           lowerCAmelCase     :     Any								=							self.sp_model_kwargs.get("alpha"						)
         if self.sp_model_kwargs.get("nbest_size"						) is not None:
           lowerCAmelCase     :     Optional[Any]								=							self.sp_model_kwargs.get("nbest_size"						)

         if not enable_sampling:
           lowerCAmelCase     :     int								=							self.sp_model.EncodeAsPieces(snake_case__						)
         else:
           lowerCAmelCase     :     Dict								=							self.sp_model.SampleEncodeAsPieces(snake_case__		,     snake_case__		,     snake_case__						)
         lowerCAmelCase     :     Optional[Any]								=							[]
         for pi, piece in enumerate(snake_case__						):
           if piece == SPIECE_UNDERLINE:
             if not pieces[pi + 1].startswith(snake_case__						) and pi != 0:
               new_pieces.append(snake_case__						)
               continue
             else:
               continue
           lowerCAmelCase     :     List[Any]								=							0
           for i, chunk in enumerate(snake_case__						):
             if chunk == SPIECE_UNDERLINE:
               continue
             if self.is_ch_char(snake_case__						) or self.is_punct(snake_case__						):
               if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
                 new_pieces.append(piece[lst_i:i]						)
               new_pieces.append(snake_case__						)
               lowerCAmelCase     :     List[str]								=							i + 1
             elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit():
               if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
                 new_pieces.append(piece[lst_i:i]						)
               lowerCAmelCase     :     Dict								=							i
             elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit():
               if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
                 new_pieces.append(piece[lst_i:i]						)
               lowerCAmelCase     :     Union[str, Any]								=							i
           if len(snake_case__						) > lst_i:
             new_pieces.append(piece[lst_i:]						)
         return new_pieces

       def        lowercase__							(						self		,     snake_case__						):






         """simple docstring"""





         lowerCAmelCase     :     str								=							"".join(snake_case__						).replace(snake_case__		,     " "						).strip()
         return out_string

       def        lowercase__							(						self		,     snake_case__						):






         """simple docstring"""





         lowerCAmelCase     :     Any								=							self.convert_ids_to_tokens(snake_case__						)
         lowerCAmelCase     :     str								=							"".join(snake_case__						).replace(snake_case__		,     " "						).strip()
         return out_string

       def        lowercase__							(						self		,     snake_case__						):






         """simple docstring"""





         return self.vocab.get(snake_case__		,     self.vocab.get(self.unk_token						)						)

       def        lowercase__							(						self		,     snake_case__						):






         """simple docstring"""





         return self.reverse_vocab.get(snake_case__		,     self.unk_token						)

       def        lowercase__							(						self		,     snake_case__		,     snake_case__=None						):






         """simple docstring"""





         if token_ids_a is None:
           return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
         lowerCAmelCase     :     Optional[Any]								=							[self.cls_token_id]
         lowerCAmelCase     :     Any								=							[self.sep_token_id]
         return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep

       def        lowercase__							(						self		,     snake_case__		,     snake_case__=None						):






         """simple docstring"""





         if offset_mapping_a is None:
           return [(0, 0)] + offset_mapping_a + [(0, 0)]

         return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)]

       def        lowercase__							(						self		,     snake_case__		,     snake_case__=None		,     snake_case__=False						):






         """simple docstring"""





         if already_has_special_tokens:
           if token_ids_a is not None:
             raise ValueError(
                 "You should not supply a second sequence if the provided sequence of "
                 "ids is already formatted with special tokens for the model."						)
           return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a]

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

       def        lowercase__							(						self		,     snake_case__		,     snake_case__ = None						):






         """simple docstring"""





         if token_ids_a is None:
           # [CLS] X [SEP]
           return (len(snake_case__						) + 2) * [0]

         # [CLS] A [SEP] [SEP] B [SEP]
         return [0] * (len(snake_case__						) + 1) + [1] * (len(snake_case__						) + 3)

       def        lowercase__							(						self		,     snake_case__						):






         """simple docstring"""





         if "\u4e00" <= char <= "\u9fff":
           return True
         return False

       def        lowercase__							(						self		,     snake_case__						):






         """simple docstring"""





         if ("a" <= char <= "z") or ("A" <= char <= "Z"):
           return True
         return False

       def        lowercase__							(						self		,     snake_case__						):






         """simple docstring"""





         if char in ",;:.?!~，；：。？！《》【】":
           return True
         return False

       def        lowercase__							(						self		,     snake_case__						):






         """simple docstring"""





         if char == " " or char == "\t" or char == "\n" or char == "\r":
           return True
         if len(snake_case__						) == 1:
           lowerCAmelCase     :     Dict								=							unicodedata.category(snake_case__						)
           if cat == "Zs":
             return True
         return False

       def        lowercase__							(						self		,     snake_case__						):






         """simple docstring"""





         lowerCAmelCase     :     Union[str, Any]								=							{}
         with io.open(snake_case__		,     "r"		,     encoding="utf-8"						) as f:
           for index, line in enumerate(snake_case__						):
             lowerCAmelCase     :     Tuple								=							line.rstrip("\n"						)
             lowerCAmelCase     :     Optional[Any]								=							int(snake_case__						)

         return token_to_idx







       def        lowercase__							(						self		,     snake_case__		,     snake_case__ = None						):






         """simple docstring"""





         lowerCAmelCase     :     List[Any]								=							0
         if os.path.isdir(snake_case__						):
           lowerCAmelCase     :     Optional[Any]								=							os.path.join(
               snake_case__		,     (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]						)
         else:
           lowerCAmelCase     :     int								=							(filename_prefix + "-" if filename_prefix else "") + save_directory
         with open(snake_case__		,     "w"		,     encoding="utf-8"						) as writer:
           for token, token_index in sorted(self.vocab.items()		,     key=lambda snake_case__						: kv[1]						):
             if index != token_index:
               logger.warning(
                   f"""Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive."""
                   " Please check that the vocabulary is not corrupted!"						)
               lowerCAmelCase     :     int								=							token_index
             writer.write(token + "\n"						)
             index += 1

         lowerCAmelCase     :     List[str]								=							os.path.join(snake_case__		,     "sentencepiece.bpe.model"						)
         with open(snake_case__		,     "wb"						) as fi:
           lowerCAmelCase     :     Tuple								=							self.sp_model.serialized_model_proto()
           fi.write(snake_case__						)

         return (vocab_file,)









"""simple docstring"""







def 					a__			(				SCREAMING_SNAKE_CASE    :    float				,      SCREAMING_SNAKE_CASE    :    float				,      SCREAMING_SNAKE_CASE    :    int				):


  '''simple docstring'''







  if principal <= 0:
    raise Exception("Principal borrowed must be > 0"				)
  if rate_per_annum < 0:
    raise Exception("Rate of interest must be >= 0"				)
  if years_to_repay <= 0 or not isinstance(SCREAMING_SNAKE_CASE				,      SCREAMING_SNAKE_CASE				):
    raise Exception("Years to repay must be an integer > 0"				)

  # Yearly rate is divided by 12 to get monthly rate
  lowerCAmelCase     :     Tuple								=							rate_per_annum / 1_2

  # Years to repay is multiplied by 12 to get number of payments as payment is monthly
  lowerCAmelCase     :     List[Any]								=							years_to_repay * 1_2

  return (
      principal
      * rate_per_month
      * (1 + rate_per_month) ** number_of_payments
      / ((1 + rate_per_month) ** number_of_payments - 1)
  )


if __name__ == "__main__":
     import doctest

     doctest.testmod()





