






'''simple docstring'''







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     :      List[Any]   					=logging.get_logger(__name__)

_A     :      Dict   					={'''vocab_file''': '''spiece.model'''}

_A     :      Dict   					={
    '''vocab_file''': {
        '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model''',
        '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model''',
    }
}

_A     :      int   					={
    '''xlnet-base-cased''': None,
    '''xlnet-large-cased''': None,
}

# Segments (not really needed)
_A     :      Any   					=0
_A     :      str   					=1
_A     :      Tuple   					=2
_A     :      Optional[Any]   					=3
_A     :      Optional[Any]   					=4

class _lowercase     (    _lowercase		):
							a    				=	VOCAB_FILES_NAMES
							a    				=	PRETRAINED_VOCAB_FILES_MAP
							a    				=	PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							a    				=	"""left"""




							def __init__(							self:    Tuple      ,							UpperCamelCase__:    Optional[int]      ,							UpperCamelCase__:    str=False      ,							UpperCamelCase__:    str=True      ,							UpperCamelCase__:    Dict=False      ,							UpperCamelCase__:    Dict="<s>"      ,							UpperCamelCase__:    int="</s>"      ,							UpperCamelCase__:    str="<unk>"      ,							UpperCamelCase__:    int="<sep>"      ,							UpperCamelCase__:    int="<pad>"      ,							UpperCamelCase__:    Any="<cls>"      ,							UpperCamelCase__:    Optional[Any]="<mask>"      ,							UpperCamelCase__:    Tuple=["<eop>", "<eod>"]      ,							UpperCamelCase__:    Optional[Dict[str, Any]] = None      ,							**UpperCamelCase__:    str      ,							):
														# Mask token behave like a normal word, i.e. include the space before it
														lowerCamelCase__ :     List[Any]	  =	AddedToken(UpperCamelCase__      ,							lstrip=UpperCamelCase__      ,							rstrip=UpperCamelCase__					) if isinstance(UpperCamelCase__      ,							UpperCamelCase__					) else mask_token

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

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

														lowerCamelCase__ :     Optional[int]	  =	3

														lowerCamelCase__ :     Optional[int]	  =	do_lower_case
														lowerCamelCase__ :     Union[str, Any]	  =	remove_space
														lowerCamelCase__ :     str	  =	keep_accents
														lowerCamelCase__ :     Optional[Any]	  =	vocab_file

														lowerCamelCase__ :     Dict	  =	spm.SentencePieceProcessor(**self.sp_model_kwargs					)
														self.sp_model.Load(UpperCamelCase__					)




							@property
							def 		lowerCamelCase_    (							self:    Any					):
														return len(self.sp_model					)




							def 		lowerCamelCase_    (							self:    Tuple					):
														lowerCamelCase__ :     List[Any]	  =	{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:    List[Any]					):
														lowerCamelCase__ :     List[str]	  =	self.__dict__.copy()
														lowerCamelCase__ :     List[str]	  =	None
														return state




							def __setstate__(							self:    List[Any]      ,							UpperCamelCase__:    Optional[Any]					):
														lowerCamelCase__ :     Tuple	  =	d

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

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




							def 		lowerCamelCase_    (							self:    Optional[Any]      ,							UpperCamelCase__:    Any					):
														if self.remove_space:
																					lowerCamelCase__ :     Any	  =	""" """.join(inputs.strip().split()					)
														else:
																					lowerCamelCase__ :     List[str]	  =	inputs
														lowerCamelCase__ :     Tuple	  =	outputs.replace("""``"""      ,							"""\""""					).replace("""''"""      ,							"""\""""					)

														if not self.keep_accents:
																					lowerCamelCase__ :     Dict	  =	unicodedata.normalize("""NFKD"""      ,							UpperCamelCase__					)
																					lowerCamelCase__ :     Optional[Any]	  =	"""""".join([c for c in outputs if not unicodedata.combining(UpperCamelCase__					)]					)
														if self.do_lower_case:
																					lowerCamelCase__ :     str	  =	outputs.lower()

														return outputs




							def 		lowerCamelCase_    (							self:    str      ,							UpperCamelCase__:    str					):
														lowerCamelCase__ :     Optional[int]	  =	self.preprocess_text(UpperCamelCase__					)
														lowerCamelCase__ :     Optional[Any]	  =	self.sp_model.encode(UpperCamelCase__      ,							out_type=UpperCamelCase__					)
														lowerCamelCase__ :     Optional[int]	  =	[]
														for piece in pieces:
																					if len(UpperCamelCase__					) > 1 and piece[-1] == str(""","""					) and piece[-2].isdigit():
																												lowerCamelCase__ :     Any	  =	self.sp_model.EncodeAsPieces(piece[:-1].replace(UpperCamelCase__      ,							""""""					)					)
																												if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
																																			if len(cur_pieces[0]					) == 1:
																																										lowerCamelCase__ :     int	  =	cur_pieces[1:]
																																			else:
																																										lowerCamelCase__ :     List[str]	  =	cur_pieces[0][1:]
																												cur_pieces.append(piece[-1]					)
																												new_pieces.extend(UpperCamelCase__					)
																					else:
																												new_pieces.append(UpperCamelCase__					)

														return new_pieces




							def 		lowerCamelCase_    (							self:    Dict      ,							UpperCamelCase__:    str					):
														return self.sp_model.PieceToId(UpperCamelCase__					)




							def 		lowerCamelCase_    (							self:    Dict      ,							UpperCamelCase__:    Union[str, Any]					):
														return self.sp_model.IdToPiece(UpperCamelCase__					)




							def 		lowerCamelCase_    (							self:    Union[str, Any]      ,							UpperCamelCase__:    Union[str, Any]					):
														lowerCamelCase__ :     Optional[int]	  =	"""""".join(UpperCamelCase__					).replace(UpperCamelCase__      ,							""" """					).strip()
														return out_string




							def 		lowerCamelCase_    (							self:    List[str]      ,							UpperCamelCase__:    List[int]      ,							UpperCamelCase__:    bool = False      ,							UpperCamelCase__:    bool = None      ,							UpperCamelCase__:    bool = True      ,							**UpperCamelCase__:    Optional[Any]      ,							):
														lowerCamelCase__ :     Tuple	  =	kwargs.pop("""use_source_tokenizer"""      ,							UpperCamelCase__					)

														lowerCamelCase__ :     List[Any]	  =	self.convert_ids_to_tokens(UpperCamelCase__      ,							skip_special_tokens=UpperCamelCase__					)

														# To avoid mixing byte-level and unicode for byte-level BPT
														# we need to build string separately for added tokens and byte-level tokens
														# cf. https://github.com/huggingface/transformers/issues/1133
														lowerCamelCase__ :     Tuple	  =	[]
														lowerCamelCase__ :     Optional[Any]	  =	[]
														for token in filtered_tokens:
																					if skip_special_tokens and token in self.all_special_ids:
																												continue
																					if token in self.added_tokens_encoder:
																												if current_sub_text:
																																			sub_texts.append(self.convert_tokens_to_string(UpperCamelCase__					)					)
																																			lowerCamelCase__ :     Optional[Any]	  =	[]
																												sub_texts.append(UpperCamelCase__					)
																					else:
																												current_sub_text.append(UpperCamelCase__					)
														if current_sub_text:
																					sub_texts.append(self.convert_tokens_to_string(UpperCamelCase__					)					)

														# Mimic the behavior of the Rust tokenizer:
														# By default, there are no spaces between special tokens
														lowerCamelCase__ :     int	  =	"""""".join(UpperCamelCase__					)

														lowerCamelCase__ :     Tuple	  =	(
														    clean_up_tokenization_spaces
														    if clean_up_tokenization_spaces is not None
														    else self.clean_up_tokenization_spaces
														)
														if clean_up_tokenization_spaces:
																					lowerCamelCase__ :     Optional[Any]	  =	self.clean_up_tokenization(UpperCamelCase__					)
																					return clean_text
														else:
																					return text




							def 		lowerCamelCase_    (							self:    Optional[int]      ,							UpperCamelCase__:    List[int]      ,							UpperCamelCase__:    Optional[List[int]] = None					):
														lowerCamelCase__ :     List[Any]	  =	[self.sep_token_id]
														lowerCamelCase__ :     List[Any]	  =	[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:    int      ,							UpperCamelCase__:    List[int]      ,							UpperCamelCase__:    Optional[List[int]] = None      ,							UpperCamelCase__:    bool = False					):

														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 ([0] * len(UpperCamelCase__					)) + [1] + ([0] * len(UpperCamelCase__					)) + [1, 1]
														return ([0] * len(UpperCamelCase__					)) + [1, 1]




							def 		lowerCamelCase_    (							self:    List[Any]      ,							UpperCamelCase__:    List[int]      ,							UpperCamelCase__:    Optional[List[int]] = None					):
														lowerCamelCase__ :     Any	  =	[self.sep_token_id]
														lowerCamelCase__ :     str	  =	[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:    List[Any]      ,							UpperCamelCase__:    str      ,							UpperCamelCase__:    Optional[str] = None					):
														if not os.path.isdir(UpperCamelCase__					):
																					logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''					)
																					return
														lowerCamelCase__ :     Optional[Any]	  =	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:
																												lowerCamelCase__ :     List[Any]	  =	self.sp_model.serialized_model_proto()
																												fi.write(UpperCamelCase__					)

														return (out_vocab_file,)









