



import json
import os
import re
import unicodedata
from typing import Dict, List, Optional, Tuple

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


__A       =logging.get_logger(__name__)

__A       ={
    "src_vocab_file": "vocab-src.json",
    "tgt_vocab_file": "vocab-tgt.json",
    "merges_file": "merges.txt",
}

__A       ={
    "src_vocab_file": {
        "stas/tiny-wmt19-en-de": "https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/vocab-src.json"
    },
    "tgt_vocab_file": {
        "stas/tiny-wmt19-en-de": "https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/vocab-tgt.json"
    },
    "merges_file": {"stas/tiny-wmt19-en-de": "https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/merges.txt"},
}

__A       ={"stas/tiny-wmt19-en-de": 1_0_2_4}
__A       ={
    "stas/tiny-wmt19-en-de": {
        "langs": ["en", "de"],
        "model_max_length": 1_0_2_4,
        "special_tokens_map_file": None,
        "full_tokenizer_file": None,
    }
}

def 			a		( _UpperCAmelCase				:   int		):







		'''simple docstring'''





		__UpperCAmelCase       :	List[Any]						=      set()
		__UpperCAmelCase       :	int						=      word[0]
		for char in word[1:]:
				pairs.add((prev_char, char)		)
				__UpperCAmelCase       :	str						=      char
		return pairs

def 			a		( _UpperCAmelCase				:   int		):







		'''simple docstring'''





		__UpperCAmelCase       :	List[str]						=      text.replace('''，'''					,  ''','''		)
		__UpperCAmelCase       :	Any						=      re.sub(R'''。\s*'''					,  '''. '''					,  _UpperCAmelCase		)
		__UpperCAmelCase       :	Union[str, Any]						=      text.replace('''、'''					,  ''','''		)
		__UpperCAmelCase       :	int						=      text.replace('''”'''					,  '''"'''		)
		__UpperCAmelCase       :	int						=      text.replace('''“'''					,  '''"'''		)
		__UpperCAmelCase       :	Optional[Any]						=      text.replace('''∶'''					,  ''':'''		)
		__UpperCAmelCase       :	Optional[int]						=      text.replace('''：'''					,  ''':'''		)
		__UpperCAmelCase       :	List[Any]						=      text.replace('''？'''					,  '''?'''		)
		__UpperCAmelCase       :	Optional[int]						=      text.replace('''《'''					,  '''"'''		)
		__UpperCAmelCase       :	int						=      text.replace('''》'''					,  '''"'''		)
		__UpperCAmelCase       :	Any						=      text.replace('''）'''					,  ''')'''		)
		__UpperCAmelCase       :	int						=      text.replace('''！'''					,  '''!'''		)
		__UpperCAmelCase       :	str						=      text.replace('''（'''					,  '''('''		)
		__UpperCAmelCase       :	Union[str, Any]						=      text.replace('''；'''					,  ''';'''		)
		__UpperCAmelCase       :	List[Any]						=      text.replace('''１'''					,  '''1'''		)
		__UpperCAmelCase       :	int						=      text.replace('''」'''					,  '''"'''		)
		__UpperCAmelCase       :	List[str]						=      text.replace('''「'''					,  '''"'''		)
		__UpperCAmelCase       :	Optional[int]						=      text.replace('''０'''					,  '''0'''		)
		__UpperCAmelCase       :	List[str]						=      text.replace('''３'''					,  '''3'''		)
		__UpperCAmelCase       :	Dict						=      text.replace('''２'''					,  '''2'''		)
		__UpperCAmelCase       :	Tuple						=      text.replace('''５'''					,  '''5'''		)
		__UpperCAmelCase       :	Union[str, Any]						=      text.replace('''６'''					,  '''6'''		)
		__UpperCAmelCase       :	List[Any]						=      text.replace('''９'''					,  '''9'''		)
		__UpperCAmelCase       :	List[Any]						=      text.replace('''７'''					,  '''7'''		)
		__UpperCAmelCase       :	Optional[int]						=      text.replace('''８'''					,  '''8'''		)
		__UpperCAmelCase       :	Dict						=      text.replace('''４'''					,  '''4'''		)
		__UpperCAmelCase       :	str						=      re.sub(R'''．\s*'''					,  '''. '''					,  _UpperCAmelCase		)
		__UpperCAmelCase       :	Optional[int]						=      text.replace('''～'''					,  '''~'''		)
		__UpperCAmelCase       :	Any						=      text.replace('''’'''					,  '''\''''		)
		__UpperCAmelCase       :	int						=      text.replace('''…'''					,  '''...'''		)
		__UpperCAmelCase       :	Union[str, Any]						=      text.replace('''━'''					,  '''-'''		)
		__UpperCAmelCase       :	int						=      text.replace('''〈'''					,  '''<'''		)
		__UpperCAmelCase       :	Tuple						=      text.replace('''〉'''					,  '''>'''		)
		__UpperCAmelCase       :	Any						=      text.replace('''【'''					,  '''['''		)
		__UpperCAmelCase       :	Any						=      text.replace('''】'''					,  ''']'''		)
		__UpperCAmelCase       :	List[str]						=      text.replace('''％'''					,  '''%'''		)
		return text

