
'''simple docstring'''
import json
import os
import re
import unicodedata
from typing import Optional, Tuple

from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace
from ...utils import logging


_SCREAMING_SNAKE_CASE =	logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE =	{
    'vocab_file': 'vocab.json',
    'merges_file': 'merges.txt',
}

_SCREAMING_SNAKE_CASE =	{
    'vocab_file': {'openai-gpt': 'https://huggingface.co/openai-gpt/resolve/main/vocab.json'},
    'merges_file': {'openai-gpt': 'https://huggingface.co/openai-gpt/resolve/main/merges.txt'},
}

_SCREAMING_SNAKE_CASE =	{
    'openai-gpt': 5_12,
}





def  lowerCamelCase(       SCREAMING_SNAKE_CASE_    )		->		Union[str, Any]:
			A_       		=   text.strip()
			if not text:
						return []
			A_       		=   text.split()
			return tokens
class      __UpperCAmelCase							(    A__							):



					'''simple docstring'''





					def __init__(  self    :	Tuple			,					_lowercase    :	Optional[int]=True			,					_lowercase    :	Tuple=None			,					_lowercase    :	str=True			,					_lowercase    :	Tuple=None			,					_lowercase    :	str=True			,					)						->       Tuple:
								if never_split is None:
											A_       		=   []
								A_       		=   do_lower_case
								A_       		=   set(_lowerCAmelCase)
								A_       		=   tokenize_chinese_chars
								A_       		=   strip_accents
								A_       		=   do_split_on_punc



					def 						__snake_case	(  self    :	Tuple			,					_lowercase    :	str			,					_lowercase    :	List[Any]=None)						->       Any:
								A_       		=   self.never_split.union(set(_lowerCAmelCase)) if never_split else self.never_split
								A_       		=   self._clean_text(_lowerCAmelCase)

								# This was added on November 1st, 2018 for the multilingual and Chinese
								# models. This is also applied to the English models now, but it doesn't
								# matter since the English models were not trained on any Chinese data
								# and generally don't have any Chinese data in them (there are Chinese
								# characters in the vocabulary because Wikipedia does have some Chinese
								# words in the English Wikipedia.).
								if self.tokenize_chinese_chars:
											A_       		=   self._tokenize_chinese_chars(_lowerCAmelCase)
								# prevents treating the same character with different unicode codepoints as different characters
								A_       		=   unicodedata.normalize('NFC'			,					_lowerCAmelCase)
								A_       		=   whitespace_tokenize(_lowerCAmelCase)
								A_       		=   []
								for token in orig_tokens:
											if token not in never_split:
														if self.do_lower_case:
																	A_       		=   token.lower()
																	if self.strip_accents is not False:
																				A_       		=   self._run_strip_accents(_lowerCAmelCase)
														elif self.strip_accents:
																	A_       		=   self._run_strip_accents(_lowerCAmelCase)
											split_tokens.extend(self._run_split_on_punc(_lowerCAmelCase			,					_lowerCAmelCase))

								A_       		=   whitespace_tokenize(' '.join(_lowerCAmelCase))
								return output_tokens



					def 						__snake_case	(  self    :	List[Any]			,					_lowercase    :	Union[str, Any])						->       Optional[int]:
								A_       		=   unicodedata.normalize('NFD'			,					_lowerCAmelCase)
								A_       		=   []
								for char in text:
											A_       		=   unicodedata.category(_lowerCAmelCase)
											if cat == "Mn":
														continue
											output.append(_lowerCAmelCase)
								return "".join(_lowerCAmelCase)



					def 						__snake_case	(  self    :	Dict			,					_lowercase    :	Dict			,					_lowercase    :	Any=None)						->       Dict:
								if not self.do_split_on_punc or (never_split is not None and text in never_split):
											return [text]
								A_       		=   list(_lowerCAmelCase)
								A_       		=   0
								A_       		=   True
								A_       		=   []
								while i < len(_lowerCAmelCase):
											A_       		=   chars[i]
											if _is_punctuation(_lowerCAmelCase):
														output.append([char])
														A_       		=   True
											else:
														if start_new_word:
																	output.append([])
														A_       		=   False
														output[-1].append(_lowerCAmelCase)
											i += 1

								return ["".join(_lowerCAmelCase) for x in output]



					def 						__snake_case	(  self    :	List[Any]			,					_lowercase    :	Dict)						->       Optional[Any]:
								A_       		=   []
								for char in text:
											A_       		=   ord(_lowerCAmelCase)
											if self._is_chinese_char(_lowerCAmelCase):
														output.append(' ')
														output.append(_lowerCAmelCase)
														output.append(' ')
											else:
														output.append(_lowerCAmelCase)
								return "".join(_lowerCAmelCase)



					def 						__snake_case	(  self    :	Optional[int]			,					_lowercase    :	Any)						->       Tuple:
								# This defines a "chinese character" as anything in the CJK Unicode block:
								#   https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block)
								#
								# Note that the CJK Unicode block is NOT all Japanese and Korean characters,
								# despite its name. The modern Korean Hangul alphabet is a different block,
								# as is Japanese Hiragana and Katakana. Those alphabets are used to write
								# space-separated words, so they are not treated specially and handled
								# like the all of the other languages.
								if (
								    (cp >= 0x4_e00 and cp <= 0x9_fff)
								    or (cp >= 0x3_400 and cp <= 0x4_dbf)  #
								    or (cp >= 0x20_000 and cp <= 0x2a_6df)  #
								    or (cp >= 0x2a_700 and cp <= 0x2b_73f)  #
								    or (cp >= 0x2b_740 and cp <= 0x2b_81f)  #
								    or (cp >= 0x2b_820 and cp <= 0x2c_eaf)  #
								    or (cp >= 0xf_900 and cp <= 0xf_aff)
								    or (cp >= 0x2f_800 and cp <= 0x2f_a1f)  #
								):  #
											return True

								return False



					def 						__snake_case	(  self    :	Dict			,					_lowercase    :	Optional[Any])						->       Optional[int]:
								A_       		=   []
								for char in text:
											A_       		=   ord(_lowerCAmelCase)
											if cp == 0 or cp == 0xf_ffd or _is_control(_lowerCAmelCase):
														continue
											if _is_whitespace(_lowerCAmelCase):
														output.append(' ')
											else:
														output.append(_lowerCAmelCase)
								return "".join(_lowerCAmelCase)





