




import collections
import os
from typing import List, Optional, Tuple

from transformers.utils import is_jieba_available, requires_backends


if is_jieba_available():
  import jieba

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


_lowerCamelCase	:		Optional[Any]							       =   logging.get_logger(__name__)

_lowerCamelCase	:		List[Any]							       =   {"""vocab_file""": """vocab.txt"""}

_lowerCamelCase	:		List[Any]							       =   {
    """vocab_file""": {
        """openbmb/cpm-ant-10b""": """https://huggingface.co/openbmb/cpm-ant-10b/blob/main/vocab.txt""",
    },
}

_lowerCamelCase	:		List[str]							       =   {
    """openbmb/cpm-ant-10b""": 1024,
}







def SCREAMING_SNAKE_CASE				(       lowercase_						)   ->       Any:



       """simple docstring"""

       A__       =    collections.OrderedDict()
       with open(lowercase_ ,				'''r''' ,				encoding='''utf-8'''						) as reader:
              A__       =    reader.readlines()
       for index, token in enumerate(lowercase_						):
              A__       =    token.rstrip('''\n'''						)
              A__       =    index
       return vocab


class 				UpperCamelCase_    ( UpperCAmelCase__     ):

    '''simple docstring'''






    def __init__(			self      :	Dict					, UpperCAmelCase__      :	Union[str, Any]					, UpperCAmelCase__      :	List[str]="<unk>"					, UpperCAmelCase__      :	Union[str, Any]=200)						->List[str]:





           '''simple docstring'''

           A__       =    vocab
           A__       =    unk_token
           A__       =    max_input_chars_per_word





    def        SCREAMING_SNAKE_CASE							(			self      :	Optional[Any]					, UpperCAmelCase__      :	Optional[int])						->Optional[int]:





           '''simple docstring'''

           A__       =    list(UpperCAmelCase__)
           if len(UpperCAmelCase__) > self.max_input_chars_per_word:
                  return [self.unk_token]

           A__       =    0
           A__       =    []
           while start < len(UpperCAmelCase__):
                  A__       =    len(UpperCAmelCase__)
                  A__       =    None
                  while start < end:
                         A__       =    ''''''.join(chars[start:end])
                         if substr in self.vocab:
                                A__       =    substr
                                break
                         end -= 1
                  if cur_substr is None:
                         sub_tokens.append(self.unk_token)
                         start += 1
                  else:
                         sub_tokens.append(UpperCAmelCase__)
                         A__       =    end

           return sub_tokens


