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


_UpperCAmelCase     =			logging.get_logger(__name__)

_UpperCAmelCase     =			"""▁"""

_UpperCAmelCase     =			{"""vocab_file""": """sentencepiece.bpe.model"""}

_UpperCAmelCase     =			{
    """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"""
        ),
    }
}

_UpperCAmelCase     =			{
    """xlm-roberta-base""": 5_1_2,
    """xlm-roberta-large""": 5_1_2,
    """xlm-roberta-large-finetuned-conll02-dutch""": 5_1_2,
    """xlm-roberta-large-finetuned-conll02-spanish""": 5_1_2,
    """xlm-roberta-large-finetuned-conll03-english""": 5_1_2,
    """xlm-roberta-large-finetuned-conll03-german""": 5_1_2,
}
class 					a	(  UpperCAmelCase__       ):
  UpperCamelCase	:				Optional[Any]     						=					VOCAB_FILES_NAMES
  UpperCamelCase	:				List[str]     						=					PRETRAINED_VOCAB_FILES_MAP
  UpperCamelCase	:				Optional[int]     						=					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  UpperCamelCase	:				List[str]     						=					['input_ids', 'attention_mask']



  def __init__(						self :					List[Any]       ,		lowerCAmelCase :					List[Any]       ,		lowerCAmelCase :					Optional[Any]="<s>"       ,		lowerCAmelCase :					List[Any]="</s>"       ,		lowerCAmelCase :					int="</s>"       ,		lowerCAmelCase :					Optional[Any]="<s>"       ,		lowerCAmelCase :					Union[str, Any]="<unk>"       ,		lowerCAmelCase :					str="<pad>"       ,		lowerCAmelCase :					Optional[int]="<mask>"       ,		lowerCAmelCase :					Optional[Dict[str, Any]] = None       ,		**lowerCAmelCase :					List[str]       ,		)     ->					None:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[int]     			=AddedToken(lowerCAmelCase       ,		lstrip=lowerCAmelCase       ,		rstrip=lowerCAmelCase		) if isinstance(lowerCAmelCase       ,		lowerCAmelCase		) else mask_token

     SCREAMING_SNAKE_CASE_: List[Any]     			={} if sp_model_kwargs is None else sp_model_kwargs

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

     SCREAMING_SNAKE_CASE_: Dict     			=spm.SentencePieceProcessor(**self.sp_model_kwargs		)
     self.sp_model.Load(str(lowerCAmelCase		)		)
     SCREAMING_SNAKE_CASE_: List[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
     SCREAMING_SNAKE_CASE_: Optional[int]     			={"""<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
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=1

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



  def __getstate__(						self :					Union[str, Any]		)     ->					Tuple:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Dict     			=self.__dict__.copy()
     SCREAMING_SNAKE_CASE_: Optional[int]     			=None
     SCREAMING_SNAKE_CASE_: Dict     			=self.sp_model.serialized_model_proto()
     return state



  def __setstate__(						self :					Optional[int]       ,		lowerCAmelCase :					Optional[Any]		)     ->					List[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[int]     			=d

     # for backward compatibility
     if not hasattr(self       ,		"""sp_model_kwargs"""		):
        SCREAMING_SNAKE_CASE_: List[str]     			={}

     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=spm.SentencePieceProcessor(**self.sp_model_kwargs		)
     self.sp_model.LoadFromSerializedProto(self.sp_model_proto		)



  def 	lowerCamelCase__       (						self :					Any       ,		lowerCAmelCase :					List[int]       ,		lowerCAmelCase :					Optional[List[int]] = None		)     ->					List[int]:


     '''simple docstring'''




     if token_ids_a is None:
        return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
     SCREAMING_SNAKE_CASE_: List[str]     			=[self.cls_token_id]
     SCREAMING_SNAKE_CASE_: Dict     			=[self.sep_token_id]
     return cls + token_ids_a + sep + sep + token_ids_a + sep



  def 	lowerCamelCase__       (						self :					Dict       ,		lowerCAmelCase :					List[int]       ,		lowerCAmelCase :					Optional[List[int]] = None       ,		lowerCAmelCase :					bool = False		)     ->					List[int]:


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



  def 	lowerCamelCase__       (						self :					List[Any]       ,		lowerCAmelCase :					List[int]       ,		lowerCAmelCase :					Optional[List[int]] = None		)     ->					List[int]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Dict     			=[self.sep_token_id]
     SCREAMING_SNAKE_CASE_: 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 	lowerCamelCase__       (						self :					str		)     ->					Any:


     '''simple docstring'''




     return len(self.sp_model		) + self.fairseq_offset + 1  # Add the <mask> token



  def 	lowerCamelCase__       (						self :					Optional[int]		)     ->					Dict:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[Any]     			={self.convert_ids_to_tokens(lowerCAmelCase		): i for i in range(self.vocab_size		)}
     vocab.update(self.added_tokens_encoder		)
     return vocab



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


     '''simple docstring'''




     return self.sp_model.encode(lowerCAmelCase       ,		out_type=lowerCAmelCase		)



  def 	lowerCamelCase__       (						self :					Optional[int]       ,		lowerCAmelCase :					Tuple		)     ->					Optional[Any]:


     '''simple docstring'''




     if token in self.fairseq_tokens_to_ids:
        return self.fairseq_tokens_to_ids[token]
     SCREAMING_SNAKE_CASE_: Dict     			=self.sp_model.PieceToId(lowerCAmelCase		)

     # 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 	lowerCamelCase__       (						self :					List[str]       ,		lowerCAmelCase :					Any		)     ->					Optional[int]:


     '''simple docstring'''




     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 	lowerCamelCase__       (						self :					Dict       ,		lowerCAmelCase :					int		)     ->					List[str]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Dict     			="""""".join(lowerCAmelCase		).replace(lowerCAmelCase       ,		""" """		).strip()
     return out_string





  def 	lowerCamelCase__       (						self :					Tuple       ,		lowerCAmelCase :					str       ,		lowerCAmelCase :					Optional[str] = None		)     ->					Tuple[str]:


     '''simple docstring'''




     if not os.path.isdir(lowerCAmelCase		):
        logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''		)
        return
     SCREAMING_SNAKE_CASE_: Any     			=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:
           SCREAMING_SNAKE_CASE_: Union[str, Any]     			=self.sp_model.serialized_model_proto()
           fi.write(lowerCAmelCase		)

     return (out_vocab_file,)








"""simple docstring"""
def      __magic_name__				(							lowercase ):
   if upper_limit < 0:
      raise ValueError("""Limit for the Catalan sequence must be ≥ 0""" )

   SCREAMING_SNAKE_CASE_: Tuple     			=[0] * (upper_limit + 1)

   # Base case: C(0) = C(1) = 1
   SCREAMING_SNAKE_CASE_: Any     			=1
   if upper_limit > 0:
      SCREAMING_SNAKE_CASE_: List[str]     			=1

   # Recurrence relation: C(i) = sum(C(j).C(i-j-1)), from j = 0 to i
   for i in range(2							,			upper_limit + 1 ):
      for j in range(lowercase ):
         catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1]

   return catalan_list


if __name__ == "__main__":
   print("""\n********* Catalan Numbers Using Dynamic Programming ************\n""")
   print("""\n*** Enter -1 at any time to quit ***""")
   print("""\nEnter the upper limit (≥ 0) for the Catalan number sequence: """, end="""""")
   try:
      while True:
         _UpperCAmelCase     =			int(input().strip())
         if N < 0:
            print("""\n********* Goodbye!! ************""")
            break
         else:
            print(f"""The Catalan numbers from 0 through {N} are:""")
            print(catalan_numbers(N))
            print("""Try another upper limit for the sequence: """, end="""""")
   except (NameError, ValueError):
      print("""\n********* Invalid input, goodbye! ************\n""")

   import doctest

   doctest.testmod()