def  lowerCamelCase(       SCREAMING_SNAKE_CASE_    )		->		str:
			A_       		=   set()
			A_       		=   word[0]
			for char in word[1:]:
						pairs.add((prev_char, char)    )
						A_       		=   char
			return pairs





def  lowerCamelCase(       SCREAMING_SNAKE_CASE_    )		->		List[str]:
			A_       		=   text.replace('—'					,'-'    )
			A_       		=   text.replace('–'					,'-'    )
			A_       		=   text.replace('―'					,'-'    )
			A_       		=   text.replace('…'					,'...'    )
			A_       		=   text.replace('´'					,'\''    )
			A_       		=   re.sub(R'(-+|~+|!+|\"+|;+|\?+|\++|,+|\)+|\(+|\\+|\/+|\*+|\[+|\]+|}+|{+|\|+|_+)'					,R' \1 '					,SCREAMING_SNAKE_CASE_    )
			A_       		=   re.sub(R'\s*\n\s*'					,' \n '					,SCREAMING_SNAKE_CASE_    )
			A_       		=   re.sub(R'[^\S\n]+'					,' '					,SCREAMING_SNAKE_CASE_    )
			return text.strip()






class      __UpperCAmelCase							(    A__							):



					'''simple docstring'''





					_UpperCamelCase  	=		VOCAB_FILES_NAMES
					_UpperCamelCase  	=		PRETRAINED_VOCAB_FILES_MAP
					_UpperCamelCase  	=		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					_UpperCamelCase  	=		["""input_ids""", """attention_mask"""]



					def __init__(  self    :	Optional[Any]			,					_lowercase    :	Any			,					_lowercase    :	Tuple			,					_lowercase    :	Tuple="<unk>"			,					**_lowercase    :	str)						->       int:
								super().__init__(unk_token=_lowerCAmelCase			,					**_lowerCAmelCase)

								try:
											import ftfy
											from spacy.lang.en import English

											A_       		=   English()
											A_       		=   _nlp.tokenizer
											A_       		=   ftfy.fix_text
								except ImportError:
											logger.warning('ftfy or spacy is not installed using BERT BasicTokenizer instead of SpaCy & ftfy.')
											A_       		=   BasicTokenizer(do_lower_case=_lowerCAmelCase)
											A_       		=   None

								with open(_lowerCAmelCase			,					encoding='utf-8') as vocab_handle:
											A_       		=   json.load(_lowerCAmelCase)
								A_       		=   {v: k for k, v in self.encoder.items()}
								with open(_lowerCAmelCase			,					encoding='utf-8') as merges_handle:
											A_       		=   merges_handle.read().split('\n')[1:-1]
								A_       		=   [tuple(merge.split()) for merge in merges]
								A_       		=   dict(zip(_lowerCAmelCase			,					range(len(_lowerCAmelCase))))
								A_       		=   {}



					@property
					def 						__snake_case	(  self    :	Any)						->       Dict:
								return True



					@property
					def 						__snake_case	(  self    :	List[Any])						->       List[Any]:
								return len(self.encoder)



					def 						__snake_case	(  self    :	List[Any])						->       List[str]:
								return dict(self.encoder			,					**self.added_tokens_encoder)



					def 						__snake_case	(  self    :	Optional[int]			,					_lowercase    :	Tuple)						->       str:
								A_       		=   tuple(token[:-1]) + (token[-1] + '</w>',)
								if token in self.cache:
											return self.cache[token]
								A_       		=   get_pairs(_lowerCAmelCase)

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

								while True:
											A_       		=   min(_lowerCAmelCase			,					key=lambda _lowercase: self.bpe_ranks.get(_lowerCAmelCase			,					float('inf')))
											if bigram not in self.bpe_ranks:
														break
											A_		,   A_       		=   bigram
											A_       		=   []
											A_       		=   0
											while i < len(_lowerCAmelCase):
														try:
																	A_       		=   word.index(_lowerCAmelCase			,					_lowerCAmelCase)
														except ValueError:
																	new_word.extend(word[i:])
																	break
														else:
																	new_word.extend(word[i:j])
																	A_       		=   j

														if word[i] == first and i < len(_lowerCAmelCase) - 1 and word[i + 1] == second:
																	new_word.append(first + second)
																	i += 2
														else:
																	new_word.append(word[i])
																	i += 1
											A_       		=   tuple(_lowerCAmelCase)
											A_       		=   new_word
											if len(_lowerCAmelCase) == 1:
														break
											else:
														A_       		=   get_pairs(_lowerCAmelCase)
								A_       		=   ' '.join(_lowerCAmelCase)
								if word == "\n  </w>":
											A_       		=   '\n</w>'
								A_       		=   word
								return word



					def 						__snake_case	(  self    :	str			,					_lowercase    :	List[Any])						->       str:
								A_       		=   []
								if self.fix_text is None:
											# Using BERT's BasicTokenizer
											A_       		=   self.nlp.tokenize(_lowerCAmelCase)
											for token in text:
														split_tokens.extend(list(self.bpe(_lowerCAmelCase).split(' ')))
								else:
											# Using SpaCy & ftfy (original tokenization process of OpenAI GPT)
											A_       		=   self.nlp(text_standardize(self.fix_text(_lowerCAmelCase)))
											for token in text:
														split_tokens.extend(list(self.bpe(token.text.lower()).split(' ')))
								return split_tokens



					def 						__snake_case	(  self    :	str			,					_lowercase    :	Optional[int])						->       str:
								return self.encoder.get(_lowerCAmelCase			,					self.encoder.get(self.unk_token))



					def 						__snake_case	(  self    :	Optional[int]			,					_lowercase    :	str)						->       Optional[int]:
								return self.decoder.get(_lowerCAmelCase			,					self.unk_token)



					def 						__snake_case	(  self    :	List[Any]			,					_lowercase    :	List[str])						->       Dict:
								A_       		=   ''.join(_lowerCAmelCase).replace('</w>'			,					' ').strip()
								return out_string



					def 						__snake_case	(  self    :	str			,					_lowercase    :	str			,					_lowercase    :	Optional[str] = None)						->       Optional[int]:
								if not os.path.isdir(_lowerCAmelCase):
											logger.error(F'Vocabulary path ({save_directory}) should be a directory')
											return
								A_       		=   os.path.join(
								    _lowerCAmelCase			,					(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'])
								A_       		=   os.path.join(
								    _lowerCAmelCase			,					(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file'])

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

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

								return vocab_file, merge_file










'''simple docstring'''
from __future__ import annotations

import sys





def  lowerCamelCase(       SCREAMING_SNAKE_CASE_					,SCREAMING_SNAKE_CASE_    )		->		tuple[int, int]:
			# base cases
			if abs(SCREAMING_SNAKE_CASE_    ) == 1:
						return a, 0
			elif abs(SCREAMING_SNAKE_CASE_    ) == 1:
						return 0, b

			A_		,   A_       		=   a, b
			A_		,   A_       		=   1, 0
			A_		,   A_       		=   0, 1

			while remainder != 0:
						A_       		=   old_remainder // remainder
						A_		,   A_       		=   remainder, old_remainder - quotient * remainder
						A_		,   A_       		=   coeff_a, old_coeff_a - quotient * coeff_a
						A_		,   A_       		=   coeff_b, old_coeff_b - quotient * coeff_b

			# sign correction for negative numbers
			if a < 0:
						A_       		=   -old_coeff_a
			if b < 0:
						A_       		=   -old_coeff_b

			return old_coeff_a, old_coeff_b





def  lowerCamelCase(       )		->		Tuple:
			if len(sys.argv    ) < 3:
						print('2 integer arguments required'    )
						return 1
			A_       		=   int(sys.argv[1]    )
			A_       		=   int(sys.argv[2]    )
			print(extended_euclidean_algorithm(SCREAMING_SNAKE_CASE_					,SCREAMING_SNAKE_CASE_    )    )
			return 0


if __name__ == "__main__":
							raise SystemExit(main())