def 			a		( _UpperCAmelCase				:   Tuple		):







		'''simple docstring'''





		__UpperCAmelCase       :	Optional[int]						=      []
		for char in text:
				__UpperCAmelCase       :	Tuple						=      unicodedata.category(_UpperCAmelCase		)
				if cat.startswith('''C'''		):
						continue
				output.append(_UpperCAmelCase		)
		return "".join(_UpperCAmelCase		)

class 						UpperCAmelCase__     (     __UpperCamelCase  ):




			'''simple docstring'''






			UpperCamelCase     		=			VOCAB_FILES_NAMES
			UpperCamelCase     		=			PRETRAINED_VOCAB_FILES_MAP
			UpperCamelCase     		=			PRETRAINED_INIT_CONFIGURATION
			UpperCamelCase     		=			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			UpperCamelCase     		=			["""input_ids""", """attention_mask"""]




			def __init__(  self				:					Optional[int]			,			a_				:					str=None			,			a_				:					Union[str, Any]=None			,			a_				:					List[str]=None			,			a_				:					List[str]=None			,			a_				:					Optional[int]=False			,			a_				:					Dict="<unk>"			,			a_				:					str="<s>"			,			a_				:					int="</s>"			,			a_				:					str="<pad>"			,			**a_				:					Optional[Any]			,			):
					'''simple docstring'''





					super().__init__(
					    langs=a_			,			src_vocab_file=a_			,			tgt_vocab_file=a_			,			merges_file=a_			,			do_lower_case=a_			,			unk_token=a_			,			bos_token=a_			,			sep_token=a_			,			pad_token=a_			,			**a_			,			)

					try:
							import sacremoses
					except ImportError:
							raise ImportError(
							    '''You need to install sacremoses to use XLMTokenizer. '''
							    '''See https://pypi.org/project/sacremoses/ for installation.'''					)

					__UpperCAmelCase       :	Optional[Any]						=      sacremoses

					__UpperCAmelCase       :	Union[str, Any]						=      src_vocab_file
					__UpperCAmelCase       :	List[str]						=      tgt_vocab_file
					__UpperCAmelCase       :	Union[str, Any]						=      merges_file
					__UpperCAmelCase       :	Any						=      do_lower_case

					# cache of sm.MosesPunctNormalizer instance
					__UpperCAmelCase       :	Tuple						=      {}
					# cache of sm.MosesTokenizer instance
					__UpperCAmelCase       :	str						=      {}
					__UpperCAmelCase       :	Any						=      {}

					if langs and len(a_					) == 2:
							__UpperCAmelCase				,   __UpperCAmelCase       :	Any						=      langs
					else:
							raise ValueError(
							    F'arg `langs` needs to be a list of 2 langs, e.g. [\'en\', \'ru\'], but got {langs}. '
							    '''Usually that means that tokenizer can\'t find a mapping for the given model path '''
							    '''in PRETRAINED_VOCAB_FILES_MAP, and other maps of this tokenizer.'''					)

					with open(a_			,			encoding='''utf-8'''					) as src_vocab_handle:
							__UpperCAmelCase       :	str						=      json.load(a_					)
					with open(a_			,			encoding='''utf-8'''					) as tgt_vocab_handle:
							__UpperCAmelCase       :	str						=      json.load(a_					)
							__UpperCAmelCase       :	str						=      {v: k for k, v in tgt_vocab.items()}
					with open(a_			,			encoding='''utf-8'''					) as merges_handle:
							__UpperCAmelCase       :	Optional[int]						=      merges_handle.read().split('''\n'''					)[:-1]
					__UpperCAmelCase       :	Any						=      [tuple(merge.split()[:2]					) for merge in merges]
					__UpperCAmelCase       :	Optional[Any]						=      dict(zip(a_			,			range(len(a_					)					)					)					)
					__UpperCAmelCase       :	List[Any]						=      {}




			def 					snake_case__	(  self				:					List[Any]					):
					'''simple docstring'''





					return self.get_src_vocab()




			@property
			def 					snake_case__	(  self				:					str					):
					'''simple docstring'''





					return self.src_vocab_size




			def 					snake_case__	(  self				:					Optional[int]			,			a_				:					Dict			,			a_				:					List[str]					):
					'''simple docstring'''





					if lang not in self.cache_moses_punct_normalizer:
							__UpperCAmelCase       :	str						=      self.sm.MosesPunctNormalizer(lang=a_					)
							__UpperCAmelCase       :	int						=      punct_normalizer
					return self.cache_moses_punct_normalizer[lang].normalize(a_					)




			def 					snake_case__	(  self				:					Union[str, Any]			,			a_				:					Union[str, Any]			,			a_				:					Any					):
					'''simple docstring'''





					if lang not in self.cache_moses_tokenizer:
							__UpperCAmelCase       :	int						=      self.sm.MosesTokenizer(lang=a_					)
							__UpperCAmelCase       :	Union[str, Any]						=      moses_tokenizer
					return self.cache_moses_tokenizer[lang].tokenize(
					    a_			,			aggressive_dash_splits=a_			,			return_str=a_			,			escape=a_					)




			def 					snake_case__	(  self				:					Optional[Any]			,			a_				:					Tuple			,			a_				:					str					):
					'''simple docstring'''





					if lang not in self.cache_moses_detokenizer:
							__UpperCAmelCase       :	Any						=      self.sm.MosesDetokenizer(lang=a_					)
							__UpperCAmelCase       :	List[Any]						=      moses_detokenizer
					return self.cache_moses_detokenizer[lang].detokenize(a_					)




			def 					snake_case__	(  self				:					Optional[Any]			,			a_				:					List[Any]			,			a_				:					Dict					):
					'''simple docstring'''





					__UpperCAmelCase       :	Tuple						=      replace_unicode_punct(a_					)
					__UpperCAmelCase       :	List[Any]						=      self.moses_punct_norm(a_			,			a_					)
					__UpperCAmelCase       :	Any						=      remove_non_printing_char(a_					)
					return text




			@property
			def 					snake_case__	(  self				:					List[Any]					):
					'''simple docstring'''





					return len(self.encoder					)




			@property
			def 					snake_case__	(  self				:					Union[str, Any]					):
					'''simple docstring'''





					return len(self.decoder					)




			def 					snake_case__	(  self				:					str					):
					'''simple docstring'''





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




			def 					snake_case__	(  self				:					List[str]					):
					'''simple docstring'''





					return dict(self.decoder			,			**self.added_tokens_decoder					)




			def 					snake_case__	(  self				:					Any			,			a_				:					str					):
					'''simple docstring'''





					__UpperCAmelCase       :	int						=      tuple(token[:-1]					) + (token[-1] + '''</w>''',)
					if token in self.cache:
							return self.cache[token]
					__UpperCAmelCase       :	Optional[Any]						=      get_pairs(a_					)

					if not pairs:
							return token + "</w>"

					while True:
							__UpperCAmelCase       :	str						=      min(a_			,			key=lambda a_					: self.bpe_ranks.get(a_			,			float('''inf'''					)					)					)
							if bigram not in self.bpe_ranks:
									break
							__UpperCAmelCase				,   __UpperCAmelCase       :	List[str]						=      bigram
							__UpperCAmelCase       :	List[str]						=      []
							__UpperCAmelCase       :	List[str]						=      0
							while i < len(a_					):
									try:
											__UpperCAmelCase       :	Optional[Any]						=      word.index(a_			,			a_					)
									except ValueError:
											new_word.extend(word[i:]					)
											break
									else:
											new_word.extend(word[i:j]					)
											__UpperCAmelCase       :	Optional[int]						=      j

									if word[i] == first and i < len(a_					) - 1 and word[i + 1] == second:
											new_word.append(first + second					)
											i += 2
									else:
											new_word.append(word[i]					)
											i += 1
							__UpperCAmelCase       :	Union[str, Any]						=      tuple(a_					)
							__UpperCAmelCase       :	Any						=      new_word
							if len(a_					) == 1:
									break
							else:
									__UpperCAmelCase       :	List[Any]						=      get_pairs(a_					)
					__UpperCAmelCase       :	Optional[int]						=      ''' '''.join(a_					)
					if word == "\n  </w>":
							__UpperCAmelCase       :	List[Any]						=      '''\n</w>'''
					__UpperCAmelCase       :	int						=      word
					return word




			def 					snake_case__	(  self				:					Tuple			,			a_				:					Optional[int]			,			a_				:					List[Any]="en"			,			a_				:					Union[str, Any]=False					):
					'''simple docstring'''





					__UpperCAmelCase       :	List[str]						=      self.src_lang

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

					if bypass_tokenizer:
							__UpperCAmelCase       :	Optional[int]						=      text.split()
					else:
							__UpperCAmelCase       :	Union[str, Any]						=      self.moses_pipeline(a_			,			lang=a_					)
							__UpperCAmelCase       :	int						=      self.moses_tokenize(a_			,			lang=a_					)

					__UpperCAmelCase       :	List[str]						=      []
					for token in text:
							if token:
									split_tokens.extend(list(self.bpe(a_					).split(''' '''					)					)					)

					return split_tokens




			def 					snake_case__	(  self				:					Union[str, Any]			,			a_				:					List[str]					):
					'''simple docstring'''





					return self.encoder.get(a_			,			self.encoder.get(self.unk_token					)					)




			def 					snake_case__	(  self				:					Optional[int]			,			a_				:					List[Any]					):
					'''simple docstring'''





					return self.decoder.get(a_			,			self.unk_token					)




			def 					snake_case__	(  self				:					List[Any]			,			a_				:					Tuple					):
					'''simple docstring'''





					__UpperCAmelCase       :	List[str]						=      [t.replace(''' '''			,			''''''					).replace('''</w>'''			,			''' '''					) for t in tokens]
					__UpperCAmelCase       :	Any						=      ''''''.join(a_					).split()
					# detokenize
					__UpperCAmelCase       :	Union[str, Any]						=      self.moses_detokenize(a_			,			self.tgt_lang					)
					return text




			def 					snake_case__	(  self				:					Optional[Any]			,			a_				:					List[int]			,			a_				:					Optional[List[int]] = None					):
					'''simple docstring'''





					__UpperCAmelCase       :	Optional[int]						=      [self.sep_token_id]

					# no bos used in fairseq
					if token_ids_a is None:
							return token_ids_a + sep
					return token_ids_a + sep + token_ids_a + sep




			def 					snake_case__	(  self				:					Any			,			a_				:					List[int]			,			a_				:					Optional[List[int]] = None			,			a_				:					bool = False					):
					'''simple docstring'''





					if already_has_special_tokens:
							return super().get_special_tokens_mask(
							    token_ids_a=a_			,			token_ids_a=a_			,			already_has_special_tokens=a_					)
					# no bos used in fairseq
					if token_ids_a is not None:
							return ([0] * len(a_					)) + [1] + ([0] * len(a_					)) + [1]
					return ([0] * len(a_					)) + [1]




			def 					snake_case__	(  self				:					str			,			a_				:					List[int]			,			a_				:					Optional[List[int]] = None					):
					'''simple docstring'''





					__UpperCAmelCase       :	Any						=      [self.sep_token_id]

					# no bos used in fairseq
					if token_ids_a is None:
							return len(token_ids_a + sep					) * [0]
					return len(token_ids_a + sep					) * [0] + len(token_ids_a + sep					) * [1]




			def 					snake_case__	(  self				:					Union[str, Any]			,			a_				:					str			,			a_				:					Optional[str] = None					):
					'''simple docstring'''





					if not os.path.isdir(a_					):
							logger.error(F'Vocabulary path ({save_directory}) should be a directory'					)
							return

					__UpperCAmelCase       :	List[Any]						=      os.path.join(
					    a_			,			(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''src_vocab_file''']					)
					__UpperCAmelCase       :	List[str]						=      os.path.join(
					    a_			,			(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''tgt_vocab_file''']					)
					__UpperCAmelCase       :	Any						=      os.path.join(
					    a_			,			(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file''']					)

					with open(a_			,			'''w'''			,			encoding='''utf-8'''					) as f:
							f.write(json.dumps(self.encoder			,			indent=2			,			sort_keys=a_			,			ensure_ascii=a_					) + '''\n'''					)

					with open(a_			,			'''w'''			,			encoding='''utf-8'''					) as f:
							__UpperCAmelCase       :	List[Any]						=      {v: k for k, v in self.decoder.items()}
							f.write(json.dumps(a_			,			indent=2			,			sort_keys=a_			,			ensure_ascii=a_					) + '''\n'''					)

					__UpperCAmelCase       :	int						=      0
					with open(a_			,			'''w'''			,			encoding='''utf-8'''					) as writer:
							for bpe_tokens, token_index in sorted(self.bpe_ranks.items()			,			key=lambda a_					: kv[1]					):
									if index != token_index:
											logger.warning(
											    F'Saving vocabulary to {merges_file}: BPE merge indices are not consecutive.'
											    ''' Please check that the tokenizer is not corrupted!'''					)
											__UpperCAmelCase       :	List[str]						=      token_index
									writer.write(''' '''.join(a_					) + '''\n'''					)
									index += 1

					return src_vocab_file, tgt_vocab_file, merges_file




			def __getstate__(  self				:					Optional[Any]					):
					'''simple docstring'''





					__UpperCAmelCase       :	Union[str, Any]						=      self.__dict__.copy()
					__UpperCAmelCase       :	str						=      None
					return state




			def __setstate__(  self				:					Tuple			,			a_				:					int					):
					'''simple docstring'''





					__UpperCAmelCase       :	Optional[Any]						=      d

					try:
							import sacremoses
					except ImportError:
							raise ImportError(
							    '''You need to install sacremoses to use XLMTokenizer. '''
							    '''See https://pypi.org/project/sacremoses/ for installation.'''					)

					__UpperCAmelCase       :	Tuple						=      sacremoses










import numpy as np

def 			a		( _UpperCAmelCase				:   str					,  _UpperCAmelCase				:   Any="utf-8"					,  _UpperCAmelCase				:   List[Any]="surrogatepass"		):







		'''simple docstring'''





		__UpperCAmelCase       :	Union[str, Any]						=      bin(int.from_bytes(text.encode(_UpperCAmelCase					,  _UpperCAmelCase		)					,  '''big'''		)		)[2:]
		return bits.zfill(8 * ((len(_UpperCAmelCase		) + 7) // 8)		)

def 			a		( _UpperCAmelCase				:   Dict					,  _UpperCAmelCase				:   Any="utf-8"					,  _UpperCAmelCase				:   str="surrogatepass"		):







		'''simple docstring'''





		__UpperCAmelCase       :	List[str]						=      int(_UpperCAmelCase					,  2		)
		return n.to_bytes((n.bit_length() + 7) // 8					,  '''big'''		).decode(_UpperCAmelCase					,  _UpperCAmelCase		) or "\0"

def 			a		( _UpperCAmelCase				:   List[Any]					,  _UpperCAmelCase				:   List[str]		):







		'''simple docstring'''





		if size_par + len(_UpperCAmelCase		) <= 2**size_par - (len(_UpperCAmelCase		) - 1):
				raise ValueError('''size of parity don\'t match with size of data'''		)

		__UpperCAmelCase       :	str						=      []
		__UpperCAmelCase       :	Any						=      []
		__UpperCAmelCase       :	Any						=      [bin(_UpperCAmelCase		)[2:] for x in range(1					,  size_par + len(_UpperCAmelCase		) + 1		)]

		# sorted information data for the size of the output data
		__UpperCAmelCase       :	Optional[Any]						=      []
		# data position template + parity
		__UpperCAmelCase       :	Any						=      []
		# parity bit counter
		__UpperCAmelCase       :	Tuple						=      0
		# counter position of data bits
		__UpperCAmelCase       :	Optional[int]						=      0

		for x in range(1					,  size_par + len(_UpperCAmelCase		) + 1		):
				# Performs a template of bit positions - who should be given,
				# and who should be parity
				if qtd_bp < size_par:
						if (np.log(_UpperCAmelCase		) / np.log(2		)).is_integer():
								data_out_gab.append('''P'''		)
								__UpperCAmelCase       :	Union[str, Any]						=      qtd_bp + 1
						else:
								data_out_gab.append('''D'''		)
				else:
						data_out_gab.append('''D'''		)

				# Sorts the data to the new output size
				if data_out_gab[-1] == "D":
						data_ord.append(data[cont_data]		)
						cont_data += 1
				else:
						data_ord.append(_UpperCAmelCase		)

    # Calculates parity
		__UpperCAmelCase       :	Union[str, Any]						=      0  # parity bit counter
		for bp in range(1					,  size_par + 1		):
				# Bit counter one for a given parity
				__UpperCAmelCase       :	List[str]						=      0
				# counter to control the loop reading
				__UpperCAmelCase       :	str						=      0
				for x in data_ord:
						if x is not None:
								try:
										__UpperCAmelCase       :	Tuple						=      (bin_pos[cont_loop])[-1 * (bp)]
								except IndexError:
										__UpperCAmelCase       :	List[str]						=      '''0'''
								if aux == "1" and x == "1":
										cont_bo += 1
						cont_loop += 1
				parity.append(cont_bo % 2		)

				qtd_bp += 1

		# Mount the message
		__UpperCAmelCase       :	int						=      0  # parity bit counter
		for x in range(0					,  size_par + len(_UpperCAmelCase		)		):
				if data_ord[x] is None:
						data_out.append(str(parity[cont_bp]		)		)
						cont_bp += 1
				else:
						data_out.append(data_ord[x]		)

		return data_out

def 			a		( _UpperCAmelCase				:   List[str]					,  _UpperCAmelCase				:   str		):







		'''simple docstring'''





		__UpperCAmelCase       :	str						=      []
		# Parity bit counter
		__UpperCAmelCase       :	List[str]						=      0
		# Counter p data bit reading
		__UpperCAmelCase       :	int						=      0
		# list of parity received
		__UpperCAmelCase       :	int						=      []
		__UpperCAmelCase       :	List[Any]						=      []

		for x in range(1					,  len(_UpperCAmelCase		) + 1		):
				# Performs a template of bit positions - who should be given,
				#  and who should be parity
				if qtd_bp < size_par and (np.log(_UpperCAmelCase		) / np.log(2		)).is_integer():
						data_out_gab.append('''P'''		)
						__UpperCAmelCase       :	Union[str, Any]						=      qtd_bp + 1
				else:
						data_out_gab.append('''D'''		)

				# Sorts the data to the new output size
				if data_out_gab[-1] == "D":
						data_output.append(data[cont_data]		)
				else:
						parity_received.append(data[cont_data]		)
				cont_data += 1

		# -----------calculates the parity with the data
		__UpperCAmelCase       :	Optional[int]						=      []
		__UpperCAmelCase       :	Dict						=      []
		__UpperCAmelCase       :	Union[str, Any]						=      [bin(_UpperCAmelCase		)[2:] for x in range(1					,  size_par + len(_UpperCAmelCase		) + 1		)]

		#  sorted information data for the size of the output data
		__UpperCAmelCase       :	str						=      []
		# Data position feedback + parity
		__UpperCAmelCase       :	Any						=      []
		# Parity bit counter
		__UpperCAmelCase       :	int						=      0
		# Counter p data bit reading
		__UpperCAmelCase       :	List[Any]						=      0

		for x in range(1					,  size_par + len(_UpperCAmelCase		) + 1		):
				# Performs a template position of bits - who should be given,
				# and who should be parity
				if qtd_bp < size_par and (np.log(_UpperCAmelCase		) / np.log(2		)).is_integer():
						data_out_gab.append('''P'''		)
						__UpperCAmelCase       :	Dict						=      qtd_bp + 1
				else:
						data_out_gab.append('''D'''		)

				# Sorts the data to the new output size
				if data_out_gab[-1] == "D":
						data_ord.append(data_output[cont_data]		)
						cont_data += 1
				else:
						data_ord.append(_UpperCAmelCase		)

    # Calculates parity
		__UpperCAmelCase       :	Union[str, Any]						=      0  # parity bit counter
		for bp in range(1					,  size_par + 1		):
				# Bit counter one for a certain parity
				__UpperCAmelCase       :	Union[str, Any]						=      0
				# Counter to control loop reading
				__UpperCAmelCase       :	Optional[Any]						=      0
				for x in data_ord:
						if x is not None:
								try:
										__UpperCAmelCase       :	int						=      (bin_pos[cont_loop])[-1 * (bp)]
								except IndexError:
										__UpperCAmelCase       :	int						=      '''0'''
								if aux == "1" and x == "1":
										cont_bo += 1
						cont_loop += 1
				parity.append(str(cont_bo % 2		)		)

				qtd_bp += 1

		# Mount the message
		__UpperCAmelCase       :	Any						=      0  # Parity bit counter
		for x in range(0					,  size_par + len(_UpperCAmelCase		)		):
				if data_ord[x] is None:
						data_out.append(str(parity[cont_bp]		)		)
						cont_bp += 1
				else:
						data_out.append(data_ord[x]		)

		__UpperCAmelCase       :	Optional[Any]						=      parity_received == parity
		return data_output, ack





