



A__								=						[
    999,
    800,
    799,
    600,
    599,
    500,
    400,
    399,
    377,
    355,
    333,
    311,
    288,
    266,
    244,
    222,
    200,
    199,
    177,
    155,
    133,
    111,
    88,
    66,
    44,
    22,
    0,
]

A__								=						[
    999,
    976,
    952,
    928,
    905,
    882,
    858,
    857,
    810,
    762,
    715,
    714,
    572,
    429,
    428,
    286,
    285,
    238,
    190,
    143,
    142,
    118,
    95,
    71,
    47,
    24,
    0,
]

A__								=						[
    999,
    988,
    977,
    966,
    955,
    944,
    933,
    922,
    911,
    900,
    899,
    879,
    859,
    840,
    820,
    800,
    799,
    766,
    733,
    700,
    699,
    650,
    600,
    599,
    500,
    499,
    400,
    399,
    350,
    300,
    299,
    266,
    233,
    200,
    199,
    179,
    159,
    140,
    120,
    100,
    99,
    88,
    77,
    66,
    55,
    44,
    33,
    22,
    11,
    0,
]

A__								=						[
    999,
    995,
    992,
    989,
    985,
    981,
    978,
    975,
    971,
    967,
    964,
    961,
    957,
    956,
    951,
    947,
    942,
    937,
    933,
    928,
    923,
    919,
    914,
    913,
    908,
    903,
    897,
    892,
    887,
    881,
    876,
    871,
    870,
    864,
    858,
    852,
    846,
    840,
    834,
    828,
    827,
    820,
    813,
    806,
    799,
    792,
    785,
    784,
    777,
    770,
    763,
    756,
    749,
    742,
    741,
    733,
    724,
    716,
    707,
    699,
    698,
    688,
    677,
    666,
    656,
    655,
    645,
    634,
    623,
    613,
    612,
    598,
    584,
    570,
    569,
    555,
    541,
    527,
    526,
    505,
    484,
    483,
    462,
    440,
    439,
    396,
    395,
    352,
    351,
    308,
    307,
    264,
    263,
    220,
    219,
    176,
    132,
    88,
    44,
    0,
]

A__								=						[
    999,
    997,
    995,
    992,
    990,
    988,
    986,
    984,
    981,
    979,
    977,
    975,
    972,
    970,
    968,
    966,
    964,
    961,
    959,
    957,
    956,
    954,
    951,
    949,
    946,
    944,
    941,
    939,
    936,
    934,
    931,
    929,
    926,
    924,
    921,
    919,
    916,
    914,
    913,
    910,
    907,
    905,
    902,
    899,
    896,
    893,
    891,
    888,
    885,
    882,
    879,
    877,
    874,
    871,
    870,
    867,
    864,
    861,
    858,
    855,
    852,
    849,
    846,
    843,
    840,
    837,
    834,
    831,
    828,
    827,
    824,
    821,
    817,
    814,
    811,
    808,
    804,
    801,
    798,
    795,
    791,
    788,
    785,
    784,
    780,
    777,
    774,
    770,
    766,
    763,
    760,
    756,
    752,
    749,
    746,
    742,
    741,
    737,
    733,
    730,
    726,
    722,
    718,
    714,
    710,
    707,
    703,
    699,
    698,
    694,
    690,
    685,
    681,
    677,
    673,
    669,
    664,
    660,
    656,
    655,
    650,
    646,
    641,
    636,
    632,
    627,
    622,
    618,
    613,
    612,
    607,
    602,
    596,
    591,
    586,
    580,
    575,
    570,
    569,
    563,
    557,
    551,
    545,
    539,
    533,
    527,
    526,
    519,
    512,
    505,
    498,
    491,
    484,
    483,
    474,
    466,
    457,
    449,
    440,
    439,
    428,
    418,
    407,
    396,
    395,
    381,
    366,
    352,
    351,
    330,
    308,
    307,
    286,
    264,
    263,
    242,
    220,
    219,
    176,
    175,
    132,
    131,
    88,
    44,
    0,
]

A__								=						[
    999,
    991,
    982,
    974,
    966,
    958,
    950,
    941,
    933,
    925,
    916,
    908,
    900,
    899,
    874,
    850,
    825,
    800,
    799,
    700,
    600,
    500,
    400,
    300,
    200,
    100,
    0,
]

A__								=						[
    999,
    992,
    985,
    978,
    971,
    964,
    957,
    949,
    942,
    935,
    928,
    921,
    914,
    907,
    900,
    899,
    879,
    859,
    840,
    820,
    800,
    799,
    766,
    733,
    700,
    699,
    650,
    600,
    599,
    500,
    499,
    400,
    399,
    300,
    299,
    200,
    199,
    100,
    99,
    0,
]

