'''simple docstring'''




_a   :	Dict 							=   [
    "VerificationMode",
    "Version",
    "disable_progress_bar",
    "enable_progress_bar",
    "is_progress_bar_enabled",
    "experimental",
]

from .info_utils import VerificationMode
from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled
from .version import Version
from .experimental import experimental







'''simple docstring'''




import os
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   :	str 							=   logging.get_logger(__name__)

_a   :	Tuple 							=   "▁"

_a   :	Optional[int] 							=   {"vocab_file": "sentencepiece.bpe.model"}

_a   :	Tuple 							=   {
    "vocab_file": {
        "xlm-roberta-base": "https://huggingface.co/xlm-roberta-base/resolve/main/sentencepiece.bpe.model",
        "xlm-roberta-large": "https://huggingface.co/xlm-roberta-large/resolve/main/sentencepiece.bpe.model",
        "xlm-roberta-large-finetuned-conll02-dutch": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/sentencepiece.bpe.model"
        ),
        "xlm-roberta-large-finetuned-conll02-spanish": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/sentencepiece.bpe.model"
        ),
        "xlm-roberta-large-finetuned-conll03-english": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/sentencepiece.bpe.model"
        ),
        "xlm-roberta-large-finetuned-conll03-german": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/sentencepiece.bpe.model"
        ),
    }
}

_a   :	Optional[Any] 							=   {
    "xlm-roberta-base": 512,
    "xlm-roberta-large": 512,
    "xlm-roberta-large-finetuned-conll02-dutch": 512,
    "xlm-roberta-large-finetuned-conll02-spanish": 512,
    "xlm-roberta-large-finetuned-conll03-english": 512,
    "xlm-roberta-large-finetuned-conll03-german": 512,
}






