

import inspect
import json
import os
import tempfile
import unittest

from transformers.models.speech_to_text_a import SpeechaTextaTokenizer
from transformers.models.speech_to_text_a.tokenization_speech_to_text_a import VOCAB_FILES_NAMES

from ...test_tokenization_common import TokenizerTesterMixin





class      _lowerCAmelCase (     lowerCamelCase_ ,				unittest.TestCase	):
				_lowercase      		=SpeechaTextaTokenizer
				_lowercase      		=False




				def 					__a						( self					)    ->				List[str]:
							super().setUp()

							lowerCAmelCase_      =     "<s> <pad> </s> <unk> here@@ a couple of@@ words for the he@@ re@@ vocab".split(" "					)
							lowerCAmelCase_      =     ["he re</w> 123", "here a 1456"]
							lowerCAmelCase_      =     dict(zip(_UpperCamelCase    ,    range(len(_UpperCamelCase					)					)					)					)

							lowerCAmelCase_      =     {"pad_token": "<pad>", "unk_token": "<unk>", "bos_token": "<s>", "eos_token": "</s>"}

							lowerCAmelCase_      =     tempfile.mkdtemp()
							lowerCAmelCase_      =     os.path.join(self.tmpdirname    ,    VOCAB_FILES_NAMES["vocab_file"]					)
							lowerCAmelCase_      =     os.path.join(self.tmpdirname    ,    VOCAB_FILES_NAMES["merges_file"]					)

							with open(self.vocab_file    ,    "w"    ,    encoding="utf-8"					) as fp:
										fp.write(json.dumps(_UpperCamelCase					) + "\n"					)

							with open(self.merges_file    ,    "w"					) as fp:
										fp.write("\n".join(_UpperCamelCase					)					)




				def 					__a						( self					)    ->				int:
							lowerCAmelCase_      =     list(self.get_tokenizer().get_vocab().keys()					)

							self.assertEqual(vocab_keys[0]    ,    "<s>"					)
							self.assertEqual(vocab_keys[1]    ,    "<pad>"					)
							self.assertEqual(vocab_keys[-1]    ,    "vocab"					)
							self.assertEqual(len(_UpperCamelCase					)    ,    14					)




				def 					__a						( self					)    ->				int:
							self.assertEqual(self.get_tokenizer().vocab_size    ,    14					)




				def 					__a						( self					)    ->				int:
							lowerCAmelCase_      =     SpeechaTextaTokenizer.from_pretrained(self.tmpdirname					)

							# make sure @@ is correctly concatenated
							lowerCAmelCase_      =     [4, 6, 8, 7, 10]  # ["here@@", "couple", "words", "of@@", "the"]
							lowerCAmelCase_      =     tokenizer.decode(_UpperCamelCase					)

							self.assertTrue(output_string == "herecouple words ofthe"					)




				def 					__a						( self					)    ->				Optional[Any]:
							lowerCAmelCase_      =     SpeechaTextaTokenizer.from_pretrained(self.tmpdirname					)

							with tempfile.TemporaryDirectory() as tmp_dirname:
										tokenizer.save_pretrained(_UpperCamelCase					)
										os.remove(os.path.join(_UpperCamelCase    ,    "merges.txt"					)					)

										# load tokenizer without merges file should not throw an error
										lowerCAmelCase_      =     SpeechaTextaTokenizer.from_pretrained(_UpperCamelCase					)

							with tempfile.TemporaryDirectory() as tmp_dirname:
										# save tokenizer and load again
										tokenizer.save_pretrained(_UpperCamelCase					)
										lowerCAmelCase_      =     SpeechaTextaTokenizer.from_pretrained(_UpperCamelCase					)

							self.assertIsNotNone(_UpperCamelCase					)




				def 					__a						( self					)    ->				List[str]:







							if not self.test_slow_tokenizer:
										return

							lowerCAmelCase_      =     inspect.signature(self.tokenizer_class.__init__					)
							lowerCAmelCase_      =     self.get_tokenizer()

							for parameter_name, parameter in signature.parameters.items():
										if parameter.default != inspect.Parameter.empty and parameter_name != "merges_file":
													self.assertIn(_UpperCamelCase    ,    tokenizer.init_kwargs					)





from __future__ import annotations






def        lowerCamelCase__    (    __lowerCAmelCase  :  int   ,							__lowerCAmelCase  :  int   ,							__lowerCAmelCase  :  int    ):




			"""simple docstring"""


			assert n > 1 and a > 0 and greatest_common_divisor(__lowerCAmelCase   ,							__lowerCAmelCase    ) == 1
			((lowerCAmelCase_)     ,  (lowerCAmelCase_)     ,  (lowerCAmelCase_))      =     extended_gcd(__lowerCAmelCase   ,							__lowerCAmelCase    )  # Implemented below
			lowerCAmelCase_      =     (b * s) % n
			return x






def        lowerCamelCase__    (    __lowerCAmelCase  :  int   ,							__lowerCAmelCase  :  int    ):




			"""simple docstring"""


			((lowerCAmelCase_)     ,  (lowerCAmelCase_))      =     extended_euclid(__lowerCAmelCase   ,							__lowerCAmelCase    )  # Implemented below
			if b < 0:
						lowerCAmelCase_      =     (b % n + n) % n
			return b






def        lowerCamelCase__    (    __lowerCAmelCase  :  int   ,							__lowerCAmelCase  :  int   ,							__lowerCAmelCase  :  int    ):




			"""simple docstring"""


			lowerCAmelCase_      =     invert_modulo(__lowerCAmelCase   ,							__lowerCAmelCase    )
			lowerCAmelCase_      =     (b * s) % n
			return x






def        lowerCamelCase__    (    __lowerCAmelCase  :  int   ,							__lowerCAmelCase  :  int    ):




			"""simple docstring"""


			assert a >= 0 and b >= 0

			if b == 0:
						lowerCAmelCase_     ,  lowerCAmelCase_     ,  lowerCAmelCase_      =     a, 1, 0
			else:
						((lowerCAmelCase_)     ,  (lowerCAmelCase_)     ,  (lowerCAmelCase_))      =     extended_gcd(__lowerCAmelCase   ,							a % b    )
						lowerCAmelCase_      =     q
						lowerCAmelCase_      =     p - q * (a // b)

			assert a % d == 0 and b % d == 0
			assert d == a * x + b * y

			return (d, x, y)






def        lowerCamelCase__    (    __lowerCAmelCase  :  int   ,							__lowerCAmelCase  :  int    ):




			"""simple docstring"""


			if b == 0:
						return (1, 0)
			((lowerCAmelCase_)     ,  (lowerCAmelCase_))      =     extended_euclid(__lowerCAmelCase   ,							a % b    )
			lowerCAmelCase_      =     a // b
			return (y, x - k * y)






def        lowerCamelCase__    (    __lowerCAmelCase  :  int   ,							__lowerCAmelCase  :  int    ):




			"""simple docstring"""


			if a < b:
						lowerCAmelCase_     ,  lowerCAmelCase_      =     b, a

			while a % b != 0:
						lowerCAmelCase_     ,  lowerCAmelCase_      =     b, a % b

			return b


if __name__ == "__main__":
			from doctest import testmod

			testmod(name="modular_division", verbose=True)
			testmod(name="modular_division2", verbose=True)
			testmod(name="invert_modulo", verbose=True)
			testmod(name="extended_gcd", verbose=True)
			testmod(name="extended_euclid", verbose=True)
			testmod(name="greatest_common_divisor", verbose=True)



