





import io
import os
import unicodedata
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

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


lowerCAmelCase							:       Optional[int]					 =	logging.get_logger(__name__)

lowerCAmelCase							:       Any					 =	"""▁"""

lowerCAmelCase							:       List[str]					 =	{"""vocab_file""": """vocab.txt""", """sentencepiece_model_ckpt""": """sentencepiece.bpe.model"""}

lowerCAmelCase							:       Union[str, Any]					 =	{
    """sentencepiece_model_file""": """sentencepiece.bpe.model""",
    """vocab_file""": """vocab.txt""",
}

lowerCAmelCase							:       List[Any]					 =	{
    """vocab_file""": {
        """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""",
        """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""",
    },
    """sentencepiece_model_file""": {
        """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""",
        """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""",
    },
}

lowerCAmelCase							:       List[Any]					 =	{
    """ernie-m-base""": 514,
    """ernie-m-large""": 514,
}

lowerCAmelCase							:       Optional[Any]					 =	{
    """ernie-m-base""": {"""do_lower_case""": False},
    """ernie-m-large""": {"""do_lower_case""": False},
}





class 				a			(      __lowercase			):
				SCREAMING_SNAKE_CASE__     :   List[str]		      =     ["input_ids"]

				SCREAMING_SNAKE_CASE__     :   Dict		      =     VOCAB_FILES_NAMES
				SCREAMING_SNAKE_CASE__     :   Dict		      =     PRETRAINED_INIT_CONFIGURATION
				SCREAMING_SNAKE_CASE__     :   str		      =     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				SCREAMING_SNAKE_CASE__     :   List[Any]		      =     PRETRAINED_VOCAB_FILES_MAP
				SCREAMING_SNAKE_CASE__     :   Any		      =     RESOURCE_FILES_NAMES






				def __init__(  self			, _lowerCAmelCase			, _lowerCAmelCase=None			, _lowerCAmelCase=False			, _lowerCAmelCase="utf8"			, _lowerCAmelCase="[UNK]"			, _lowerCAmelCase="[SEP]"			, _lowerCAmelCase="[PAD]"			, _lowerCAmelCase="[CLS]"			, _lowerCAmelCase="[MASK]"			, _lowerCAmelCase = None			, **_lowerCAmelCase			, ):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				int           =   {} if sp_model_kwargs is None else sp_model_kwargs
										super().__init__(
										    do_lower_case=_lowerCAmelCase			, unk_token=_lowerCAmelCase			, sep_token=_lowerCAmelCase			, pad_token=_lowerCAmelCase			, cls_token=_lowerCAmelCase			, mask_token=_lowerCAmelCase			, vocab_file=_lowerCAmelCase			, encoding=_lowerCAmelCase			, sp_model_kwargs=self.sp_model_kwargs			, **_lowerCAmelCase			, )
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   do_lower_case
										__SCREAMING_SNAKE_CASE:				Tuple           =   sentencepiece_model_ckpt
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   spm.SentencePieceProcessor(**self.sp_model_kwargs					)
										self.sp_model.Load(_lowerCAmelCase					)

										# to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning
										if vocab_file is not None:
																__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.load_vocab(filepath=_lowerCAmelCase					)
										else:
																__SCREAMING_SNAKE_CASE:				Dict           =   {self.sp_model.id_to_piece(_lowerCAmelCase					): id for id in range(self.sp_model.get_piece_size()					)}
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   {v: k for k, v in self.vocab.items()}






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										if text is None:
																return None

										__SCREAMING_SNAKE_CASE:				int           =   self.tokenize(_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				Tuple           =   '''''', []

										for i, ch in enumerate(_lowerCAmelCase					):
																if ch in self.SP_CHAR_MAPPING:
																						__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.SP_CHAR_MAPPING.get(_lowerCAmelCase					)
																else:
																						__SCREAMING_SNAKE_CASE:				Tuple           =   unicodedata.normalize('''NFKC'''			, _lowerCAmelCase					)
																if self.is_whitespace(_lowerCAmelCase					):
																						continue
																normalized_text += ch
																char_mapping.extend([i] * len(_lowerCAmelCase					)					)

										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				Optional[int]           =   normalized_text, [], 0

										if self.do_lower_case:
																__SCREAMING_SNAKE_CASE:				Any           =   text.lower()

										for token in split_tokens:
																if token[:1] == "▁":
																						__SCREAMING_SNAKE_CASE:				List[Any]           =   token[1:]
																__SCREAMING_SNAKE_CASE:				List[Any]           =   text[offset:].index(_lowerCAmelCase					) + offset
																__SCREAMING_SNAKE_CASE:				Optional[Any]           =   start + len(_lowerCAmelCase					)

																token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1)					)
																__SCREAMING_SNAKE_CASE:				List[Any]           =   end
										return token_mapping






				@property
				def 			snake_case_    (  self					):

										"""simple docstring"""




										return len(self.vocab					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




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






				def __getstate__(  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Tuple           =   self.__dict__.copy()
										__SCREAMING_SNAKE_CASE:				List[Any]           =   None
										return state






				def __setstate__(  self			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[str]           =   d

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

										__SCREAMING_SNAKE_CASE:				Dict           =   spm.SentencePieceProcessor(**self.sp_model_kwargs					)
										self.sp_model.Load(self.sentencepiece_model_ckpt					)






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										return "".join((self.SP_CHAR_MAPPING.get(_lowerCAmelCase			, _lowerCAmelCase					) for c in text)					)






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase=False			, _lowerCAmelCase=64			, _lowerCAmelCase=0.1					):

										"""simple docstring"""




										if self.sp_model_kwargs.get('''enable_sampling'''					) is True:
																__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   True
										if self.sp_model_kwargs.get('''alpha'''					) is not None:
																__SCREAMING_SNAKE_CASE:				Tuple           =   self.sp_model_kwargs.get('''alpha'''					)
										if self.sp_model_kwargs.get('''nbest_size'''					) is not None:
																__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.sp_model_kwargs.get('''nbest_size'''					)

										if not enable_sampling:
																__SCREAMING_SNAKE_CASE:				Dict           =   self.sp_model.EncodeAsPieces(_lowerCAmelCase					)
										else:
																__SCREAMING_SNAKE_CASE:				Optional[int]           =   self.sp_model.SampleEncodeAsPieces(_lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				str           =   []
										for pi, piece in enumerate(_lowerCAmelCase					):
																if piece == SPIECE_UNDERLINE:
																						if not pieces[pi + 1].startswith(_lowerCAmelCase					) and pi != 0:
																												new_pieces.append(_lowerCAmelCase					)
																												continue
																						else:
																												continue
																__SCREAMING_SNAKE_CASE:				int           =   0
																for i, chunk in enumerate(_lowerCAmelCase					):
																						if chunk == SPIECE_UNDERLINE:
																												continue
																						if self.is_ch_char(_lowerCAmelCase					) or self.is_punct(_lowerCAmelCase					):
																												if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
																																		new_pieces.append(piece[lst_i:i]					)
																												new_pieces.append(_lowerCAmelCase					)
																												__SCREAMING_SNAKE_CASE:				str           =   i + 1
																						elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit():
																												if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
																																		new_pieces.append(piece[lst_i:i]					)
																												__SCREAMING_SNAKE_CASE:				Dict           =   i
																						elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit():
																												if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
																																		new_pieces.append(piece[lst_i:i]					)
																												__SCREAMING_SNAKE_CASE:				Optional[int]           =   i
																if len(_lowerCAmelCase					) > lst_i:
																						new_pieces.append(piece[lst_i:]					)
										return new_pieces






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   ''''''.join(_lowerCAmelCase					).replace(_lowerCAmelCase			, ''' '''					).strip()
										return out_string






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Dict           =   self.convert_ids_to_tokens(_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				int           =   ''''''.join(_lowerCAmelCase					).replace(_lowerCAmelCase			, ''' '''					).strip()
										return out_string






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										return self.vocab.get(_lowerCAmelCase			, self.vocab.get(self.unk_token					)					)






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										return self.reverse_vocab.get(_lowerCAmelCase			, self.unk_token					)






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase=None					):

										"""simple docstring"""




										if token_ids_a is None:
																return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
										__SCREAMING_SNAKE_CASE:				Tuple           =   [self.cls_token_id]
										__SCREAMING_SNAKE_CASE:				int           =   [self.sep_token_id]
										return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase=None					):

										"""simple docstring"""




										if offset_mapping_a is None:
																return [(0, 0)] + offset_mapping_a + [(0, 0)]

										return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)]






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase=None			, _lowerCAmelCase=False					):

										"""simple docstring"""




										if already_has_special_tokens:
																if token_ids_a is not None:
																						raise ValueError(
																						    '''You should not supply a second sequence if the provided sequence of '''
																						    '''ids is already formatted with special tokens for the model.'''					)
																return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a]

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






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase = None					):

										"""simple docstring"""




										if token_ids_a is None:
																# [CLS] X [SEP]
																return (len(_lowerCAmelCase					) + 2) * [0]

										# [CLS] A [SEP] [SEP] B [SEP]
										return [0] * (len(_lowerCAmelCase					) + 1) + [1] * (len(_lowerCAmelCase					) + 3)






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										if "\u4e00" <= char <= "\u9fff":
																return True
										return False






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										if ("a" <= char <= "z") or ("A" <= char <= "Z"):
																return True
										return False






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										if char in ",;:.?!~，；：。？！《》【】":
																return True
										return False






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										if char == " " or char == "\t" or char == "\n" or char == "\r":
																return True
										if len(_lowerCAmelCase					) == 1:
																__SCREAMING_SNAKE_CASE:				Optional[Any]           =   unicodedata.category(_lowerCAmelCase					)
																if cat == "Zs":
																						return True
										return False






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   {}
										with io.open(_lowerCAmelCase			, '''r'''			, encoding='''utf-8'''					) as f:
																for index, line in enumerate(_lowerCAmelCase					):
																						__SCREAMING_SNAKE_CASE:				int           =   line.rstrip('''\n'''					)
																						__SCREAMING_SNAKE_CASE:				str           =   int(_lowerCAmelCase					)

										return token_to_idx






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase = None					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Tuple           =   0
										if os.path.isdir(_lowerCAmelCase					):
																__SCREAMING_SNAKE_CASE:				int           =   os.path.join(
																    _lowerCAmelCase			, (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']					)
										else:
																__SCREAMING_SNAKE_CASE:				List[Any]           =   (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory
										with open(_lowerCAmelCase			, '''w'''			, encoding='''utf-8'''					) as writer:
																for token, token_index in sorted(self.vocab.items()			, key=lambda _lowerCAmelCase					: kv[1]					):
																						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!'''					)
																												__SCREAMING_SNAKE_CASE:				Tuple           =   token_index
																						writer.write(token + '''\n'''					)
																						index += 1

										__SCREAMING_SNAKE_CASE:				Any           =   os.path.join(_lowerCAmelCase			, '''sentencepiece.bpe.model'''					)
										with open(_lowerCAmelCase			, '''wb'''					) as fi:
																__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.sp_model.serialized_model_proto()
																fi.write(_lowerCAmelCase					)

										return (vocab_file,)










from __future__ import annotations






def 		lowerCAmelCase  (	UpperCamelCase__   :   list[int]       ,			UpperCamelCase__   :   list[int]       ,			UpperCamelCase__   :   list[int]       ,			UpperCamelCase__   :   list[list[str]]       ,			UpperCamelCase__   :   int       ,			)      ->    None:

						"""simple docstring"""





						__SCREAMING_SNAKE_CASE:				Any           =   len(UpperCamelCase__					)

						# If row is equal to the size of the board it means there are a queen in each row in
						# the current board (possible_board)
						if row == n:
												# We convert the variable possible_board that looks like this: [1, 3, 0, 2] to
												# this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . ']
												boards.append(['''. ''' * i + '''Q ''' + '''. ''' * (n - 1 - i) for i in possible_board]					)
												return

						# We iterate each column in the row to find all possible results in each row
						for col in range(UpperCamelCase__					):
												# We apply that we learned previously. First we check that in the current board
												# (possible_board) there are not other same value because if there is it means
												# that there are a collision in vertical. Then we apply the two formulas we
												# learned before:
												#
												# 45º: y - x = b or 45: row - col = b
												# 135º: y + x = b or row + col = b.
												#
												# And we verify if the results of this two formulas not exist in their variables
												# respectively.  (diagonal_right_collisions, diagonal_left_collisions)
												#
												# If any or these are True it means there is a collision so we continue to the
												# next value in the for loop.
												if (
												    col in possible_board
												    or row - col in diagonal_right_collisions
												    or row + col in diagonal_left_collisions
												):
																		continue

												# If it is False we call dfs function again and we update the inputs
												depth_first_search(
												    [*possible_board, col]       ,			[*diagonal_right_collisions, row - col]       ,			[*diagonal_left_collisions, row + col]       ,			UpperCamelCase__       ,			UpperCamelCase__       ,			)






def 		lowerCAmelCase  (	UpperCamelCase__   :   int					)      ->    None:

						"""simple docstring"""





						__SCREAMING_SNAKE_CASE:				list[list[str]]           =   []
						depth_first_search([]       ,			[]       ,			[]       ,			UpperCamelCase__       ,			UpperCamelCase__					)

						# Print all the boards
						for board in boards:
												for column in board:
																		print(UpperCamelCase__					)
												print(''''''					)

						print(len(UpperCamelCase__					)       ,			'''solutions were found.'''					)


if __name__ == "__main__":
							import doctest

							doctest.testmod()
							n_queens_solution(4)



