

"""simple docstring"""






import unittest

from cer import CER


lowerCamelCase__				:  Optional[Any]													=  CER()






class     _UpperCAmelCase			(		unittest.TestCase):
			def 							__snake_case (		self     )    ->    Optional[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							Union[str, Any]									=					["""White House"""]
				_UpperCAmelCase    :							List[Any]									=					["""white house"""]
				# S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
				_UpperCAmelCase    :							Tuple									=					cer.compute(predictions=a_      ,		references=a_     )
				self.assertTrue(abs(char_error_rate - 0.1818181818     ) < 1e-6     )
			def 							__snake_case (		self     )    ->    int:






				'''simple docstring'''







				_UpperCAmelCase    :							List[str]									=					["""were wolf"""]
				_UpperCAmelCase    :							List[Any]									=					["""werewolf"""]
				# S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
				_UpperCAmelCase    :							Dict									=					cer.compute(predictions=a_      ,		references=a_     )
				self.assertTrue(abs(char_error_rate - 0.1111111     ) < 1e-6     )

				_UpperCAmelCase    :							Any									=					["""werewolf"""]
				_UpperCAmelCase    :							str									=					["""weae     wolf"""]
				# S = 1, D = 1, I = 0, N = 8, CER = 0.25
				_UpperCAmelCase    :							Dict									=					cer.compute(predictions=a_      ,		references=a_     )
				self.assertTrue(abs(char_error_rate - 0.25     ) < 1e-6     )

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

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






				'''simple docstring'''







				_UpperCAmelCase    :							List[Any]									=					["""werewolf"""]
				_UpperCAmelCase    :							Optional[Any]									=					["""weaewolf"""]
				# S = 1, D = 0, I = 0, N = 8, CER = 0.125
				_UpperCAmelCase    :							Optional[int]									=					cer.compute(predictions=a_      ,		references=a_     )
				self.assertTrue(abs(char_error_rate - 0.125     ) < 1e-6     )
			def 							__snake_case (		self     )    ->    List[Any]:






				'''simple docstring'''







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






				'''simple docstring'''







				_UpperCAmelCase    :							str									=					["""werewolf"""]
				_UpperCAmelCase    :							Dict									=					["""wereolf"""]
				# S = 0, D = 0, I = 1, N = 8, CER = 0.125
				_UpperCAmelCase    :							int									=					cer.compute(predictions=a_      ,		references=a_     )
				self.assertTrue(abs(char_error_rate - 0.125     ) < 1e-6     )
			def 							__snake_case (		self     )    ->    Optional[int]:






				'''simple docstring'''







				_UpperCAmelCase    :							List[Any]									=					["""werewolf"""]
				_UpperCAmelCase    :							int									=					cer.compute(predictions=a_      ,		references=a_     )
				self.assertEqual(a_      ,		0.0     )
			def 							__snake_case (		self     )    ->    List[str]:






				'''simple docstring'''







				_UpperCAmelCase    :							int									=					["""werewolf""", """I am your father"""]
				_UpperCAmelCase    :							Tuple									=					cer.compute(predictions=a_      ,		references=a_     )
				self.assertEqual(a_      ,		0.0     )

				_UpperCAmelCase    :							List[Any]									=					["""werewolf""", """I am your father""", """doge"""]
				_UpperCAmelCase    :							List[str]									=					["""werxwolf""", """I       am your father""", """doge"""]
				# S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
				_UpperCAmelCase    :							Union[str, Any]									=					cer.compute(predictions=a_      ,		references=a_     )
				self.assertTrue(abs(char_error_rate - 0.03571428     ) < 1e-6     )
			def 							__snake_case (		self     )    ->    List[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							List[str]									=					["""My hovercraft""", """is full of eels"""]
				_UpperCAmelCase    :							List[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
				_UpperCAmelCase    :							str									=					cer.compute(predictions=a_      ,		references=a_      ,		concatenate_texts=a_     )
				self.assertTrue(abs(char_error_rate - 0.071428     ) < 1e-6     )
			def 							__snake_case (		self     )    ->    Optional[int]:






				'''simple docstring'''







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

				_UpperCAmelCase    :							List[Any]									=					["""我能吞下玻璃""", """而不伤身体"""]
				_UpperCAmelCase    :							int									=					["""我    能 吞 下 玻 璃""", """而不伤身体"""]
				# S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
				_UpperCAmelCase    :							str									=					cer.compute(predictions=a_      ,		references=a_     )
				self.assertTrue(abs(char_error_rate - 0.454545454545     ) < 1e-6     )

				_UpperCAmelCase    :							List[Any]									=					["""我能吞下玻璃而不伤身体"""]
				_UpperCAmelCase    :							Any									=					cer.compute(predictions=a_      ,		references=a_     )
				self.assertFalse(a_      ,		0.0     )
			def 							__snake_case (		self     )    ->    Dict:






				'''simple docstring'''







				_UpperCAmelCase    :							Optional[int]									=					[""""""]
				_UpperCAmelCase    :							Optional[int]									=					["""Hypothesis"""]
				with self.assertRaises(a_     ):
					cer.compute(predictions=a_      ,		references=a_     )


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



"""simple docstring"""






from __future__ import annotations
def       UpperCamelCase					(			_lowerCAmelCase     :						list,  _lowerCAmelCase     :						list )				->			list:
	_UpperCAmelCase    :							List[Any]									=					[None] * (len(_lowerCAmelCase ) + len(_lowerCAmelCase ))

	_UpperCAmelCase    :							Union[str, Any]									=					0  # pointer to current index for left Half
	_UpperCAmelCase    :							str									=					0  # pointer to current index for the right Half
	_UpperCAmelCase    :							Optional[int]									=					0  # pointer to current index for the sorted array Half

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

	while pointera < len(_lowerCAmelCase ):
		_UpperCAmelCase    :							Union[str, Any]									=					right_half[pointera]
		pointera += 1
		index += 1

	return sorted_array
def       UpperCamelCase					(			_lowerCAmelCase     :						list )				->			list:
	if len(_lowerCAmelCase ) <= 1:
		return array
	# the actual formula to calculate the middle element = left + (right - left) // 2
	# this avoids integer overflow in case of large N
	_UpperCAmelCase    :							Any									=					0 + (len(_lowerCAmelCase ) - 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
	_UpperCAmelCase    :							Optional[Any]									=					array[:middle]
	_UpperCAmelCase    :							Optional[int]									=					array[middle:]

	return merge(merge_sort(_lowerCAmelCase ),  merge_sort(_lowerCAmelCase ) )


if __name__ == "__main__":
						import doctest

						doctest.testmod()