A__								=						[
    999,
    996,
    992,
    989,
    985,
    982,
    979,
    975,
    972,
    968,
    965,
    961,
    958,
    955,
    951,
    948,
    944,
    941,
    938,
    934,
    931,
    927,
    924,
    920,
    917,
    914,
    910,
    907,
    903,
    900,
    899,
    891,
    884,
    876,
    869,
    861,
    853,
    846,
    838,
    830,
    823,
    815,
    808,
    800,
    799,
    788,
    777,
    766,
    755,
    744,
    733,
    722,
    711,
    700,
    699,
    688,
    677,
    666,
    655,
    644,
    633,
    622,
    611,
    600,
    599,
    585,
    571,
    557,
    542,
    528,
    514,
    500,
    499,
    485,
    471,
    457,
    442,
    428,
    414,
    400,
    399,
    379,
    359,
    340,
    320,
    300,
    299,
    279,
    259,
    240,
    220,
    200,
    199,
    166,
    133,
    100,
    99,
    66,
    33,
    0,
]






import os
import unicodedata
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 SPIECE_UNDERLINE, logging


A__								=						logging.get_logger(__name__)

A__								=						{'''vocab_file''': '''spiece.model'''}

A__								=						{
    '''vocab_file''': {
        '''TsinghuaAI/CPM-Generate''': '''https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model''',
    }
}


