



"""simple docstring"""




import shutil
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from transformers import SpeechaTextTokenizer, is_speech_available
from transformers.models.speech_to_text.tokenization_speech_to_text import VOCAB_FILES_NAMES, save_json
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, require_torchaudio
from transformers.utils import FEATURE_EXTRACTOR_NAME

from .test_feature_extraction_speech_to_text import floats_list


if is_speech_available():
     from transformers import SpeechaTextFeatureExtractor, SpeechaTextProcessor


_UpperCamelCase:					str     				=	get_tests_dir('fixtures/test_sentencepiece.model')


@require_torch
@require_torchaudio
@require_sentencepiece
class 	a__		(    unittest.TestCase  ):


   def 							lowercase					(				self   :				List[str]      )     ->							Any:
         lowercase	:   int											= tempfile.mkdtemp()

         lowercase	:   Tuple											= ['<s>', '<pad>', '</s>', '<unk>', '▁This', '▁is', '▁a', '▁t', 'est']
         lowercase	:   int											= dict(zip(_lowerCAmelCase,  range(len(_lowerCAmelCase      )      )      )      )
         lowercase	:   Union[str, Any]											= Path(self.tmpdirname      )
         save_json(_lowerCAmelCase,  save_dir / VOCAB_FILES_NAMES['vocab_file']      )
         if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists():
               copyfile(_lowerCAmelCase,  save_dir / VOCAB_FILES_NAMES['spm_file']      )

         lowercase	:   Union[str, Any]											= SpeechaTextTokenizer.from_pretrained(self.tmpdirname      )
         tokenizer.save_pretrained(self.tmpdirname      )

         lowercase	:   Dict											= {
             'feature_size': 24,
             'num_mel_bins': 24,
             'padding_value': 0.0,
             'sampling_rate': 16000,
             'return_attention_mask': False,
             'do_normalize': True,
         }
         save_json(_lowerCAmelCase,  save_dir / FEATURE_EXTRACTOR_NAME      )


   def 							lowercase					(				self   :				Union[str, Any],  **lowerCAmelCase   :				Optional[Any]      )     ->							List[str]:
         return SpeechaTextTokenizer.from_pretrained(self.tmpdirname,  **_lowerCAmelCase      )


   def 							lowercase					(				self   :				Optional[Any],  **lowerCAmelCase   :				Dict      )     ->							Tuple:
         return SpeechaTextFeatureExtractor.from_pretrained(self.tmpdirname,  **_lowerCAmelCase      )


   def 							lowercase					(				self   :				Optional[Any]      )     ->							int:
         shutil.rmtree(self.tmpdirname      )


   def 							lowercase					(				self   :				Optional[int]      )     ->							Any:
         lowercase	:   int											= self.get_tokenizer()
         lowercase	:   Dict											= self.get_feature_extractor()

         lowercase	:   List[str]											= SpeechaTextProcessor(tokenizer=_lowerCAmelCase,  feature_extractor=_lowerCAmelCase      )

         processor.save_pretrained(self.tmpdirname      )
         lowercase	:   int											= SpeechaTextProcessor.from_pretrained(self.tmpdirname      )

         self.assertEqual(processor.tokenizer.get_vocab(),  tokenizer.get_vocab()      )
         self.assertIsInstance(processor.tokenizer,  _lowerCAmelCase      )

         self.assertEqual(processor.feature_extractor.to_json_string(),  feature_extractor.to_json_string()      )
         self.assertIsInstance(processor.feature_extractor,  _lowerCAmelCase      )


   def 							lowercase					(				self   :				Optional[int]      )     ->							int:
         lowercase	:   Optional[int]											= SpeechaTextProcessor(
             tokenizer=self.get_tokenizer(),  feature_extractor=self.get_feature_extractor()      )
         processor.save_pretrained(self.tmpdirname      )

         lowercase	:   int											= self.get_tokenizer(bos_token='(BOS)',  eos_token='(EOS)'      )
         lowercase	:   Union[str, Any]											= self.get_feature_extractor(do_normalize=_lowerCAmelCase,  padding_value=1.0      )

         lowercase	:   int											= SpeechaTextProcessor.from_pretrained(
             self.tmpdirname,  bos_token='(BOS)',  eos_token='(EOS)',  do_normalize=_lowerCAmelCase,  padding_value=1.0      )

         self.assertEqual(processor.tokenizer.get_vocab(),  tokenizer_add_kwargs.get_vocab()      )
         self.assertIsInstance(processor.tokenizer,  _lowerCAmelCase      )

         self.assertEqual(processor.feature_extractor.to_json_string(),  feature_extractor_add_kwargs.to_json_string()      )
         self.assertIsInstance(processor.feature_extractor,  _lowerCAmelCase      )


   def 							lowercase					(				self   :				Dict      )     ->							Union[str, Any]:
         lowercase	:   Union[str, Any]											= self.get_feature_extractor()
         lowercase	:   Dict											= self.get_tokenizer()

         lowercase	:   Optional[int]											= SpeechaTextProcessor(tokenizer=_lowerCAmelCase,  feature_extractor=_lowerCAmelCase      )

         lowercase	:   List[Any]											= floats_list((3, 1000)      )

         lowercase	:   Optional[Any]											= feature_extractor(_lowerCAmelCase,  return_tensors='np'      )
         lowercase	:   str											= processor(_lowerCAmelCase,  return_tensors='np'      )

         for key in input_feat_extract.keys():
               self.assertAlmostEqual(input_feat_extract[key].sum(),  input_processor[key].sum(),  delta=1e-2      )


   def 							lowercase					(				self   :				Dict      )     ->							Any:
         lowercase	:   Optional[Any]											= self.get_feature_extractor()
         lowercase	:   int											= self.get_tokenizer()

         lowercase	:   Union[str, Any]											= SpeechaTextProcessor(tokenizer=_lowerCAmelCase,  feature_extractor=_lowerCAmelCase      )

         lowercase	:   Optional[int]											= 'This is a test string'

         lowercase	:   Optional[int]											= processor(text=_lowerCAmelCase      )

         lowercase	:   Optional[int]											= tokenizer(_lowerCAmelCase      )

         for key in encoded_tok.keys():
               self.assertListEqual(encoded_tok[key],  encoded_processor[key]      )


   def 							lowercase					(				self   :				Tuple      )     ->							Tuple:
         lowercase	:   str											= self.get_feature_extractor()
         lowercase	:   List[str]											= self.get_tokenizer()

         lowercase	:   Tuple											= SpeechaTextProcessor(tokenizer=_lowerCAmelCase,  feature_extractor=_lowerCAmelCase      )

         lowercase	:   Optional[int]											= [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

         lowercase	:   Optional[int]											= processor.batch_decode(_lowerCAmelCase      )
         lowercase	:   Tuple											= tokenizer.batch_decode(_lowerCAmelCase      )

         self.assertListEqual(_lowerCAmelCase,  _lowerCAmelCase      )






   def 							lowercase					(				self   :				Any      )     ->							Union[str, Any]:
         lowercase	:   int											= self.get_feature_extractor()
         lowercase	:   List[Any]											= self.get_tokenizer()

         lowercase	:   Any											= SpeechaTextProcessor(tokenizer=_lowerCAmelCase,  feature_extractor=_lowerCAmelCase      )

         self.assertListEqual(
             processor.model_input_names,  feature_extractor.model_input_names,  msg='`processor` and `feature_extractor` model input names do not match',  )


from __future__ import annotations



def 	_a  (					__SCREAMING_SNAKE_CASE					:  list		,				__SCREAMING_SNAKE_CASE					:  list    ):






  """simple docstring"""




  _lowerCAmelCase						=		[None] * (len(__SCREAMING_SNAKE_CASE    ) + len(__SCREAMING_SNAKE_CASE    ))

  _lowerCAmelCase						=		0  # pointer to current index for left Half
  _lowerCAmelCase						=		0  # pointer to current index for the right Half
  _lowerCAmelCase						=		0  # pointer to current index for the sorted array Half

  while pointera < len(__SCREAMING_SNAKE_CASE    ) and pointera < len(__SCREAMING_SNAKE_CASE    ):
    if left_half[pointera] < right_half[pointera]:
      _lowerCAmelCase						=		left_half[pointera]
      pointera += 1
      index += 1
    else:
      _lowerCAmelCase						=		right_half[pointera]
      pointera += 1
      index += 1
  while pointera < len(__SCREAMING_SNAKE_CASE    ):
    _lowerCAmelCase						=		left_half[pointera]
    pointera += 1
    index += 1

  while pointera < len(__SCREAMING_SNAKE_CASE    ):
    _lowerCAmelCase						=		right_half[pointera]
    pointera += 1
    index += 1

  return sorted_array



def 	_a  (					__SCREAMING_SNAKE_CASE					:  list    ):






  """simple docstring"""




  if len(__SCREAMING_SNAKE_CASE    ) <= 1:
    return array
  # the actual formula to calculate the middle element = left + (right - left) // 2
  # this avoids integer overflow in case of large N
  _lowerCAmelCase						=		0 + (len(__SCREAMING_SNAKE_CASE    ) - 0) // 2

  # Split the array into halves till the array length becomes equal to One
  # merge the arrays of single length returned by mergeSort function and
  # pass them into the merge arrays function which merges the array
  _lowerCAmelCase						=		array[:middle]
  _lowerCAmelCase						=		array[middle:]

  return merge(merge_sort(__SCREAMING_SNAKE_CASE    )		,				merge_sort(__SCREAMING_SNAKE_CASE    )    )


if __name__ == "__main__":
       import doctest

       doctest.testmod()
