






'''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


lowerCamelCase_												=					logging.get_logger(__name__)

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

lowerCamelCase_												=					{
    '''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''',
    }
}

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

# Segments (not really needed)
lowerCamelCase_												=					0
lowerCamelCase_												=					1
lowerCamelCase_												=					2
lowerCamelCase_												=					3
lowerCamelCase_												=					4

class    _UpperCAmelCase			(     snake_case_						):




   """simple docstring"""

   snake_case    				=						VOCAB_FILES_NAMES
   snake_case    				=						PRETRAINED_VOCAB_FILES_MAP
   snake_case    				=						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   snake_case    				=						'''left'''

   def __init__(		self :	Any      ,      __UpperCAmelCase :	Tuple      ,      __UpperCAmelCase :	Tuple=False      ,      __UpperCAmelCase :	Optional[Any]=True      ,      __UpperCAmelCase :	List[Any]=False      ,      __UpperCAmelCase :	List[Any]="<s>"      ,      __UpperCAmelCase :	Optional[Any]="</s>"      ,      __UpperCAmelCase :	Dict="<unk>"      ,      __UpperCAmelCase :	Tuple="<sep>"      ,      __UpperCAmelCase :	List[str]="<pad>"      ,      __UpperCAmelCase :	int="<cls>"      ,      __UpperCAmelCase :	Dict="<mask>"      ,      __UpperCAmelCase :	Optional[Any]=["<eop>", "<eod>"]      ,      __UpperCAmelCase :	Optional[Dict[str, Any]] = None      ,      **__UpperCAmelCase :	List[Any]      ,      ):





         '''simple docstring'''





         _A			     =	AddedToken(__UpperCAmelCase      ,      lstrip=__UpperCAmelCase      ,      rstrip=__UpperCAmelCase		) if isinstance(__UpperCAmelCase      ,      __UpperCAmelCase		) else mask_token

         _A			     =	{} 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      ,      )

         _A			     =	3

         _A			     =	do_lower_case
         _A			     =	remove_space
         _A			     =	keep_accents
         _A			     =	vocab_file

         _A			     =	spm.SentencePieceProcessor(**self.sp_model_kwargs		)
         self.sp_model.Load(__UpperCAmelCase		)

   @property
   def 						lowerCAmelCase (		self :	List[Any]		):





         '''simple docstring'''





         return len(self.sp_model		)

   def 						lowerCAmelCase (		self :	int		):





         '''simple docstring'''





         _A			     =	{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 :	Union[str, Any]		):





         '''simple docstring'''





         _A			     =	self.__dict__.copy()
         _A			     =	None
         return state

   def __setstate__(		self :	Any      ,      __UpperCAmelCase :	str		):





         '''simple docstring'''





         _A			     =	d

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

         _A			     =	spm.SentencePieceProcessor(**self.sp_model_kwargs		)
         self.sp_model.Load(self.vocab_file		)

   def 						lowerCAmelCase (		self :	Optional[int]      ,      __UpperCAmelCase :	Optional[int]		):





         '''simple docstring'''





         if self.remove_space:
               _A			     =	" ".join(inputs.strip().split()		)
         else:
               _A			     =	inputs
         _A			     =	outputs.replace("``"      ,      "\""		).replace("''"      ,      "\""		)

         if not self.keep_accents:
               _A			     =	unicodedata.normalize("NFKD"      ,      __UpperCAmelCase		)
               _A			     =	"".join([c for c in outputs if not unicodedata.combining(__UpperCAmelCase		)]		)
         if self.do_lower_case:
               _A			     =	outputs.lower()

         return outputs

   def 						lowerCAmelCase (		self :	str      ,      __UpperCAmelCase :	str		):





         '''simple docstring'''





         _A			     =	self.preprocess_text(__UpperCAmelCase		)
         _A			     =	self.sp_model.encode(__UpperCAmelCase      ,      out_type=__UpperCAmelCase		)
         _A			     =	[]
         for piece in pieces:
               if len(__UpperCAmelCase		) > 1 and piece[-1] == str(","		) and piece[-2].isdigit():
                     _A			     =	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:
                                 _A			     =	cur_pieces[1:]
                           else:
                                 _A			     =	cur_pieces[0][1:]
                     cur_pieces.append(piece[-1]		)
                     new_pieces.extend(__UpperCAmelCase		)
               else:
                     new_pieces.append(__UpperCAmelCase		)

         return new_pieces

   def 						lowerCAmelCase (		self :	Optional[int]      ,      __UpperCAmelCase :	Optional[int]		):





         '''simple docstring'''





         return self.sp_model.PieceToId(__UpperCAmelCase		)

   def 						lowerCAmelCase (		self :	List[str]      ,      __UpperCAmelCase :	List[Any]		):





         '''simple docstring'''





         return self.sp_model.IdToPiece(__UpperCAmelCase		)

   def 						lowerCAmelCase (		self :	str      ,      __UpperCAmelCase :	Dict		):





         '''simple docstring'''





         _A			     =	"".join(__UpperCAmelCase		).replace(__UpperCAmelCase      ,      " "		).strip()
         return out_string

   def 						lowerCAmelCase (		self :	int      ,      __UpperCAmelCase :	List[int]      ,      __UpperCAmelCase :	bool = False      ,      __UpperCAmelCase :	bool = None      ,      __UpperCAmelCase :	bool = True      ,      **__UpperCAmelCase :	List[Any]      ,      ):





         '''simple docstring'''





         _A			     =	kwargs.pop("use_source_tokenizer"      ,      __UpperCAmelCase		)

         _A			     =	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
         _A			     =	[]
         _A			     =	[]
         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		)		)
                           _A			     =	[]
                     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
         _A			     =	"".join(__UpperCAmelCase		)

         _A			     =	(
             clean_up_tokenization_spaces
             if clean_up_tokenization_spaces is not None
             else self.clean_up_tokenization_spaces
         )
         if clean_up_tokenization_spaces:
               _A			     =	self.clean_up_tokenization(__UpperCAmelCase		)
               return clean_text
         else:
               return text

   def 						lowerCAmelCase (		self :	Dict      ,      __UpperCAmelCase :	List[int]      ,      __UpperCAmelCase :	Optional[List[int]] = None		):





         '''simple docstring'''





         _A			     =	[self.sep_token_id]
         _A			     =	[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 :	Any      ,      __UpperCAmelCase :	List[int]      ,      __UpperCAmelCase :	Optional[List[int]] = None      ,      __UpperCAmelCase :	bool = False		):





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

   def 						lowerCAmelCase (		self :	Any      ,      __UpperCAmelCase :	List[int]      ,      __UpperCAmelCase :	Optional[List[int]] = None		):





         '''simple docstring'''





         _A			     =	[self.sep_token_id]
         _A			     =	[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		):





         '''simple docstring'''





         if not os.path.isdir(__UpperCAmelCase		):
               logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''		)
               return
         _A			     =	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:
                     _A			     =	self.sp_model.serialized_model_proto()
                     fi.write(__UpperCAmelCase		)

         return (out_vocab_file,)







def   lowerCAmelCase_						(					_snake_case      :  str      ,   _snake_case      :  str   )				->		bool:







	'''simple docstring'''


	__magic_name__		:		Union[str, Any]	   =     len(_snake_case   ) + 1
	__magic_name__		:		List[str]	   =     len(_snake_case   ) + 1

	# dp is a 2d matrix where dp[i][j] denotes whether prefix string of
	# length i of input_string matches with prefix string of length j of
	# given pattern.
	# "dp" stands for dynamic programming.
	__magic_name__		:		str	   =     [[0 for i in range(_snake_case   )] for j in range(_snake_case   )]

	# since string of zero length match pattern of zero length
	__magic_name__		:		Optional[int]	   =     1

	# since pattern of zero length will never match with string of non-zero length
	for i in range(1      ,   _snake_case   ):
		__magic_name__		:		Optional[int]	   =     0

	# since string of zero length will match with pattern where there
	# is at least one * alternatively
	for j in range(1      ,   _snake_case   ):
		__magic_name__		:		Union[str, Any]	   =     dp[0][j - 2] if pattern[j - 1] == "*" else 0

	# now using bottom-up approach to find for all remaining lengths
	for i in range(1      ,   _snake_case   ):
		for j in range(1      ,   _snake_case   ):
			if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".":
				__magic_name__		:		Optional[int]	   =     dp[i - 1][j - 1]

			elif pattern[j - 1] == "*":
				if dp[i][j - 2] == 1:
					__magic_name__		:		Optional[Any]	   =     1
				elif pattern[j - 2] in (input_string[i - 1], "."):
					__magic_name__		:		List[Any]	   =     dp[i - 1][j]
				else:
					__magic_name__		:		Union[str, Any]	   =     0
			else:
				__magic_name__		:		Dict	   =     0

	return bool(dp[-1][-1]   )


if __name__ == "__main__":
				import doctest

				doctest.testmod()
				# inputing the strings
				# input_string = input("input a string :")
				# pattern = input("input a pattern :")

				snake_case :		Optional[Any]							      =  "aab"
				snake_case :		List[str]							      =  "c*a*b"

				# using function to check whether given string matches the given pattern
				if match_pattern(input_string, pattern):
								print(F"{input_string} matches the given pattern {pattern}")
				else:
								print(F"{input_string} does not match with the given pattern {pattern}")