class       a							(			__lowerCamelCase				):



		def __init__(     self     :Union[str, Any]   ,__lowercase     :Optional[Any]   ,__lowercase     :int=False   ,__lowercase     :int=True   ,__lowercase     :Optional[int]=False   ,__lowercase     :List[str]="<s>"   ,__lowercase     :str="</s>"   ,__lowercase     :Dict="<unk>"   ,__lowercase     :Optional[int]="<sep>"   ,__lowercase     :Tuple="<pad>"   ,__lowercase     :Union[str, Any]="<cls>"   ,__lowercase     :Dict="<mask>"   ,__lowercase     :List[Any]=["<eop>", "<eod>"]   ,__lowercase     :Optional[Dict[str, Any]] = None   ,**__lowercase     :int   ,):
						snake_case__	:      Any     		=	AddedToken(__lowercase   ,lstrip=__lowercase   ,rstrip=__lowercase  ) if isinstance(__lowercase   ,__lowercase  ) else mask_token

						snake_case__	:      Tuple     		=	{} if sp_model_kwargs is None else sp_model_kwargs

						super().__init__(
						    do_lower_case=__lowercase   ,remove_space=__lowercase   ,keep_accents=__lowercase   ,bos_token=__lowercase   ,eos_token=__lowercase   ,unk_token=__lowercase   ,sep_token=__lowercase   ,pad_token=__lowercase   ,cls_token=__lowercase   ,mask_token=__lowercase   ,additional_special_tokens=__lowercase   ,sp_model_kwargs=self.sp_model_kwargs   ,**__lowercase   ,)

						snake_case__	:      Optional[Any]     		=	3

						snake_case__	:      List[str]     		=	do_lower_case
						snake_case__	:      Union[str, Any]     		=	remove_space
						snake_case__	:      Tuple     		=	keep_accents
						snake_case__	:      List[str]     		=	vocab_file

						snake_case__	:      Optional[Any]     		=	spm.SentencePieceProcessor(**self.sp_model_kwargs  )
						self.sp_model.Load(__lowercase  )

						try:
										import jieba
						except ModuleNotFoundError as error:
										raise error.__class__(
										    '''You need to install jieba to use CpmTokenizer or CpmTokenizerFast. '''
										    '''See https://pypi.org/project/jieba/ for installation.'''  )
						snake_case__	:      List[Any]     		=	jieba
						snake_case__	:      Union[str, Any]     		=	str.maketrans(''' \n'''   ,'''\u2582\u2583'''  )



		@property
		# Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size
		def 	__lowerCamelCase			(     self     :Union[str, Any]  ):
						return len(self.sp_model  )



		def 	__lowerCamelCase			(     self     :Any  ):
						snake_case__	:      Optional[Any]     		=	{self.convert_ids_to_tokens(__lowercase  ): i for i in range(self.vocab_size  )}
						vocab.update(self.added_tokens_encoder  )
						return vocab



		def __getstate__(     self     :List[str]  ):
						snake_case__	:      Optional[Any]     		=	self.__dict__.copy()
						snake_case__	:      Optional[int]     		=	None
						return state



		def __setstate__(     self     :int   ,__lowercase     :str  ):
						snake_case__	:      Tuple     		=	d

						# for backward compatibility
						if not hasattr(self   ,'''sp_model_kwargs'''  ):
										snake_case__	:      List[Any]     		=	{}

						snake_case__	:      List[Any]     		=	spm.SentencePieceProcessor(**self.sp_model_kwargs  )
						self.sp_model.Load(self.vocab_file  )



		def 	__lowerCamelCase			(     self     :Dict   ,__lowercase     :Optional[int]  ):
						if self.remove_space:
										snake_case__	:      int     		=	''' '''.join(inputs.strip().split()  )
						else:
										snake_case__	:      Tuple     		=	inputs
						snake_case__	:      List[Any]     		=	outputs.replace('''``'''   ,'''"'''  ).replace('''\'\''''   ,'''"'''  )

						if not self.keep_accents:
										snake_case__	:      Any     		=	unicodedata.normalize('''NFKD'''   ,__lowercase  )
										snake_case__	:      Dict     		=	''''''.join([c for c in outputs if not unicodedata.combining(__lowercase  )]  )
						if self.do_lower_case:
										snake_case__	:      str     		=	outputs.lower()

						return outputs



		def 	__lowerCamelCase			(     self     :Optional[Any]   ,__lowercase     :str  ):
						snake_case__	:      Dict     		=	self.preprocess_text(__lowercase  )
						snake_case__	:      Any     		=	self.sp_model.encode(__lowercase   ,out_type=__lowercase  )
						snake_case__	:      List[str]     		=	[]
						for piece in pieces:
										if len(__lowercase  ) > 1 and piece[-1] == str(''','''  ) and piece[-2].isdigit():
														snake_case__	:      Union[str, Any]     		=	self.sp_model.EncodeAsPieces(piece[:-1].replace(__lowercase   ,''''''  )  )
														if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
																		if len(cur_pieces[0]  ) == 1:
																						snake_case__	:      Optional[Any]     		=	cur_pieces[1:]
																		else:
																						snake_case__	:      Optional[Any]     		=	cur_pieces[0][1:]
														cur_pieces.append(piece[-1]  )
														new_pieces.extend(__lowercase  )
										else:
														new_pieces.append(__lowercase  )

						return new_pieces



		def 	__lowerCamelCase			(     self     :Union[str, Any]   ,__lowercase     :Dict  ):
						return self.sp_model.PieceToId(__lowercase  )



		def 	__lowerCamelCase			(     self     :Tuple   ,__lowercase     :Optional[int]  ):
						return self.sp_model.IdToPiece(__lowercase  )



		def 	__lowerCamelCase			(     self     :Tuple   ,__lowercase     :List[Any]  ):
						snake_case__	:      Union[str, Any]     		=	''''''.join(__lowercase  ).replace(__lowercase   ,''' '''  ).strip()
						return out_string



		def 	__lowerCamelCase			(     self     :List[Any]   ,__lowercase     :List[int]   ,__lowercase     :Optional[List[int]] = None  ):
						snake_case__	:      Optional[Any]     		=	[self.sep_token_id]
						snake_case__	:      str     		=	[self.cls_token_id]
						if token_ids_a is None:
										return token_ids_a + sep + cls
						return token_ids_a + sep + token_ids_a + sep + cls



		def 	__lowerCamelCase			(     self     :str   ,__lowercase     :List[int]   ,__lowercase     :Optional[List[int]] = None   ,__lowercase     :bool = False  ):

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

						if token_ids_a is not None:
										return ([0] * len(__lowercase  )) + [1] + ([0] * len(__lowercase  )) + [1, 1]
						return ([0] * len(__lowercase  )) + [1, 1]



		def 	__lowerCamelCase			(     self     :List[str]   ,__lowercase     :List[int]   ,__lowercase     :Optional[List[int]] = None  ):
						snake_case__	:      List[Any]     		=	[self.sep_token_id]
						snake_case__	:      Optional[Any]     		=	[2]

						if token_ids_a is None:
										return len(token_ids_a + sep  ) * [0] + cls_segment_id
						return len(token_ids_a + sep  ) * [0] + len(token_ids_a + sep  ) * [1] + cls_segment_id



		def 	__lowerCamelCase			(     self     :Optional[int]   ,__lowercase     :str   ,__lowercase     :Optional[str] = None  ):
						if not os.path.isdir(__lowercase  ):
										logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""  )
										return
						snake_case__	:      Union[str, Any]     		=	os.path.join(
						    __lowercase   ,(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']  )

						if os.path.abspath(self.vocab_file  ) != os.path.abspath(__lowercase  ) and os.path.isfile(self.vocab_file  ):
										copyfile(self.vocab_file   ,__lowercase  )
						elif not os.path.isfile(self.vocab_file  ):
										with open(__lowercase   ,'''wb'''  ) as fi:
														snake_case__	:      str     		=	self.sp_model.serialized_model_proto()
														fi.write(__lowercase  )

						return (out_vocab_file,)






		def 	__lowerCamelCase			(     self     :Union[str, Any]   ,*__lowercase     :Optional[int]   ,**__lowercase     :Dict  ):
						snake_case__	:      Dict     		=	super()._decode(*__lowercase   ,**__lowercase  )
						snake_case__	:      List[Any]     		=	text.replace(''' '''   ,''''''  ).replace('''\u2582'''   ,''' '''  ).replace('''\u2583'''   ,'''\n'''  )
						return text