'''simple docstring'''







import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available


if is_vision_available():
			from PIL import Image

			from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor

@require_vision
class _lowercase     (    unittest.TestCase		):




							def 		lowerCamelCase_    (							self:    Union[str, Any]					):
														lowerCamelCase__ :     Any	  =	tempfile.mkdtemp()

														lowerCamelCase__ :     Any	  =	[
														    """[UNK]""",
														    """[CLS]""",
														    """[SEP]""",
														    """[PAD]""",
														    """[MASK]""",
														    """的""",
														    """价""",
														    """格""",
														    """是""",
														    """15""",
														    """便""",
														    """alex""",
														    """##andra""",
														    """，""",
														    """。""",
														    """-""",
														    """t""",
														    """shirt""",
														]
														lowerCamelCase__ :     Optional[int]	  =	os.path.join(self.tmpdirname      ,							VOCAB_FILES_NAMES["""vocab_file"""]					)
														with open(self.vocab_file      ,							"""w"""      ,							encoding="""utf-8"""					) as vocab_writer:
																					vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]					)					)

														lowerCamelCase__ :     Union[str, Any]	  =	{
														    """do_resize""": True,
														    """size""": {"""height""": 224, """width""": 224},
														    """do_center_crop""": True,
														    """crop_size""": {"""height""": 18, """width""": 18},
														    """do_normalize""": True,
														    """image_mean""": [0.48_145_466, 0.4_578_275, 0.40_821_073],
														    """image_std""": [0.26_862_954, 0.26_130_258, 0.27_577_711],
														    """do_convert_rgb""": True,
														}
														lowerCamelCase__ :     Optional[int]	  =	os.path.join(self.tmpdirname      ,							UpperCamelCase__					)
														with open(self.image_processor_file      ,							"""w"""      ,							encoding="""utf-8"""					) as fp:
																					json.dump(UpperCamelCase__      ,							UpperCamelCase__					)




							def 		lowerCamelCase_    (							self:    List[Any]      ,							**UpperCamelCase__:    Optional[int]					):
														return BertTokenizer.from_pretrained(self.tmpdirname      ,							**UpperCamelCase__					)




							def 		lowerCamelCase_    (							self:    Any      ,							**UpperCamelCase__:    List[str]					):
														return BertTokenizerFast.from_pretrained(self.tmpdirname      ,							**UpperCamelCase__					)




							def 		lowerCamelCase_    (							self:    str      ,							**UpperCamelCase__:    int					):
														return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname      ,							**UpperCamelCase__					)




							def 		lowerCamelCase_    (							self:    Optional[int]					):
														shutil.rmtree(self.tmpdirname					)




							def 		lowerCamelCase_    (							self:    List[str]					):
														lowerCamelCase__ :     int	  =	[np.random.randint(255      ,							size=(3, 30, 400)      ,							dtype=np.uinta					)]

														lowerCamelCase__ :     List[str]	  =	[Image.fromarray(np.moveaxis(UpperCamelCase__      ,							0      ,							-1					)					) for x in image_inputs]

														return image_inputs




							def 		lowerCamelCase_    (							self:    List[str]					):
														lowerCamelCase__ :     Tuple	  =	self.get_tokenizer()
														lowerCamelCase__ :     Any	  =	self.get_rust_tokenizer()
														lowerCamelCase__ :     str	  =	self.get_image_processor()

														lowerCamelCase__ :     str	  =	ChineseCLIPProcessor(tokenizer=UpperCamelCase__      ,							image_processor=UpperCamelCase__					)
														processor_slow.save_pretrained(self.tmpdirname					)
														lowerCamelCase__ :     List[Any]	  =	ChineseCLIPProcessor.from_pretrained(self.tmpdirname      ,							use_fast=UpperCamelCase__					)

														lowerCamelCase__ :     Any	  =	ChineseCLIPProcessor(tokenizer=UpperCamelCase__      ,							image_processor=UpperCamelCase__					)
														processor_fast.save_pretrained(self.tmpdirname					)
														lowerCamelCase__ :     List[Any]	  =	ChineseCLIPProcessor.from_pretrained(self.tmpdirname					)

														self.assertEqual(processor_slow.tokenizer.get_vocab()      ,							tokenizer_slow.get_vocab()					)
														self.assertEqual(processor_fast.tokenizer.get_vocab()      ,							tokenizer_fast.get_vocab()					)
														self.assertEqual(tokenizer_slow.get_vocab()      ,							tokenizer_fast.get_vocab()					)
														self.assertIsInstance(processor_slow.tokenizer      ,							UpperCamelCase__					)
														self.assertIsInstance(processor_fast.tokenizer      ,							UpperCamelCase__					)

														self.assertEqual(processor_slow.image_processor.to_json_string()      ,							image_processor.to_json_string()					)
														self.assertEqual(processor_fast.image_processor.to_json_string()      ,							image_processor.to_json_string()					)
														self.assertIsInstance(processor_slow.image_processor      ,							UpperCamelCase__					)
														self.assertIsInstance(processor_fast.image_processor      ,							UpperCamelCase__					)




							def 		lowerCamelCase_    (							self:    Optional[int]					):
														lowerCamelCase__ :     Optional[int]	  =	ChineseCLIPProcessor(tokenizer=self.get_tokenizer()      ,							image_processor=self.get_image_processor()					)
														processor.save_pretrained(self.tmpdirname					)

														lowerCamelCase__ :     str	  =	self.get_tokenizer(cls_token="""(CLS)"""      ,							sep_token="""(SEP)"""					)
														lowerCamelCase__ :     Optional[int]	  =	self.get_image_processor(do_normalize=UpperCamelCase__					)

														lowerCamelCase__ :     Optional[int]	  =	ChineseCLIPProcessor.from_pretrained(
														    self.tmpdirname      ,							cls_token="""(CLS)"""      ,							sep_token="""(SEP)"""      ,							do_normalize=UpperCamelCase__					)

														self.assertEqual(processor.tokenizer.get_vocab()      ,							tokenizer_add_kwargs.get_vocab()					)
														self.assertIsInstance(processor.tokenizer      ,							UpperCamelCase__					)

														self.assertEqual(processor.image_processor.to_json_string()      ,							image_processor_add_kwargs.to_json_string()					)
														self.assertIsInstance(processor.image_processor      ,							UpperCamelCase__					)




							def 		lowerCamelCase_    (							self:    Tuple					):
														lowerCamelCase__ :     Optional[Any]	  =	self.get_image_processor()
														lowerCamelCase__ :     Optional[int]	  =	self.get_tokenizer()

														lowerCamelCase__ :     int	  =	ChineseCLIPProcessor(tokenizer=UpperCamelCase__      ,							image_processor=UpperCamelCase__					)

														lowerCamelCase__ :     Tuple	  =	self.prepare_image_inputs()

														lowerCamelCase__ :     str	  =	image_processor(UpperCamelCase__      ,							return_tensors="""np"""					)
														lowerCamelCase__ :     Optional[int]	  =	processor(images=UpperCamelCase__      ,							return_tensors="""np"""					)

														for key in input_feat_extract.keys():
																					self.assertAlmostEqual(input_feat_extract[key].sum()      ,							input_processor[key].sum()      ,							delta=1e-2					)




							def 		lowerCamelCase_    (							self:    Tuple					):
														lowerCamelCase__ :     int	  =	self.get_image_processor()
														lowerCamelCase__ :     Optional[int]	  =	self.get_tokenizer()

														lowerCamelCase__ :     Union[str, Any]	  =	ChineseCLIPProcessor(tokenizer=UpperCamelCase__      ,							image_processor=UpperCamelCase__					)

														lowerCamelCase__ :     Union[str, Any]	  =	"""Alexandra，T-shirt的价格是15便士。"""

														lowerCamelCase__ :     Optional[Any]	  =	processor(text=UpperCamelCase__					)

														lowerCamelCase__ :     Optional[Any]	  =	tokenizer(UpperCamelCase__					)

														for key in encoded_tok.keys():
																					self.assertListEqual(encoded_tok[key]      ,							encoded_processor[key]					)




							def 		lowerCamelCase_    (							self:    Optional[Any]					):
														lowerCamelCase__ :     Optional[int]	  =	self.get_image_processor()
														lowerCamelCase__ :     List[Any]	  =	self.get_tokenizer()

														lowerCamelCase__ :     Optional[Any]	  =	ChineseCLIPProcessor(tokenizer=UpperCamelCase__      ,							image_processor=UpperCamelCase__					)

														lowerCamelCase__ :     Dict	  =	"""Alexandra，T-shirt的价格是15便士。"""
														lowerCamelCase__ :     Any	  =	self.prepare_image_inputs()

														lowerCamelCase__ :     Optional[int]	  =	processor(text=UpperCamelCase__      ,							images=UpperCamelCase__					)

														self.assertListEqual(list(inputs.keys()					)      ,							["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""]					)

														# test if it raises when no input is passed
														with pytest.raises(UpperCamelCase__					):
																					processor()




							def 		lowerCamelCase_    (							self:    Dict					):
														lowerCamelCase__ :     Dict	  =	self.get_image_processor()
														lowerCamelCase__ :     Any	  =	self.get_tokenizer()

														lowerCamelCase__ :     Union[str, Any]	  =	ChineseCLIPProcessor(tokenizer=UpperCamelCase__      ,							image_processor=UpperCamelCase__					)

														lowerCamelCase__ :     str	  =	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

														lowerCamelCase__ :     Any	  =	processor.batch_decode(UpperCamelCase__					)
														lowerCamelCase__ :     int	  =	tokenizer.batch_decode(UpperCamelCase__					)

														self.assertListEqual(UpperCamelCase__      ,							UpperCamelCase__					)




							def 		lowerCamelCase_    (							self:    Optional[Any]					):
														lowerCamelCase__ :     Dict	  =	self.get_image_processor()
														lowerCamelCase__ :     int	  =	self.get_tokenizer()

														lowerCamelCase__ :     Optional[Any]	  =	ChineseCLIPProcessor(tokenizer=UpperCamelCase__      ,							image_processor=UpperCamelCase__					)

														lowerCamelCase__ :     Optional[Any]	  =	"""Alexandra，T-shirt的价格是15便士。"""
														lowerCamelCase__ :     Tuple	  =	self.prepare_image_inputs()

														lowerCamelCase__ :     str	  =	processor(text=UpperCamelCase__      ,							images=UpperCamelCase__					)

														self.assertListEqual(list(inputs.keys()					)      ,							processor.model_input_names					)

