import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_funnel import FunnelTokenizer


SCREAMING_SNAKE_CASE		:     Union[str, Any]         =	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE		:     Dict         =	{"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

SCREAMING_SNAKE_CASE		:     Dict         =	[
    "small",
    "small-base",
    "medium",
    "medium-base",
    "intermediate",
    "intermediate-base",
    "large",
    "large-base",
    "xlarge",
    "xlarge-base",
]

SCREAMING_SNAKE_CASE		:     int         =	{
    "vocab_file": {
        "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt",
        "funnel-transformer/small-base": "https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt",
        "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt",
        "funnel-transformer/medium-base": (
            "https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt"
        ),
        "funnel-transformer/intermediate": (
            "https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt"
        ),
        "funnel-transformer/intermediate-base": (
            "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt"
        ),
        "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt",
        "funnel-transformer/large-base": "https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt",
        "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt",
        "funnel-transformer/xlarge-base": (
            "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt"
        ),
    },
    "tokenizer_file": {
        "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json",
        "funnel-transformer/small-base": (
            "https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json",
        "funnel-transformer/medium-base": (
            "https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/intermediate": (
            "https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/intermediate-base": (
            "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json",
        "funnel-transformer/large-base": (
            "https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json",
        "funnel-transformer/xlarge-base": (
            "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json"
        ),
    },
}
SCREAMING_SNAKE_CASE		:     Any         =	{F'''funnel-transformer/{name}''': 512 for name in _model_names}
SCREAMING_SNAKE_CASE		:     Tuple         =	{F'''funnel-transformer/{name}''': {"do_lower_case": True} for name in _model_names}

class        UpperCamelCase						(				__a				):
						a__		:Dict      =			VOCAB_FILES_NAMES
						a__		:Dict      =			PRETRAINED_VOCAB_FILES_MAP
						a__		:Dict      =			PRETRAINED_INIT_CONFIGURATION
						a__		:str      =			FunnelTokenizer
						a__		:List[str]      =			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
						a__		:int      =			2





						def __init__(self			,     __UpperCamelCase=None			,     __UpperCamelCase=None			,     __UpperCamelCase=True			,     __UpperCamelCase="<unk>"			,     __UpperCamelCase="<sep>"			,     __UpperCamelCase="<pad>"			,     __UpperCamelCase="<cls>"			,     __UpperCamelCase="<mask>"			,     __UpperCamelCase="<s>"			,     __UpperCamelCase="</s>"			,     __UpperCamelCase=True			,     __UpperCamelCase=True			,     __UpperCamelCase=None			,     __UpperCamelCase="##"			,     **__UpperCamelCase			,     )				->				List[Any]:
												super().__init__(
												    __UpperCamelCase			,     tokenizer_file=__UpperCamelCase			,     do_lower_case=__UpperCamelCase			,     unk_token=__UpperCamelCase			,     sep_token=__UpperCamelCase			,     pad_token=__UpperCamelCase			,     cls_token=__UpperCamelCase			,     mask_token=__UpperCamelCase			,     bos_token=__UpperCamelCase			,     eos_token=__UpperCamelCase			,     clean_text=__UpperCamelCase			,     tokenize_chinese_chars=__UpperCamelCase			,     strip_accents=__UpperCamelCase			,     wordpieces_prefix=__UpperCamelCase			,     **__UpperCamelCase			,     )

												UpperCamelCase_		:							List[str]  =		json.loads(self.backend_tokenizer.normalizer.__getstate__()						)
												if (
												    normalizer_state.get("""lowercase"""			,     __UpperCamelCase						) != do_lower_case
												    or normalizer_state.get("""strip_accents"""			,     __UpperCamelCase						) != strip_accents
												    or normalizer_state.get("""handle_chinese_chars"""			,     __UpperCamelCase						) != tokenize_chinese_chars
												):
																		UpperCamelCase_		:							List[Any]  =		getattr(__UpperCamelCase			,     normalizer_state.pop("""type"""						)						)
																		UpperCamelCase_		:							Union[str, Any]  =		do_lower_case
																		UpperCamelCase_		:							Tuple  =		strip_accents
																		UpperCamelCase_		:							Dict  =		tokenize_chinese_chars
																		UpperCamelCase_		:							Union[str, Any]  =		normalizer_class(**__UpperCamelCase						)

												UpperCamelCase_		:							Dict  =		do_lower_case





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase=None						)				->				Dict:
												UpperCamelCase_		:							Any  =		[self.cls_token_id] + token_ids_a + [self.sep_token_id]

												if token_ids_a:
																		output += token_ids_a + [self.sep_token_id]

												return output





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase = None						)				->				List[int]:
												UpperCamelCase_		:							Optional[Any]  =		[self.sep_token_id]
												UpperCamelCase_		:							Any  =		[self.cls_token_id]
												if token_ids_a is None:
																		return len(cls						) * [self.cls_token_type_id] + len(token_ids_a + sep						) * [0]
												return len(cls						) * [self.cls_token_type_id] + len(token_ids_a + sep						) * [0] + len(token_ids_a + sep						) * [1]





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase = None						)				->				Tuple[str]:
												UpperCamelCase_		:							int  =		self._tokenizer.model.save(__UpperCamelCase			,     name=__UpperCamelCase						)
												return tuple(__UpperCamelCase						)







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

import sentencepiece as spm

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


SCREAMING_SNAKE_CASE		:     Union[str, Any]         =	logging.get_logger(__name__)


SCREAMING_SNAKE_CASE		:     Union[str, Any]         =	"▁"

SCREAMING_SNAKE_CASE		:     Dict         =	{"vocab_file": "spiece.model"}

SCREAMING_SNAKE_CASE		:     List[Any]         =	{
    "vocab_file": {
        "google/reformer-crime-and-punishment": (
            "https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model"
        )
    }
}

SCREAMING_SNAKE_CASE		:     Any         =	{
    "google/reformer-crime-and-punishment": 524288,
}

class        UpperCamelCase						(				__a				):
						a__		:Optional[Any]      =			VOCAB_FILES_NAMES
						a__		:List[Any]      =			PRETRAINED_VOCAB_FILES_MAP
						a__		:Union[str, Any]      =			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
						a__		:List[Any]      =			['''input_ids''', '''attention_mask''']





						def __init__(self			,     __UpperCamelCase			,     __UpperCamelCase="</s>"			,     __UpperCamelCase="<unk>"			,     __UpperCamelCase=[]			,     __UpperCamelCase = None			,     **__UpperCamelCase			,     )				->				None:
												UpperCamelCase_		:							List[str]  =		{} if sp_model_kwargs is None else sp_model_kwargs

												super().__init__(
												    eos_token=__UpperCamelCase			,     unk_token=__UpperCamelCase			,     additional_special_tokens=__UpperCamelCase			,     sp_model_kwargs=self.sp_model_kwargs			,     **__UpperCamelCase			,     )

												UpperCamelCase_		:							int  =		vocab_file
												UpperCamelCase_		:							str  =		spm.SentencePieceProcessor(**self.sp_model_kwargs						)
												self.sp_model.Load(__UpperCamelCase						)





						@property
						def        A_							(self						)				->				Dict:
												return self.sp_model.get_piece_size()





						def        A_							(self						)				->				Dict[str, int]:
												UpperCamelCase_		:							str  =		{self.convert_ids_to_tokens(__UpperCamelCase						): i for i in range(self.vocab_size						)}
												vocab.update(self.added_tokens_encoder						)
												return vocab





						def __getstate__(self						)				->				Any:
												UpperCamelCase_		:							Dict  =		self.__dict__.copy()
												UpperCamelCase_		:							List[str]  =		None
												return state





						def __setstate__(self			,     __UpperCamelCase						)				->				List[Any]:
												UpperCamelCase_		:							List[Any]  =		d

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

												UpperCamelCase_		:							Dict  =		spm.SentencePieceProcessor(**self.sp_model_kwargs						)
												self.sp_model.Load(self.vocab_file						)





						def        A_							(self			,     __UpperCamelCase						)				->				List[str]:
												return self.sp_model.encode(__UpperCamelCase			,     out_type=__UpperCamelCase						)





						def        A_							(self			,     __UpperCamelCase						)				->				Union[str, Any]:
												return self.sp_model.piece_to_id(__UpperCamelCase						)





						def        A_							(self			,     __UpperCamelCase						)				->				Dict:
												if index < self.sp_model.get_piece_size():
																		UpperCamelCase_		:							Union[str, Any]  =		self.sp_model.IdToPiece(__UpperCamelCase						)
												return token





						def        A_							(self			,     __UpperCamelCase						)				->				int:
												UpperCamelCase_		:							Dict  =		[]
												UpperCamelCase_		:							Union[str, Any]  =		""""""
												for token in tokens:
																		# make sure that special tokens are not decoded using sentencepiece model
																		if token in self.all_special_tokens:
																								out_string += self.sp_model.decode(__UpperCamelCase						) + token
																								UpperCamelCase_		:							Optional[Any]  =		[]
																		else:
																								current_sub_tokens.append(__UpperCamelCase						)
												out_string += self.sp_model.decode(__UpperCamelCase						)
												return out_string.strip()





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

												return (out_vocab_file,)






