

'''simple docstring'''





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


__snake_case	: Dict       				=							{}


try:
							if not is_sentencepiece_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__snake_case	: List[Any]       				=							["MLukeTokenizer"]

if TYPE_CHECKING:
							try:
														if not is_sentencepiece_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_mluke import MLukeTokenizer


else:
							import sys

							__snake_case	: Union[str, Any]       				=							_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)







"""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


__SCREAMING_SNAKE_CASE							=     logging.get_logger(__name__)
__SCREAMING_SNAKE_CASE							=     {'vocab_file': 'spiece.model'}

__SCREAMING_SNAKE_CASE							=     {
    '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',
    }
}

__SCREAMING_SNAKE_CASE							=     {
    '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,
}

__SCREAMING_SNAKE_CASE							=     '▁'





class 				a__					(						A__       ):
     UpperCAmelCase__         =      VOCAB_FILES_NAMES
     UpperCAmelCase__         =      PRETRAINED_VOCAB_FILES_MAP
     UpperCAmelCase__         =      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES



     def __init__(							self :Optional[Any]      ,  _lowerCamelCase :int      ,  _lowerCamelCase :List[Any]=True      ,  _lowerCamelCase :int=True      ,  _lowerCamelCase :Tuple=False      ,  _lowerCamelCase :Optional[Any]="[CLS]"      ,  _lowerCamelCase :List[Any]="[SEP]"      ,  _lowerCamelCase :Dict="<unk>"      ,  _lowerCamelCase :int="[SEP]"      ,  _lowerCamelCase :int="<pad>"      ,  _lowerCamelCase :Optional[Any]="[CLS]"      ,  _lowerCamelCase :str="[MASK]"      ,  _lowerCamelCase :Optional[Dict[str, Any]] = None      ,  **_lowerCamelCase :Union[str, Any]      ,  ):







      '''simple docstring'''



      UpperCamelCase_     :					Union[str, Any]   	=(
          AddedToken(_lowerCamelCase      ,  lstrip=_lowerCamelCase      ,  rstrip=_lowerCamelCase      ,  normalized=_lowerCamelCase   )
          if isinstance(_lowerCamelCase      ,  _lowerCamelCase   )
          else mask_token
      )

      UpperCamelCase_     :					Any   	={} if sp_model_kwargs is None else sp_model_kwargs

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

      UpperCamelCase_     :					str   	=do_lower_case
      UpperCamelCase_     :					Any   	=remove_space
      UpperCamelCase_     :					List[Any]   	=keep_accents
      UpperCamelCase_     :					Optional[Any]   	=vocab_file

      UpperCamelCase_     :					List[str]   	=spm.SentencePieceProcessor(**self.sp_model_kwargs   )
      self.sp_model.Load(_lowerCamelCase   )



     @property
     def  lowerCamelCase_						(							self :List[str]   ):







      '''simple docstring'''



      return len(self.sp_model   )



     def  lowerCamelCase_						(							self :Optional[Any]   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[int]   	={self.convert_ids_to_tokens(_lowerCamelCase   ): i for i in range(self.vocab_size   )}
      vocab.update(self.added_tokens_encoder   )
      return vocab



     def __getstate__(							self :Optional[int]   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[Any]   	=self.__dict__.copy()
      UpperCamelCase_     :					Optional[Any]   	=None
      return state



     def __setstate__(							self :List[Any]      ,  _lowerCamelCase :Any   ):







      '''simple docstring'''



      UpperCamelCase_     :					Union[str, Any]   	=d

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

      UpperCamelCase_     :					List[Any]   	=spm.SentencePieceProcessor(**self.sp_model_kwargs   )
      self.sp_model.Load(self.vocab_file   )



     def  lowerCamelCase_						(							self :Union[str, Any]      ,  _lowerCamelCase :Tuple   ):







      '''simple docstring'''



      if self.remove_space:
       UpperCamelCase_     :					Union[str, Any]   	=' '.join(inputs.strip().split()   )
      else:
       UpperCamelCase_     :					List[Any]   	=inputs
      UpperCamelCase_     :					Optional[int]   	=outputs.replace('``'      ,  '"'   ).replace('\'\''      ,  '"'   )

      if not self.keep_accents:
       UpperCamelCase_     :					Optional[Any]   	=unicodedata.normalize('NFKD'      ,  _lowerCamelCase   )
       UpperCamelCase_     :					Tuple   	=''.join([c for c in outputs if not unicodedata.combining(_lowerCamelCase   )]   )
      if self.do_lower_case:
       UpperCamelCase_     :					Optional[Any]   	=outputs.lower()

      return outputs



     def  lowerCamelCase_						(							self :Dict      ,  _lowerCamelCase :str   ):







      '''simple docstring'''



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

      return new_pieces



     def  lowerCamelCase_						(							self :Dict      ,  _lowerCamelCase :List[str]   ):







      '''simple docstring'''



      return self.sp_model.PieceToId(_lowerCamelCase   )



     def  lowerCamelCase_						(							self :Optional[Any]      ,  _lowerCamelCase :Union[str, Any]   ):







      '''simple docstring'''



      return self.sp_model.IdToPiece(_lowerCamelCase   )



     def  lowerCamelCase_						(							self :Union[str, Any]      ,  _lowerCamelCase :Optional[int]   ):







      '''simple docstring'''



      UpperCamelCase_     :					int   	=[]
      UpperCamelCase_     :					int   	=''
      UpperCamelCase_     :					int   	=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(_lowerCamelCase   ) + token
        UpperCamelCase_     :					List[Any]   	=True
        UpperCamelCase_     :					str   	=[]
       else:
        current_sub_tokens.append(_lowerCamelCase   )
        UpperCamelCase_     :					List[Any]   	=False
      out_string += self.sp_model.decode(_lowerCamelCase   )
      return out_string.strip()



     def  lowerCamelCase_						(							self :Optional[int]      ,  _lowerCamelCase :List[int]      ,  _lowerCamelCase :Optional[List[int]] = None   ):







      '''simple docstring'''



      UpperCamelCase_     :					Tuple   	=[self.sep_token_id]
      UpperCamelCase_     :					Tuple   	=[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 :Union[str, Any]      ,  _lowerCamelCase :List[int]      ,  _lowerCamelCase :Optional[List[int]] = None      ,  _lowerCamelCase :bool = False   ):







      '''simple docstring'''



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

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



     def  lowerCamelCase_						(							self :List[Any]      ,  _lowerCamelCase :List[int]      ,  _lowerCamelCase :Optional[List[int]] = None   ):







      '''simple docstring'''



      UpperCamelCase_     :					Tuple   	=[self.sep_token_id]
      UpperCamelCase_     :					List[str]   	=[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 :Tuple      ,  _lowerCamelCase :str      ,  _lowerCamelCase :Optional[str] = None   ):







      '''simple docstring'''



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

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

      return (out_vocab_file,)
