

import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, BatchEncoding, PreTrainedTokenizer
from ...utils import logging


__UpperCAmelCase    				=     logging.get_logger(__name__)

__UpperCAmelCase    				=     '▁'

__UpperCAmelCase    				=     {'vocab_file': 'sentencepiece.bpe.model'}

__UpperCAmelCase    				=     {
    'vocab_file': {
        'facebook/mbart-large-en-ro': (
            'https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model'
        ),
        'facebook/mbart-large-cc25': (
            'https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model'
        ),
    }
}

__UpperCAmelCase    				=     {
    'facebook/mbart-large-en-ro': 1_0_2_4,
    'facebook/mbart-large-cc25': 1_0_2_4,
}

# fmt: off
__UpperCAmelCase    				=     ['ar_AR', 'cs_CZ', 'de_DE', 'en_XX', 'es_XX', 'et_EE', 'fi_FI', 'fr_XX', 'gu_IN', 'hi_IN', 'it_IT', 'ja_XX', 'kk_KZ', 'ko_KR', 'lt_LT', 'lv_LV', 'my_MM', 'ne_NP', 'nl_XX', 'ro_RO', 'ru_RU', 'si_LK', 'tr_TR', 'vi_VN', 'zh_CN']





class A__    ( A ):



 """simple docstring"""







 _lowercase     :					Any      =						VOCAB_FILES_NAMES
 _lowercase     :					int      =						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 _lowercase     :					str      =						PRETRAINED_VOCAB_FILES_MAP
 _lowercase     :					int      =						['''input_ids''', '''attention_mask''']

 _lowercase     :					List[int]      =						[]
 _lowercase     :					List[int]      =						[]

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






      '''simple docstring'''




      _lowerCAmelCase   :					int        =    AddedToken(A_     ,						lstrip=A_     ,						rstrip=A_       ) if isinstance(A_     ,						A_       ) else mask_token

      _lowerCAmelCase   :					Optional[Any]        =    {} if sp_model_kwargs is None else sp_model_kwargs

      super().__init__(
          bos_token=A_     ,						eos_token=A_     ,						unk_token=A_     ,						sep_token=A_     ,						cls_token=A_     ,						pad_token=A_     ,						mask_token=A_     ,						tokenizer_file=A_     ,						src_lang=A_     ,						tgt_lang=A_     ,						additional_special_tokens=A_     ,						sp_model_kwargs=self.sp_model_kwargs     ,						**A_     ,						)

      _lowerCAmelCase   :					Optional[Any]        =    spm.SentencePieceProcessor(**self.sp_model_kwargs       )
      self.sp_model.Load(str(A_       )       )
      _lowerCAmelCase   :					Dict        =    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
      _lowerCAmelCase   :					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
      _lowerCAmelCase   :					str        =    1

      _lowerCAmelCase   :					Any        =    len(self.sp_model       )
      _lowerCAmelCase   :					Union[str, Any]        =    {
          code: self.sp_model_size + i + self.fairseq_offset for i, code in enumerate(A_       )
      }
      _lowerCAmelCase   :					Tuple        =    {v: k for k, v in self.lang_code_to_id.items()}
      _lowerCAmelCase   :					List[str]        =    len(self.sp_model       ) + len(self.lang_code_to_id       ) + self.fairseq_offset

      self.fairseq_tokens_to_ids.update(self.lang_code_to_id       )
      _lowerCAmelCase   :					int        =    {v: k for k, v in self.fairseq_tokens_to_ids.items()}
      _lowerCAmelCase   :					Optional[int]        =    list(self.lang_code_to_id.keys()       )

      if additional_special_tokens is not None:
           # Only add those special tokens if they are not already there.
           self._additional_special_tokens.extend(
               [t for t in additional_special_tokens if t not in self._additional_special_tokens]       )

      _lowerCAmelCase   :					Optional[int]        =    src_lang if src_lang is not None else "en_XX"
      _lowerCAmelCase   :					Union[str, Any]        =    self.lang_code_to_id[self._src_lang]
      _lowerCAmelCase   :					List[str]        =    tgt_lang
      self.set_src_lang_special_tokens(self._src_lang       )

 def __getstate__(		self :	List[Any]       ):






      '''simple docstring'''




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

 def __setstate__(		self :	Any     ,						A_ :	Dict       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[int]        =    d

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

      _lowerCAmelCase   :					Any        =    spm.SentencePieceProcessor(**self.sp_model_kwargs       )
      self.sp_model.LoadFromSerializedProto(self.sp_model_proto       )

 @property
 def    __magic_name__       (		self :	Optional[int]       ):






      '''simple docstring'''




      return len(self.sp_model       ) + len(self.lang_code_to_id       ) + self.fairseq_offset + 1  # Plus 1 for the mask token

 @property
 def    __magic_name__       (		self :	int       ):






      '''simple docstring'''




      return self._src_lang

 @src_lang.setter
 def    __magic_name__       (		self :	Any     ,						A_ :	str       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Dict        =    new_src_lang
      self.set_src_lang_special_tokens(self._src_lang       )

 def    __magic_name__       (		self :	Union[str, Any]     ,						A_ :	List[int]     ,						A_ :	Optional[List[int]] = None     ,						A_ :	bool = False       ):






      '''simple docstring'''




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

      _lowerCAmelCase   :					Optional[Any]        =    [1] * len(self.prefix_tokens       )
      _lowerCAmelCase   :					Union[str, Any]        =    [1] * len(self.suffix_tokens       )
      if token_ids_a is None:
           return prefix_ones + ([0] * len(A_       )) + suffix_ones
      return prefix_ones + ([0] * len(A_       )) + ([0] * len(A_       )) + suffix_ones

 def    __magic_name__       (		self :	List[str]     ,						A_ :	List[int]     ,						A_ :	Optional[List[int]] = None       ):






      '''simple docstring'''




      if token_ids_a is None:
           return self.prefix_tokens + token_ids_a + self.suffix_tokens
      # We don't expect to process pairs, but leave the pair logic for API consistency
      return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens

 def    __magic_name__       (		self :	Union[str, Any]     ,						A_ :	List[int]     ,						A_ :	Optional[List[int]] = None       ):






      '''simple docstring'''




      _lowerCAmelCase   :					List[str]        =    [self.sep_token_id]
      _lowerCAmelCase   :					Optional[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]

 def    __magic_name__       (		self :	Optional[int]     ,						A_ :	Tuple     ,						A_ :	str     ,						A_ :	Optional[str]     ,						A_ :	Optional[str]     ,						**A_ :	str       ):






      '''simple docstring'''




      if src_lang is None or tgt_lang is None:
           raise ValueError("Translation requires a `src_lang` and a `tgt_lang` for this model"       )
      _lowerCAmelCase   :					str        =    src_lang
      _lowerCAmelCase   :					List[str]        =    self(A_     ,						add_special_tokens=A_     ,						return_tensors=A_     ,						**A_       )
      _lowerCAmelCase   :					Any        =    self.convert_tokens_to_ids(A_       )
      _lowerCAmelCase   :					Optional[int]        =    tgt_lang_id
      return inputs

 def    __magic_name__       (		self :	Optional[int]       ):






      '''simple docstring'''




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

 def    __magic_name__       (		self :	Union[str, Any]     ,						A_ :	str       ):






      '''simple docstring'''




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

 def    __magic_name__       (		self :	str     ,						A_ :	int       ):






      '''simple docstring'''




      if token in self.fairseq_tokens_to_ids:
           return self.fairseq_tokens_to_ids[token]
      _lowerCAmelCase   :					str        =    self.sp_model.PieceToId(A_       )

      # 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    __magic_name__       (		self :	List[Any]     ,						A_ :	Optional[Any]       ):






      '''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    __magic_name__       (		self :	int     ,						A_ :	Optional[int]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[int]        =    "".join(A_       ).replace(A_     ,						" "       ).strip()
      return out_string

 def    __magic_name__       (		self :	Optional[int]     ,						A_ :	str     ,						A_ :	Optional[str] = None       ):






      '''simple docstring'''




      if not os.path.isdir(A_       ):
           logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''       )
           return
      _lowerCAmelCase   :					List[Any]        =    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:
                _lowerCAmelCase   :					Any        =    self.sp_model.serialized_model_proto()
                fi.write(A_       )

      return (out_vocab_file,)

 def    __magic_name__       (		self :	List[Any]     ,						A_ :	List[str]     ,						A_ :	str = "en_XX"     ,						A_ :	Optional[List[str]] = None     ,						A_ :	str = "ro_RO"     ,						**A_ :	Union[str, Any]     ,						):






      '''simple docstring'''




      _lowerCAmelCase   :					List[Any]        =    src_lang
      _lowerCAmelCase   :					List[Any]        =    tgt_lang
      return super().prepare_seqaseq_batch(A_     ,						A_     ,						**A_       )

 def    __magic_name__       (		self :	Tuple       ):






      '''simple docstring'''




      return self.set_src_lang_special_tokens(self.src_lang       )

 def    __magic_name__       (		self :	Tuple       ):






      '''simple docstring'''




      return self.set_tgt_lang_special_tokens(self.tgt_lang       )

 def    __magic_name__       (		self :	List[Any]     ,						A_ :	Dict       ):






      '''simple docstring'''




      _lowerCAmelCase   :					List[str]        =    self.lang_code_to_id[src_lang]
      _lowerCAmelCase   :					Dict        =    []
      _lowerCAmelCase   :					List[str]        =    [self.eos_token_id, self.cur_lang_code]


 def    __magic_name__       (		self :	List[Any]     ,						A_ :	str       ):






      '''simple docstring'''




      _lowerCAmelCase   :					List[Any]        =    self.lang_code_to_id[lang]
      _lowerCAmelCase   :					Any        =    []
      _lowerCAmelCase   :					Any        =    [self.eos_token_id, self.cur_lang_code]









def 	_snake_case		(    SCREAMING_SNAKE_CASE				)       ->      "list[int]":

     """simple docstring"""







     if upper_limit < 0:
          raise ValueError("Limit for the Catalan sequence must be ≥ 0"				)

     _lowerCAmelCase   :					Optional[Any]        =    [0] * (upper_limit + 1)

     # Base case: C(0) = C(1) = 1
     _lowerCAmelCase   :					Optional[int]        =    1
     if upper_limit > 0:
          _lowerCAmelCase   :					Any        =    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(SCREAMING_SNAKE_CASE				):
               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()






