
"""simple docstring"""




import unittest

from cer import CER


__lowerCAmelCase							:  List[Any] 				=		CER()
class SCREAMING_SNAKE_CASE       (      unittest.TestCase					):






	'''simple docstring'''







	def    _UpperCamelCase							(  self :Tuple			)			->   Union[str, Any]:

			'''simple docstring'''






			a__   		=     ['''White House''']
			a__   		=     ['''white house''']
			# S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
			a__   		=     cer.compute(predictions=__magic_name__       ,    references=__magic_name__			)
			self.assertTrue(abs(char_error_rate - 0.1_818_181_818			) < 1e-6			)


	def    _UpperCamelCase							(  self :Union[str, Any]			)			->   Optional[int]:

			'''simple docstring'''






			a__   		=     ['''were wolf''']
			a__   		=     ['''werewolf''']
			# S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
			a__   		=     cer.compute(predictions=__magic_name__       ,    references=__magic_name__			)
			self.assertTrue(abs(char_error_rate - 0.1_111_111			) < 1e-6			)

			a__   		=     ['''werewolf''']
			a__   		=     ['''weae     wolf''']
			# S = 1, D = 1, I = 0, N = 8, CER = 0.25
			a__   		=     cer.compute(predictions=__magic_name__       ,    references=__magic_name__			)
			self.assertTrue(abs(char_error_rate - 0.25			) < 1e-6			)

			# consecutive whitespaces case 1
			a__   		=     ['''were wolf''']
			a__   		=     ['''were               wolf''']
			# S = 0, D = 0, I = 0, N = 9, CER = 0
			a__   		=     cer.compute(predictions=__magic_name__       ,    references=__magic_name__			)
			self.assertTrue(abs(char_error_rate - 0.0			) < 1e-6			)

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


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

			'''simple docstring'''






			a__   		=     ['''werewolf''']
			a__   		=     ['''weaewolf''']
			# S = 1, D = 0, I = 0, N = 8, CER = 0.125
			a__   		=     cer.compute(predictions=__magic_name__       ,    references=__magic_name__			)
			self.assertTrue(abs(char_error_rate - 0.125			) < 1e-6			)


	def    _UpperCamelCase							(  self :Dict			)			->   Optional[Any]:

			'''simple docstring'''






			a__   		=     ['''werewolf''']
			a__   		=     ['''wereawolf''']
			# S = 0, D = 1, I = 0, N = 8, CER = 0.125
			a__   		=     cer.compute(predictions=__magic_name__       ,    references=__magic_name__			)
			self.assertTrue(abs(char_error_rate - 0.125			) < 1e-6			)


	def    _UpperCamelCase							(  self :Dict			)			->   str:

			'''simple docstring'''






			a__   		=     ['''werewolf''']
			a__   		=     ['''wereolf''']
			# S = 0, D = 0, I = 1, N = 8, CER = 0.125
			a__   		=     cer.compute(predictions=__magic_name__       ,    references=__magic_name__			)
			self.assertTrue(abs(char_error_rate - 0.125			) < 1e-6			)


	def    _UpperCamelCase							(  self :Dict			)			->   Union[str, Any]:

			'''simple docstring'''






			a__   		=     ['''werewolf''']
			a__   		=     cer.compute(predictions=__magic_name__       ,    references=__magic_name__			)
			self.assertEqual(__magic_name__       ,    0.0			)


	def    _UpperCamelCase							(  self :Any			)			->   Tuple:

			'''simple docstring'''






			a__   		=     ['''werewolf''', '''I am your father''']
			a__   		=     cer.compute(predictions=__magic_name__       ,    references=__magic_name__			)
			self.assertEqual(__magic_name__       ,    0.0			)

			a__   		=     ['''werewolf''', '''I am your father''', '''doge''']
			a__   		=     ['''werxwolf''', '''I       am your father''', '''doge''']
			# S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
			a__   		=     cer.compute(predictions=__magic_name__       ,    references=__magic_name__			)
			self.assertTrue(abs(char_error_rate - 0.03_571_428			) < 1e-6			)


	def    _UpperCamelCase							(  self :str			)			->   List[Any]:

			'''simple docstring'''






			a__   		=     ['''My hovercraft''', '''is full of eels''']
			a__   		=     ['''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
			a__   		=     cer.compute(predictions=__magic_name__       ,    references=__magic_name__       ,    concatenate_texts=__magic_name__			)
			self.assertTrue(abs(char_error_rate - 0.071_428			) < 1e-6			)


	def    _UpperCamelCase							(  self :str			)			->   Union[str, Any]:

			'''simple docstring'''






			a__   		=     ['''我能吞下玻璃而不伤身体''']
			a__   		=     [''' 能吞虾玻璃而 不霜身体啦''']
			# S = 3, D = 2, I = 0, N = 11, CER = 5 / 11
			a__   		=     cer.compute(predictions=__magic_name__       ,    references=__magic_name__			)
			self.assertTrue(abs(char_error_rate - 0.4_545_454_545			) < 1e-6			)

			a__   		=     ['''我能吞下玻璃''', '''而不伤身体''']
			a__   		=     ['''我    能 吞 下 玻 璃''', '''而不伤身体''']
			# S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
			a__   		=     cer.compute(predictions=__magic_name__       ,    references=__magic_name__			)
			self.assertTrue(abs(char_error_rate - 0.454_545_454_545			) < 1e-6			)

			a__   		=     ['''我能吞下玻璃而不伤身体''']
			a__   		=     cer.compute(predictions=__magic_name__       ,    references=__magic_name__			)
			self.assertFalse(__magic_name__       ,    0.0			)






	def    _UpperCamelCase							(  self :Optional[int]			)			->   Optional[Any]:

			'''simple docstring'''






			a__   		=     ['''''']
			a__   		=     ['''Hypothesis''']
			with self.assertRaises(__magic_name__			):
					cer.compute(predictions=__magic_name__       ,    references=__magic_name__			)


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






"""simple docstring"""




import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import MobileNetVaImageProcessor
class SCREAMING_SNAKE_CASE       (      unittest.TestCase					):






	'''simple docstring'''







	def __init__(  self :Tuple       ,    __magic_name__ :str       ,    __magic_name__ :Optional[Any]=7       ,    __magic_name__ :int=3       ,    __magic_name__ :Union[str, Any]=18       ,    __magic_name__ :Union[str, Any]=30       ,    __magic_name__ :str=400       ,    __magic_name__ :str=True       ,    __magic_name__ :Any=None       ,    __magic_name__ :Tuple=True       ,    __magic_name__ :List[str]=None       ,    )			->   Optional[int]:

			'''simple docstring'''






			a__   		=     size if size is not None else {'''shortest_edge''': 20}
			a__   		=     crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
			a__   		=     parent
			a__   		=     batch_size
			a__   		=     num_channels
			a__   		=     image_size
			a__   		=     min_resolution
			a__   		=     max_resolution
			a__   		=     do_resize
			a__   		=     size
			a__   		=     do_center_crop
			a__   		=     crop_size






	def    _UpperCamelCase							(  self :Tuple			)			->   Any:

			'''simple docstring'''






			return {
			    "do_resize": self.do_resize,
			    "size": self.size,
			    "do_center_crop": self.do_center_crop,
			    "crop_size": self.crop_size,
			}

@require_torch
@require_vision
class SCREAMING_SNAKE_CASE       (      lowerCAmelCase					,   unittest.TestCase					):






	'''simple docstring'''







	snake_case__							:		str         =		MobileNetVaImageProcessor if is_vision_available() else None


	def    _UpperCamelCase							(  self :Union[str, Any]			)			->   str:

			'''simple docstring'''






			a__   		=     MobileNetVaImageProcessingTester(self			)


	@property
	def    _UpperCamelCase							(  self :List[str]			)			->   Optional[Any]:

			'''simple docstring'''






			return self.image_processor_tester.prepare_image_processor_dict()


	def    _UpperCamelCase							(  self :Tuple			)			->   Tuple:

			'''simple docstring'''






			a__   		=     self.image_processing_class(**self.image_processor_dict			)
			self.assertTrue(hasattr(__magic_name__       ,    '''do_resize'''			)			)
			self.assertTrue(hasattr(__magic_name__       ,    '''size'''			)			)
			self.assertTrue(hasattr(__magic_name__       ,    '''do_center_crop'''			)			)
			self.assertTrue(hasattr(__magic_name__       ,    '''center_crop'''			)			)


	def    _UpperCamelCase							(  self :List[Any]			)			->   int:

			'''simple docstring'''






			a__   		=     self.image_processing_class.from_dict(self.image_processor_dict			)
			self.assertEqual(image_processor.size       ,    {'''shortest_edge''': 20}			)
			self.assertEqual(image_processor.crop_size       ,    {'''height''': 18, '''width''': 18}			)

			a__   		=     self.image_processing_class.from_dict(self.image_processor_dict       ,    size=42       ,    crop_size=84			)
			self.assertEqual(image_processor.size       ,    {'''shortest_edge''': 42}			)
			self.assertEqual(image_processor.crop_size       ,    {'''height''': 84, '''width''': 84}			)


	def    _UpperCamelCase							(  self :Optional[int]			)			->   Any:

			'''simple docstring'''






			pass


	def    _UpperCamelCase							(  self :Dict			)			->   Tuple:

			'''simple docstring'''






			a__   		=     self.image_processing_class(**self.image_processor_dict			)
			# create random PIL images
			a__   		=     prepare_image_inputs(self.image_processor_tester       ,    equal_resolution=__magic_name__			)
			for image in image_inputs:
					self.assertIsInstance(__magic_name__       ,    Image.Image			)

			# Test not batched input
			a__   		=     image_processing(image_inputs[0]       ,    return_tensors='''pt'''			).pixel_values
			self.assertEqual(
			    encoded_images.shape       ,    (
			        1,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size['''height'''],
			        self.image_processor_tester.crop_size['''width'''],
			    )       ,    )

			# Test batched
			a__   		=     image_processing(__magic_name__       ,    return_tensors='''pt'''			).pixel_values
			self.assertEqual(
			    encoded_images.shape       ,    (
			        self.image_processor_tester.batch_size,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size['''height'''],
			        self.image_processor_tester.crop_size['''width'''],
			    )       ,    )


	def    _UpperCamelCase							(  self :Optional[Any]			)			->   Union[str, Any]:

			'''simple docstring'''






			a__   		=     self.image_processing_class(**self.image_processor_dict			)
			# create random numpy tensors
			a__   		=     prepare_image_inputs(self.image_processor_tester       ,    equal_resolution=__magic_name__       ,    numpify=__magic_name__			)
			for image in image_inputs:
					self.assertIsInstance(__magic_name__       ,    np.ndarray			)

			# Test not batched input
			a__   		=     image_processing(image_inputs[0]       ,    return_tensors='''pt'''			).pixel_values
			self.assertEqual(
			    encoded_images.shape       ,    (
			        1,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size['''height'''],
			        self.image_processor_tester.crop_size['''width'''],
			    )       ,    )

			# Test batched
			a__   		=     image_processing(__magic_name__       ,    return_tensors='''pt'''			).pixel_values
			self.assertEqual(
			    encoded_images.shape       ,    (
			        self.image_processor_tester.batch_size,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size['''height'''],
			        self.image_processor_tester.crop_size['''width'''],
			    )       ,    )






	def    _UpperCamelCase							(  self :List[Any]			)			->   str:

			'''simple docstring'''






			a__   		=     self.image_processing_class(**self.image_processor_dict			)
			# create random PyTorch tensors
			a__   		=     prepare_image_inputs(self.image_processor_tester       ,    equal_resolution=__magic_name__       ,    torchify=__magic_name__			)
			for image in image_inputs:
					self.assertIsInstance(__magic_name__       ,    torch.Tensor			)

			# Test not batched input
			a__   		=     image_processing(image_inputs[0]       ,    return_tensors='''pt'''			).pixel_values
			self.assertEqual(
			    encoded_images.shape       ,    (
			        1,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size['''height'''],
			        self.image_processor_tester.crop_size['''width'''],
			    )       ,    )

			# Test batched
			a__   		=     image_processing(__magic_name__       ,    return_tensors='''pt'''			).pixel_values
			self.assertEqual(
			    encoded_images.shape       ,    (
			        self.image_processor_tester.batch_size,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size['''height'''],
			        self.image_processor_tester.crop_size['''width'''],
			    )       ,    )




