





"""simple docstring"""


import unittest

from cer import CER


A_     :      str								=CER()







class 	__a   (  unittest.TestCase		):



     def 						snake_case_			(       self   ):
            _lowerCamelCase    	=					['White House']
            _lowerCamelCase    	=					['white house']
            # S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
            _lowerCamelCase    	=					cer.compute(predictions=UpperCAmelCase_					,     references=UpperCAmelCase_   )
            self.assertTrue(abs(char_error_rate - 0.1818181818   ) < 1e-6   )



     def 						snake_case_			(       self   ):
            _lowerCamelCase    	=					['were wolf']
            _lowerCamelCase    	=					['werewolf']
            # S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
            _lowerCamelCase    	=					cer.compute(predictions=UpperCAmelCase_					,     references=UpperCAmelCase_   )
            self.assertTrue(abs(char_error_rate - 0.1111111   ) < 1e-6   )

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

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

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



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



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



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



     def 						snake_case_			(       self   ):
            _lowerCamelCase    	=					['werewolf']
            _lowerCamelCase    	=					cer.compute(predictions=UpperCAmelCase_					,     references=UpperCAmelCase_   )
            self.assertEqual(UpperCAmelCase_					,     0.0   )



     def 						snake_case_			(       self   ):
            _lowerCamelCase    	=					['werewolf', 'I am your father']
            _lowerCamelCase    	=					cer.compute(predictions=UpperCAmelCase_					,     references=UpperCAmelCase_   )
            self.assertEqual(UpperCAmelCase_					,     0.0   )

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



     def 						snake_case_			(       self   ):
            _lowerCamelCase    	=					['My hovercraft', 'is full of eels']
            _lowerCamelCase    	=					['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    	=					cer.compute(predictions=UpperCAmelCase_					,     references=UpperCAmelCase_					,     concatenate_texts=UpperCAmelCase_   )
            self.assertTrue(abs(char_error_rate - 0.071428   ) < 1e-6   )



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

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

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



     def 						snake_case_			(       self   ):
            _lowerCamelCase    	=					['']
            _lowerCamelCase    	=					['Hypothesis']
            with self.assertRaises(UpperCAmelCase_   ):
                   cer.compute(predictions=UpperCAmelCase_					,     references=UpperCAmelCase_   )


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













from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


UpperCAmelCase__  :	Dict     =logging.get_logger(__name__)


if is_vision_available():
							import PIL

class 					__A   (  a			):
				__A            = ["""pixel_values"""]


				def __init__(						self      ,       UpperCAmelCase_ = True      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = PILImageResampling.BICUBIC      ,       UpperCAmelCase_ = True      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = True      ,       UpperCAmelCase_ = 1 / 255      ,       UpperCAmelCase_ = True      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = True      ,       **UpperCAmelCase_      ,       ):
								super().__init__(**UpperCAmelCase_							)
								lowerCamelCase		       =size if size is not None else {"""shortest_edge""": 224}
								lowerCamelCase		       =get_size_dict(UpperCAmelCase_      ,       default_to_square=UpperCAmelCase_							)
								lowerCamelCase		       =crop_size if crop_size is not None else {"""height""": 224, """width""": 224}
								lowerCamelCase		       =get_size_dict(UpperCAmelCase_							)

								lowerCamelCase		       =do_resize
								lowerCamelCase		       =size
								lowerCamelCase		       =resample
								lowerCamelCase		       =do_center_crop
								lowerCamelCase		       =crop_size
								lowerCamelCase		       =do_rescale
								lowerCamelCase		       =rescale_factor
								lowerCamelCase		       =do_normalize
								lowerCamelCase		       =image_mean if image_mean is not None else OPENAI_CLIP_MEAN
								lowerCamelCase		       =image_std if image_std is not None else OPENAI_CLIP_STD
								lowerCamelCase		       =do_convert_rgb


				def _snake_case				(						self      ,       UpperCAmelCase_      ,       UpperCAmelCase_      ,       UpperCAmelCase_ = PILImageResampling.BICUBIC      ,       UpperCAmelCase_ = None      ,       **UpperCAmelCase_      ,       ):
								lowerCamelCase		       =get_size_dict(UpperCAmelCase_      ,       default_to_square=UpperCAmelCase_							)
								lowerCamelCase		       =get_resize_output_image_size(
								    UpperCAmelCase_      ,       size=(size["""height"""], size["""width"""])      ,       default_to_square=UpperCAmelCase_							)
								return resize(UpperCAmelCase_      ,       size=UpperCAmelCase_      ,       resample=UpperCAmelCase_      ,       data_format=UpperCAmelCase_      ,       **UpperCAmelCase_							)


				def _snake_case				(						self      ,       UpperCAmelCase_      ,       UpperCAmelCase_      ,       UpperCAmelCase_ = None      ,       **UpperCAmelCase_      ,       ):
								lowerCamelCase		       =get_size_dict(UpperCAmelCase_							)
								return center_crop(UpperCAmelCase_      ,       size=(size["""height"""], size["""width"""])      ,       data_format=UpperCAmelCase_      ,       **UpperCAmelCase_							)


				def _snake_case				(						self      ,       UpperCAmelCase_      ,       UpperCAmelCase_      ,       UpperCAmelCase_ = None      ,       **UpperCAmelCase_      ,       ):
								return rescale(UpperCAmelCase_      ,       scale=UpperCAmelCase_      ,       data_format=UpperCAmelCase_      ,       **UpperCAmelCase_							)


				def _snake_case				(						self      ,       UpperCAmelCase_      ,       UpperCAmelCase_      ,       UpperCAmelCase_      ,       UpperCAmelCase_ = None      ,       **UpperCAmelCase_      ,       ):
								return normalize(UpperCAmelCase_      ,       mean=UpperCAmelCase_      ,       std=UpperCAmelCase_      ,       data_format=UpperCAmelCase_      ,       **UpperCAmelCase_							)


				def _snake_case				(						self      ,       UpperCAmelCase_      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = None      ,       UpperCAmelCase_ = ChannelDimension.FIRST      ,       **UpperCAmelCase_      ,       ):
								lowerCamelCase		       =do_resize if do_resize is not None else self.do_resize
								lowerCamelCase		       =size if size is not None else self.size
								lowerCamelCase		       =get_size_dict(UpperCAmelCase_      ,       default_to_square=UpperCAmelCase_							)
								lowerCamelCase		       =resample if resample is not None else self.resample
								lowerCamelCase		       =do_center_crop if do_center_crop is not None else self.do_center_crop
								lowerCamelCase		       =crop_size if crop_size is not None else self.crop_size
								lowerCamelCase		       =get_size_dict(UpperCAmelCase_							)
								lowerCamelCase		       =do_rescale if do_rescale is not None else self.do_rescale
								lowerCamelCase		       =rescale_factor if rescale_factor is not None else self.rescale_factor
								lowerCamelCase		       =do_normalize if do_normalize is not None else self.do_normalize
								lowerCamelCase		       =image_mean if image_mean is not None else self.image_mean
								lowerCamelCase		       =image_std if image_std is not None else self.image_std
								lowerCamelCase		       =do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

								lowerCamelCase		       =make_list_of_images(UpperCAmelCase_							)

								if not valid_images(UpperCAmelCase_							):
												raise ValueError(
												    """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
												    """torch.Tensor, tf.Tensor or jax.ndarray."""							)

								if do_resize and size is None:
												raise ValueError("""Size must be specified if do_resize is True."""							)

								if do_center_crop and crop_size is None:
												raise ValueError("""Crop size must be specified if do_center_crop is True."""							)

								if do_rescale and rescale_factor is None:
												raise ValueError("""Rescale factor must be specified if do_rescale is True."""							)

								if do_normalize and (image_mean is None or image_std is None):
												raise ValueError("""Image mean and std must be specified if do_normalize is True."""							)

								# PIL RGBA images are converted to RGB
								if do_convert_rgb:
												lowerCamelCase		       =[convert_to_rgb(UpperCAmelCase_							) for image in images]

								# All transformations expect numpy arrays.
								lowerCamelCase		       =[to_numpy_array(UpperCAmelCase_							) for image in images]

								if do_resize:
												lowerCamelCase		       =[self.resize(image=UpperCAmelCase_      ,       size=UpperCAmelCase_      ,       resample=UpperCAmelCase_							) for image in images]

								if do_center_crop:
												lowerCamelCase		       =[self.center_crop(image=UpperCAmelCase_      ,       size=UpperCAmelCase_							) for image in images]

								if do_rescale:
												lowerCamelCase		       =[self.rescale(image=UpperCAmelCase_      ,       scale=UpperCAmelCase_							) for image in images]

								if do_normalize:
												lowerCamelCase		       =[self.normalize(image=UpperCAmelCase_      ,       mean=UpperCAmelCase_      ,       std=UpperCAmelCase_							) for image in images]

								lowerCamelCase		       =[to_channel_dimension_format(UpperCAmelCase_      ,       UpperCAmelCase_							) for image in images]

								lowerCamelCase		       ={"""pixel_values""": images}
								return BatchFeature(data=UpperCAmelCase_      ,       tensor_type=UpperCAmelCase_							)





