


"""simple docstring"""
import unittest

from cer import CER


SCREAMING_SNAKE_CASE					:       Optional[Any]							   =							CER()



class      _UpperCAmelCase					(				unittest.TestCase							):






		'''simple docstring'''

		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		List[str]         =							['''White House''']
			__snake_case      :		str         =							['''white house''']
			# S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
			__snake_case      :		Union[str, Any]         =							cer.compute(predictions=a_							,			references=a_   )
			self.assertTrue(abs(char_error_rate - 0.18_1818_1818   ) < 1E-6   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		str         =							['''were wolf''']
			__snake_case      :		Tuple         =							['''werewolf''']
			# S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
			__snake_case      :		Optional[Any]         =							cer.compute(predictions=a_							,			references=a_   )
			self.assertTrue(abs(char_error_rate - 0.111_1111   ) < 1E-6   )

			__snake_case      :		Optional[int]         =							['''werewolf''']
			__snake_case      :		Union[str, Any]         =							['''weae     wolf''']
			# S = 1, D = 1, I = 0, N = 8, CER = 0.25
			__snake_case      :		Any         =							cer.compute(predictions=a_							,			references=a_   )
			self.assertTrue(abs(char_error_rate - 0.25   ) < 1E-6   )

			# consecutive whitespaces case 1
			__snake_case      :		Optional[int]         =							['''were wolf''']
			__snake_case      :		Optional[int]         =							['''were               wolf''']
			# S = 0, D = 0, I = 0, N = 9, CER = 0
			__snake_case      :		Union[str, Any]         =							cer.compute(predictions=a_							,			references=a_   )
			self.assertTrue(abs(char_error_rate - 0.0   ) < 1E-6   )

			# consecutive whitespaces case 2
			__snake_case      :		int         =							['''were   wolf''']
			__snake_case      :		str         =							['''were               wolf''']
			# S = 0, D = 0, I = 0, N = 9, CER = 0
			__snake_case      :		List[Any]         =							cer.compute(predictions=a_							,			references=a_   )
			self.assertTrue(abs(char_error_rate - 0.0   ) < 1E-6   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Optional[int]         =							['''werewolf''']
			__snake_case      :		Union[str, Any]         =							['''weaewolf''']
			# S = 1, D = 0, I = 0, N = 8, CER = 0.125
			__snake_case      :		Optional[Any]         =							cer.compute(predictions=a_							,			references=a_   )
			self.assertTrue(abs(char_error_rate - 0.125   ) < 1E-6   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		int         =							['''werewolf''']
			__snake_case      :		Optional[int]         =							['''wereawolf''']
			# S = 0, D = 1, I = 0, N = 8, CER = 0.125
			__snake_case      :		List[str]         =							cer.compute(predictions=a_							,			references=a_   )
			self.assertTrue(abs(char_error_rate - 0.125   ) < 1E-6   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Tuple         =							['''werewolf''']
			__snake_case      :		Optional[Any]         =							['''wereolf''']
			# S = 0, D = 0, I = 1, N = 8, CER = 0.125
			__snake_case      :		Dict         =							cer.compute(predictions=a_							,			references=a_   )
			self.assertTrue(abs(char_error_rate - 0.125   ) < 1E-6   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		str         =							['''werewolf''']
			__snake_case      :		int         =							cer.compute(predictions=a_							,			references=a_   )
			self.assertEqual(a_							,			0.0   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		List[Any]         =							['''werewolf''', '''I am your father''']
			__snake_case      :		str         =							cer.compute(predictions=a_							,			references=a_   )
			self.assertEqual(a_							,			0.0   )

			__snake_case      :		List[str]         =							['''werewolf''', '''I am your father''', '''doge''']
			__snake_case      :		Optional[int]         =							['''werxwolf''', '''I       am your father''', '''doge''']
			# S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
			__snake_case      :		Tuple         =							cer.compute(predictions=a_							,			references=a_   )
			self.assertTrue(abs(char_error_rate - 0.0357_1428   ) < 1E-6   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Tuple         =							['''My hovercraft''', '''is full of eels''']
			__snake_case      :		List[str]         =							['''My hovercraft is full''', ''' of eels''']
			# S = 0, D = 0, I = 2, N = 28, CER = 2 / 28
			# whitespace at the front of " of eels" will be strip during preporcessing
			# so need to insert 2 whitespaces
			__snake_case      :		List[Any]         =							cer.compute(predictions=a_							,			references=a_							,			concatenate_texts=a_   )
			self.assertTrue(abs(char_error_rate - 0.07_1428   ) < 1E-6   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		str         =							['''我能吞下玻璃而不伤身体''']
			__snake_case      :		Any         =							[''' 能吞虾玻璃而 不霜身体啦''']
			# S = 3, D = 2, I = 0, N = 11, CER = 5 / 11
			__snake_case      :		List[Any]         =							cer.compute(predictions=a_							,			references=a_   )
			self.assertTrue(abs(char_error_rate - 0.45_4545_4545   ) < 1E-6   )

			__snake_case      :		Tuple         =							['''我能吞下玻璃''', '''而不伤身体''']
			__snake_case      :		Tuple         =							['''我    能 吞 下 玻 璃''', '''而不伤身体''']
			# S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
			__snake_case      :		Union[str, Any]         =							cer.compute(predictions=a_							,			references=a_   )
			self.assertTrue(abs(char_error_rate - 0.4545_4545_4545   ) < 1E-6   )

			__snake_case      :		Tuple         =							['''我能吞下玻璃而不伤身体''']
			__snake_case      :		Union[str, Any]         =							cer.compute(predictions=a_							,			references=a_   )
			self.assertFalse(a_							,			0.0   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		str         =							['''''']
			__snake_case      :		int         =							['''Hypothesis''']
			with self.assertRaises(a_   ):
				cer.compute(predictions=a_							,			references=a_   )


if __name__ == "__main__":
			unittest.main()











'''simple docstring'''


import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin






class 						lowerCAmelCase_	(       UpperCAmelCase_							):







  '''simple docstring'''

  UpperCamelCase_			:      Optional[int]										=				"""AutoFeatureExtractor"""
  UpperCamelCase_			:      List[Any]										=				"""Speech2Text2Tokenizer"""



  def __init__( self				:							str					,  SCREAMING_SNAKE_CASE_				:							str					,  SCREAMING_SNAKE_CASE_				:							int	)			->		Any:







       '''simple docstring'''



       super().__init__(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	)
       A:		Union[str, Any]          =		self.feature_extractor
       A:		Dict          =		False



  def __call__( self				:							Any					,  *SCREAMING_SNAKE_CASE_				:							Any					,  **SCREAMING_SNAKE_CASE_				:							Optional[int]	)			->		Optional[Any]:







       '''simple docstring'''



       if self._in_target_context_manager:
            return self.current_processor(*SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_	)

       if "raw_speech" in kwargs:
            warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''	)
            A:		List[str]          =		kwargs.pop('''raw_speech'''	)
       else:
            A:		Any          =		kwargs.pop('''audio'''					,  SCREAMING_SNAKE_CASE_	)
       A:		List[Any]          =		kwargs.pop('''sampling_rate'''					,  SCREAMING_SNAKE_CASE_	)
       A:		Optional[int]          =		kwargs.pop('''text'''					,  SCREAMING_SNAKE_CASE_	)
       if len(SCREAMING_SNAKE_CASE_	) > 0:
            A:		str          =		args[0]
            A:		Tuple          =		args[1:]

       if audio is None and text is None:
            raise ValueError('''You need to specify either an `audio` or `text` input to process.'''	)

       if audio is not None:
            A:		Tuple          =		self.feature_extractor(SCREAMING_SNAKE_CASE_					,  *SCREAMING_SNAKE_CASE_					,  sampling_rate=SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_	)
       if text is not None:
            A:		List[Any]          =		self.tokenizer(SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_	)

       if text is None:
            return inputs
       elif audio is None:
            return encodings
       else:
            A:		str          =		encodings['''input_ids''']
            return inputs



  def 					_snake_case     ( self				:							Tuple					,  *SCREAMING_SNAKE_CASE_				:							int					,  **SCREAMING_SNAKE_CASE_				:							Union[str, Any]	)			->		str:







       '''simple docstring'''



       return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_	)



  def 					_snake_case     ( self				:							Optional[int]					,  *SCREAMING_SNAKE_CASE_				:							str					,  **SCREAMING_SNAKE_CASE_				:							int	)			->		Union[str, Any]:







       '''simple docstring'''



       return self.tokenizer.decode(*SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_	)




  @contextmanager
  def 					_snake_case     ( self				:							str	)			->		int:







       '''simple docstring'''



       warnings.warn(
           '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '''
           '''labels by using the argument `text` of the regular `__call__` method (either in the same call as '''
           '''your audio inputs, or in a separate call.'''	)
       A:		List[Any]          =		True
       A:		List[str]          =		self.tokenizer
       yield
       A:		List[Any]          =		self.feature_extractor
       A:		int          =		False