class 		__A       (__magic_name__   ):
       snake_case       :Union[str, Any]				    =					VOCAB_FILES_NAMES
       snake_case       :Any				    =					PRETRAINED_VOCAB_FILES_MAP
       snake_case       :Tuple				    =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       snake_case       :Optional[int]				    =					["input_ids", "attention_mask"]






       def __init__(      self				,     UpperCamelCase_				,     UpperCamelCase_="<s>"				,     UpperCamelCase_="</s>"				,     UpperCamelCase_="</s>"				,     UpperCamelCase_="<s>"				,     UpperCamelCase_="<unk>"				,     UpperCamelCase_="<pad>"				,     UpperCamelCase_="<mask>"				,     UpperCamelCase_ = None				,     **UpperCamelCase_				,     ):
              # Mask token behave like a normal word, i.e. include the space before it
              __UpperCAmelCase  :				Optional[int]       				=		AddedToken(UpperCamelCase_				,     lstrip=UpperCamelCase_				,     rstrip=UpperCamelCase_      ) if isinstance(UpperCamelCase_				,     UpperCamelCase_      ) else mask_token

              __UpperCAmelCase  :				int       				=		{} if sp_model_kwargs is None else sp_model_kwargs

              super().__init__(
                  bos_token=UpperCamelCase_				,     eos_token=UpperCamelCase_				,     unk_token=UpperCamelCase_				,     sep_token=UpperCamelCase_				,     cls_token=UpperCamelCase_				,     pad_token=UpperCamelCase_				,     mask_token=UpperCamelCase_				,     sp_model_kwargs=self.sp_model_kwargs				,     **UpperCamelCase_				,     )

              __UpperCAmelCase  :				Optional[int]       				=		spm.SentencePieceProcessor(**self.sp_model_kwargs      )
              self.sp_model.Load(str(UpperCamelCase_      )      )
              __UpperCAmelCase  :				Union[str, Any]       				=		vocab_file

              # Original fairseq vocab and spm vocab must be "aligned":
              # Vocab    |    0    |    1    |   2    |    3    |  4  |  5  |  6  |   7   |   8   |  9
              # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
              # fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's'   | '▁de' | '-'
              # spm      | '<unk>' | '<s>'   | '</s>' | ','     | '.' | '▁' | 's' | '▁de' | '-'   | '▁a'

              # Mimic fairseq token-to-id alignment for the first 4 token
              __UpperCAmelCase  :				Optional[Any]       				=		{"<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3}

              # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
              __UpperCAmelCase  :				List[Any]       				=		1

              __UpperCAmelCase  :				Optional[Any]       				=		len(self.sp_model      ) + self.fairseq_offset
              __UpperCAmelCase  :				str       				=		{v: k for k, v in self.fairseq_tokens_to_ids.items()}






       def __getstate__(      self      ):
              __UpperCAmelCase  :				List[str]       				=		self.__dict__.copy()
              __UpperCAmelCase  :				str       				=		None
              __UpperCAmelCase  :				str       				=		self.sp_model.serialized_model_proto()
              return state






       def __setstate__(      self				,     UpperCamelCase_      ):
              __UpperCAmelCase  :				Union[str, Any]       				=		d

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

              __UpperCAmelCase  :				Dict       				=		spm.SentencePieceProcessor(**self.sp_model_kwargs      )
              self.sp_model.LoadFromSerializedProto(self.sp_model_proto      )






       def 			_snake_case						(      self				,     UpperCamelCase_				,     UpperCamelCase_ = None      ):

              if token_ids_a is None:
                     return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
              __UpperCAmelCase  :				List[Any]       				=		[self.cls_token_id]
              __UpperCAmelCase  :				Union[str, Any]       				=		[self.sep_token_id]
              return cls + token_ids_a + sep + sep + token_ids_a + sep






       def 			_snake_case						(      self				,     UpperCamelCase_				,     UpperCamelCase_ = None				,     UpperCamelCase_ = False      ):

              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 None:
                     return [1] + ([0] * len(UpperCamelCase_      )) + [1]
              return [1] + ([0] * len(UpperCamelCase_      )) + [1, 1] + ([0] * len(UpperCamelCase_      )) + [1]






       def 			_snake_case						(      self				,     UpperCamelCase_				,     UpperCamelCase_ = None      ):
              __UpperCAmelCase  :				Dict       				=		[self.sep_token_id]
              __UpperCAmelCase  :				List[Any]       				=		[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 + sep + token_ids_a + sep      ) * [0]






       @property
       def 			_snake_case						(      self      ):
              return len(self.sp_model      ) + self.fairseq_offset + 1  # Add the <mask> token






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				Union[str, Any]       				=		{self.convert_ids_to_tokens(UpperCamelCase_      ): i for i in range(self.vocab_size      )}
              vocab.update(self.added_tokens_encoder      )
              return vocab






       def 			_snake_case						(      self				,     UpperCamelCase_      ):
              return self.sp_model.encode(UpperCamelCase_				,     out_type=UpperCamelCase_      )






       def 			_snake_case						(      self				,     UpperCamelCase_      ):
              if token in self.fairseq_tokens_to_ids:
                     return self.fairseq_tokens_to_ids[token]
              __UpperCAmelCase  :				Optional[int]       				=		self.sp_model.PieceToId(UpperCamelCase_      )

              # Need to return unknown token if the SP model returned 0
              return spm_id + self.fairseq_offset if spm_id else self.unk_token_id






       def 			_snake_case						(      self				,     UpperCamelCase_      ):
              if index in self.fairseq_ids_to_tokens:
                     return self.fairseq_ids_to_tokens[index]
              return self.sp_model.IdToPiece(index - self.fairseq_offset      )






       def 			_snake_case						(      self				,     UpperCamelCase_      ):
              __UpperCAmelCase  :				Tuple       				=		"".join(UpperCamelCase_      ).replace(UpperCamelCase_				,     " "      ).strip()
              return out_string







       def 			_snake_case						(      self				,     UpperCamelCase_				,     UpperCamelCase_ = None      ):
              if not os.path.isdir(UpperCamelCase_      ):
                     logger.error(f"""Vocabulary path ({save_directory}) should be a directory"""      )
                     return
              __UpperCAmelCase  :				List[str]       				=		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:
                            __UpperCAmelCase  :				Optional[int]       				=		self.sp_model.serialized_model_proto()
                            fi.write(UpperCamelCase_      )

              return (out_vocab_file,)






