


'''simple docstring'''




import unittest

from cer import CER


_lowerCAmelCase	:		Optional[int]		  =						CER()



class 			lowerCAmelCase					(					unittest.TestCase				):



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     Union[str, Any]   				=			['White House']
		lowerCAmelCase       :     str   				=			['white house']
		# S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
		lowerCAmelCase       :     List[str]   				=			cer.compute(predictions=snake_case__			,       references=snake_case__     )
		self.assertTrue(abs(char_error_rate - 0.1_8_1_8_1_8_1_8_1_8     ) < 1e-6     )



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     Any   				=			['were wolf']
		lowerCAmelCase       :     Optional[Any]   				=			['werewolf']
		# S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
		lowerCAmelCase       :     int   				=			cer.compute(predictions=snake_case__			,       references=snake_case__     )
		self.assertTrue(abs(char_error_rate - 0.1_1_1_1_1_1_1     ) < 1e-6     )

		lowerCAmelCase       :     Dict   				=			['werewolf']
		lowerCAmelCase       :     int   				=			['weae     wolf']
		# S = 1, D = 1, I = 0, N = 8, CER = 0.25
		lowerCAmelCase       :     List[Any]   				=			cer.compute(predictions=snake_case__			,       references=snake_case__     )
		self.assertTrue(abs(char_error_rate - 0.2_5     ) < 1e-6     )

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

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



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     Tuple   				=			['werewolf']
		lowerCAmelCase       :     Optional[Any]   				=			['weaewolf']
		# S = 1, D = 0, I = 0, N = 8, CER = 0.125
		lowerCAmelCase       :     Dict   				=			cer.compute(predictions=snake_case__			,       references=snake_case__     )
		self.assertTrue(abs(char_error_rate - 0.1_2_5     ) < 1e-6     )



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     Any   				=			['werewolf']
		lowerCAmelCase       :     List[Any]   				=			['wereawolf']
		# S = 0, D = 1, I = 0, N = 8, CER = 0.125
		lowerCAmelCase       :     Any   				=			cer.compute(predictions=snake_case__			,       references=snake_case__     )
		self.assertTrue(abs(char_error_rate - 0.1_2_5     ) < 1e-6     )



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     int   				=			['werewolf']
		lowerCAmelCase       :     int   				=			['wereolf']
		# S = 0, D = 0, I = 1, N = 8, CER = 0.125
		lowerCAmelCase       :     List[str]   				=			cer.compute(predictions=snake_case__			,       references=snake_case__     )
		self.assertTrue(abs(char_error_rate - 0.1_2_5     ) < 1e-6     )



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     List[str]   				=			['werewolf']
		lowerCAmelCase       :     Union[str, Any]   				=			cer.compute(predictions=snake_case__			,       references=snake_case__     )
		self.assertEqual(snake_case__			,       0.0     )



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     Union[str, Any]   				=			['werewolf', 'I am your father']
		lowerCAmelCase       :     Optional[int]   				=			cer.compute(predictions=snake_case__			,       references=snake_case__     )
		self.assertEqual(snake_case__			,       0.0     )

		lowerCAmelCase       :     Union[str, Any]   				=			['werewolf', 'I am your father', 'doge']
		lowerCAmelCase       :     int   				=			['werxwolf', 'I       am your father', 'doge']
		# S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
		lowerCAmelCase       :     Dict   				=			cer.compute(predictions=snake_case__			,       references=snake_case__     )
		self.assertTrue(abs(char_error_rate - 0.0_3_5_7_1_4_2_8     ) < 1e-6     )



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     int   				=			['My hovercraft', 'is full of eels']
		lowerCAmelCase       :     Optional[Any]   				=			['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
		lowerCAmelCase       :     Any   				=			cer.compute(predictions=snake_case__			,       references=snake_case__			,       concatenate_texts=snake_case__     )
		self.assertTrue(abs(char_error_rate - 0.0_7_1_4_2_8     ) < 1e-6     )



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     List[Any]   				=			['我能吞下玻璃而不伤身体']
		lowerCAmelCase       :     str   				=			[' 能吞虾玻璃而 不霜身体啦']
		# S = 3, D = 2, I = 0, N = 11, CER = 5 / 11
		lowerCAmelCase       :     Optional[Any]   				=			cer.compute(predictions=snake_case__			,       references=snake_case__     )
		self.assertTrue(abs(char_error_rate - 0.4_5_4_5_4_5_4_5_4_5     ) < 1e-6     )

		lowerCAmelCase       :     Optional[Any]   				=			['我能吞下玻璃', '而不伤身体']
		lowerCAmelCase       :     Union[str, Any]   				=			['我    能 吞 下 玻 璃', '而不伤身体']
		# S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
		lowerCAmelCase       :     Union[str, Any]   				=			cer.compute(predictions=snake_case__			,       references=snake_case__     )
		self.assertTrue(abs(char_error_rate - 0.4_5_4_5_4_5_4_5_4_5_4_5     ) < 1e-6     )

		lowerCAmelCase       :     int   				=			['我能吞下玻璃而不伤身体']
		lowerCAmelCase       :     Dict   				=			cer.compute(predictions=snake_case__			,       references=snake_case__     )
		self.assertFalse(snake_case__			,       0.0     )



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     List[Any]   				=			['']
		lowerCAmelCase       :     List[str]   				=			['Hypothesis']
		with self.assertRaises(snake_case__     ):
			cer.compute(predictions=snake_case__			,       references=snake_case__     )


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




'''simple docstring'''




import inspect

from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
				import jax



@require_flax
class 			lowerCAmelCase					:



	def 					lowercase  (       self     ):
		lowerCAmelCase				, lowerCAmelCase       :     Union[str, Any]   				=			self.prepare_init_args_and_inputs_for_common()

		lowerCAmelCase       :     Any   				=			self.model_class(**snake_case__     )
		lowerCAmelCase       :     int   				=			model.init(inputs_dict['prng_key']			,       inputs_dict['sample']     )
		jax.lax.stop_gradient(snake_case__     )

		lowerCAmelCase       :     Tuple   				=			model.apply(snake_case__			,       inputs_dict['sample']     )

		if isinstance(snake_case__			,       snake_case__     ):
			lowerCAmelCase       :     int   				=			output.sample

		self.assertIsNotNone(snake_case__     )
		lowerCAmelCase       :     List[str]   				=			inputs_dict['sample'].shape
		self.assertEqual(output.shape			,       snake_case__			,       'Input and output shapes do not match'     )



	def 					lowercase  (       self     ):
		lowerCAmelCase				, lowerCAmelCase       :     Optional[int]   				=			self.prepare_init_args_and_inputs_for_common()

		lowerCAmelCase       :     Tuple   				=			16
		lowerCAmelCase       :     Dict   				=			(16, 32)

		lowerCAmelCase       :     Dict   				=			self.model_class(**snake_case__     )
		lowerCAmelCase       :     int   				=			model.init(inputs_dict['prng_key']			,       inputs_dict['sample']     )
		jax.lax.stop_gradient(snake_case__     )

		lowerCAmelCase       :     int   				=			model.apply(snake_case__			,       inputs_dict['sample']     )

		if isinstance(snake_case__			,       snake_case__     ):
			lowerCAmelCase       :     Dict   				=			output.sample

		self.assertIsNotNone(snake_case__     )
		lowerCAmelCase       :     Union[str, Any]   				=			inputs_dict['sample'].shape
		self.assertEqual(output.shape			,       snake_case__			,       'Input and output shapes do not match'     )



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     Any   				=			'kwargs' in inspect.signature(self.model_class.__init__     ).parameters
		lowerCAmelCase       :     Optional[Any]   				=			len(self.model_class._deprecated_kwargs     ) > 0

		if has_kwarg_in_model_class and not has_deprecated_kwarg:
			raise ValueError(
			    f"{self.model_class} has `**kwargs` in its __init__ method but has not defined any deprecated kwargs"
			    ' under the `_deprecated_kwargs` class attribute. Make sure to either remove `**kwargs` if there are'
			    ' no deprecated arguments or add the deprecated argument with `_deprecated_kwargs ='
			    ' [<deprecated_argument>]`'     )

		if not has_kwarg_in_model_class and has_deprecated_kwarg:
			raise ValueError(
			    f"{self.model_class} doesn't have `**kwargs` in its __init__ method but has defined deprecated kwargs"
			    ' under the `_deprecated_kwargs` class attribute. Make sure to either add the `**kwargs` argument to'
			    f" {self.model_class}.__init__ if there are deprecated arguments or remove the deprecated argument"
			    ' from `_deprecated_kwargs = [<deprecated_argument>]`'     )