class 				UpperCamelCase_    ( UpperCAmelCase__     ):

    '''simple docstring'''






    UpperCAmelCase__							  =      VOCAB_FILES_NAMES
    UpperCAmelCase__							  =      PRETRAINED_VOCAB_FILES_MAP
    UpperCAmelCase__							  =      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    UpperCAmelCase__							  =      ['''input_ids''', '''attention_mask''']
    UpperCAmelCase__							  =      False


    def __init__(			self      :	Union[str, Any]					, UpperCAmelCase__      :	Tuple					, UpperCAmelCase__      :	Optional[int]="<d>"					, UpperCAmelCase__      :	Union[str, Any]="</d>"					, UpperCAmelCase__      :	Dict="<s>"					, UpperCAmelCase__      :	Optional[int]="</s>"					, UpperCAmelCase__      :	Optional[int]="<pad>"					, UpperCAmelCase__      :	Dict="<unk>"					, UpperCAmelCase__      :	Optional[Any]="</n>"					, UpperCAmelCase__      :	List[Any]="</_>"					, UpperCAmelCase__      :	Optional[Any]="left"					, **UpperCAmelCase__      :	Union[str, Any]					, )						->Dict:





           '''simple docstring'''

           requires_backends(self					, ['''jieba'''])
           super().__init__(
               bod_token=UpperCAmelCase__					, eod_token=UpperCAmelCase__					, bos_token=UpperCAmelCase__					, eos_token=UpperCAmelCase__					, pad_token=UpperCAmelCase__					, unk_token=UpperCAmelCase__					, line_token=UpperCAmelCase__					, space_token=UpperCAmelCase__					, padding_side=UpperCAmelCase__					, **UpperCAmelCase__					, )
           A__       =    bod_token
           A__       =    eod_token
           A__       =    load_vocab(UpperCAmelCase__)
           A__       =    self.encoder[space_token]
           A__       =    self.encoder[line_token]

           del self.encoder[space_token]
           del self.encoder[line_token]

           A__       =    collections.OrderedDict(sorted(self.encoder.items()					, key=lambda UpperCAmelCase__: x[1]))
           A__       =    {v: k for k, v in self.encoder.items()}

           A__       =    WordpieceTokenizer(vocab=self.encoder					, unk_token=self.unk_token)


    @property
    def        SCREAMING_SNAKE_CASE							(			self      :	List[str])						->Dict:





           '''simple docstring'''

           return self.encoder[self.bod_token]


    @property
    def        SCREAMING_SNAKE_CASE							(			self      :	Optional[Any])						->List[Any]:





           '''simple docstring'''

           return self.encoder[self.eod_token]


    @property
    def        SCREAMING_SNAKE_CASE							(			self      :	int)						->List[Any]:





           '''simple docstring'''

           return self.encoder["\n"]


    @property
    def        SCREAMING_SNAKE_CASE							(			self      :	int)						->int:





           '''simple docstring'''

           return len(self.encoder)


    def        SCREAMING_SNAKE_CASE							(			self      :	Dict)						->str:





           '''simple docstring'''

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


    def        SCREAMING_SNAKE_CASE							(			self      :	Any					, UpperCAmelCase__      :	Any)						->Union[str, Any]:





           '''simple docstring'''

           A__       =    []
           for x in jieba.cut(UpperCAmelCase__					, cut_all=UpperCAmelCase__):
                  output_tokens.extend(self.wordpiece_tokenizer.tokenize(UpperCAmelCase__))
           return output_tokens


    def        SCREAMING_SNAKE_CASE							(			self      :	Optional[Any]					, UpperCAmelCase__      :	Optional[Any]					, **UpperCAmelCase__      :	Dict)						->List[str]:





           '''simple docstring'''

           A__       =    [i for i in token_ids if i >= 0]
           A__       =    [
               x for x in token_ids if x != self.pad_token_id and x != self.eos_token_id and x != self.bos_token_id
           ]
           return super()._decode(UpperCAmelCase__					, **UpperCAmelCase__)


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





           '''simple docstring'''

           return token in self.encoder


    def        SCREAMING_SNAKE_CASE							(			self      :	Any					, UpperCAmelCase__      :	List[str])						->str:





           '''simple docstring'''

           return "".join(UpperCAmelCase__)


    def        SCREAMING_SNAKE_CASE							(			self      :	int					, UpperCAmelCase__      :	int)						->Union[str, Any]:





           '''simple docstring'''

           return self.encoder.get(UpperCAmelCase__					, self.encoder.get(self.unk_token))


    def        SCREAMING_SNAKE_CASE							(			self      :	int					, UpperCAmelCase__      :	List[str])						->Any:





           '''simple docstring'''

           return self.decoder.get(UpperCAmelCase__					, self.unk_token)


    def        SCREAMING_SNAKE_CASE							(			self      :	List[str]					, UpperCAmelCase__      :	str					, UpperCAmelCase__      :	Optional[str] = None)						->Tuple[str]:





           '''simple docstring'''

           if os.path.isdir(UpperCAmelCase__):
                  A__       =    os.path.join(
                      UpperCAmelCase__					, (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''])
           else:
                  A__       =    (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory
           A__       =    0
           if " " in self.encoder:
                  A__       =    self.encoder[''' ''']
                  del self.encoder[" "]
           if "\n" in self.encoder:
                  A__       =    self.encoder['''\n''']
                  del self.encoder["\n"]
           A__       =    collections.OrderedDict(sorted(self.encoder.items()					, key=lambda UpperCAmelCase__: x[1]))
           with open(UpperCAmelCase__					, '''w'''					, encoding='''utf-8''') as writer:
                  for token, token_index in self.encoder.items():
                         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!''')
                                A__       =    token_index
                         writer.write(token + '''\n''')
                         index += 1
           return (vocab_file,)


    def        SCREAMING_SNAKE_CASE							(			self      :	int					, UpperCAmelCase__      :	List[int]					, UpperCAmelCase__      :	List[int] = None)						->List[int]:





           '''simple docstring'''

           if token_ids_a is None:
                  return [self.bos_token_id] + token_ids_a
           return [self.bos_token_id] + token_ids_a + [self.bos_token_id] + token_ids_a





    def        SCREAMING_SNAKE_CASE							(			self      :	Optional[int]					, UpperCAmelCase__      :	List[int]					, UpperCAmelCase__      :	Optional[List[int]] = None					, UpperCAmelCase__      :	bool = False)						->List[int]:





           '''simple docstring'''

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








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



       """simple docstring"""

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

       A__       =    [0] * (upper_limit + 1)

       # Base case: C(0) = C(1) = 1
       A__       =    1
       if upper_limit > 0:
              A__       =    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:
      _lowerCamelCase	:		List[Any]							       =   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()


