




import unittest

from cer import CER


__lowerCamelCase :    Optional[int]      						=      CER()


class  lowerCamelCase     (		unittest.TestCase    ):






			'''simple docstring'''





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

			def 				UpperCAmelCase__							(			self      : Optional[Any]	)    -> Optional[Any]:
								__magic_name__				:						List[Any]     				=				['''were wolf''']
								__magic_name__				:						str     				=				['''werewolf''']
								# S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
								__magic_name__				:						Optional[int]     				=				cer.compute(predictions=lowerCamelCase_							,    references=lowerCamelCase_	)
								self.assertTrue(abs(char_error_rate - 0.1_1_1_1_1_1_1	) < 1E-6	)

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

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

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

			def 				UpperCAmelCase__							(			self      : Any	)    -> Dict:
								__magic_name__				:						Tuple     				=				['''werewolf''']
								__magic_name__				:						List[str]     				=				['''weaewolf''']
								# S = 1, D = 0, I = 0, N = 8, CER = 0.125
								__magic_name__				:						Dict     				=				cer.compute(predictions=lowerCamelCase_							,    references=lowerCamelCase_	)
								self.assertTrue(abs(char_error_rate - 0.1_2_5	) < 1E-6	)

			def 				UpperCAmelCase__							(			self      : Optional[Any]	)    -> Optional[Any]:
								__magic_name__				:						Dict     				=				['''werewolf''']
								__magic_name__				:						Optional[Any]     				=				['''wereawolf''']
								# S = 0, D = 1, I = 0, N = 8, CER = 0.125
								__magic_name__				:						List[Any]     				=				cer.compute(predictions=lowerCamelCase_							,    references=lowerCamelCase_	)
								self.assertTrue(abs(char_error_rate - 0.1_2_5	) < 1E-6	)

			def 				UpperCAmelCase__							(			self      : Any	)    -> str:
								__magic_name__				:						Union[str, Any]     				=				['''werewolf''']
								__magic_name__				:						Optional[Any]     				=				['''wereolf''']
								# S = 0, D = 0, I = 1, N = 8, CER = 0.125
								__magic_name__				:						List[str]     				=				cer.compute(predictions=lowerCamelCase_							,    references=lowerCamelCase_	)
								self.assertTrue(abs(char_error_rate - 0.1_2_5	) < 1E-6	)

			def 				UpperCAmelCase__							(			self      : Optional[int]	)    -> List[str]:
								__magic_name__				:						str     				=				['''werewolf''']
								__magic_name__				:						Any     				=				cer.compute(predictions=lowerCamelCase_							,    references=lowerCamelCase_	)
								self.assertEqual(lowerCamelCase_							,    0.0	)

			def 				UpperCAmelCase__							(			self      : List[str]	)    -> Any:
								__magic_name__				:						str     				=				['''werewolf''', '''I am your father''']
								__magic_name__				:						Optional[Any]     				=				cer.compute(predictions=lowerCamelCase_							,    references=lowerCamelCase_	)
								self.assertEqual(lowerCamelCase_							,    0.0	)

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

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

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

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

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


			def 				UpperCAmelCase__							(			self      : Any	)    -> Union[str, Any]:
								__magic_name__				:						Dict     				=				['''''']
								__magic_name__				:						str     				=				['''Hypothesis''']
								with self.assertRaises(lowerCamelCase_	):
													cer.compute(predictions=lowerCamelCase_							,    references=lowerCamelCase_	)


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









import math


class  lowerCamelCase     :






			'''simple docstring'''





			def __init__(			self      : Dict							,    lowerCamelCase_      : int							,    lowerCamelCase_      : int							,    lowerCamelCase_      : int							,    lowerCamelCase_      : Union[str, Any]	)    -> Optional[Any]:
								__magic_name__				:						Union[str, Any]     				=				x
								__magic_name__				:						Optional[int]     				=				y
								__magic_name__				:						List[str]     				=				step_size
								__magic_name__				:						Union[str, Any]     				=				function_to_optimize

			def 				UpperCAmelCase__							(			self      : int	)    -> int:
								return self.function(self.x							,    self.y	)

			def 				UpperCAmelCase__							(			self      : Dict	)    -> Any:
								__magic_name__				:						Optional[int]     				=				self.step_size
								return [
								    SearchProblem(lowerCamelCase_							,    lowerCamelCase_							,    lowerCamelCase_							,    self.function	)
								    for x, y in (
								        (self.x - step_size, self.y - step_size),
								        (self.x - step_size, self.y),
								        (self.x - step_size, self.y + step_size),
								        (self.x, self.y - step_size),
								        (self.x, self.y + step_size),
								        (self.x + step_size, self.y - step_size),
								        (self.x + step_size, self.y),
								        (self.x + step_size, self.y + step_size),
								    )
								]

			def __hash__(			self      : Dict	)    -> Tuple:
								return hash(str(self	)	)

			def __eq__(			self      : List[str]							,    lowerCamelCase_      : Dict	)    -> Union[str, Any]:
								if isinstance(lowerCamelCase_							,    lowerCamelCase_	):
													return hash(str(self	)	) == hash(str(lowerCamelCase_	)	)
								return False


			def __str__(			self      : Tuple	)    -> Tuple:
								return F'''x: {self.x} y: {self.y}'''







def 		lowercase__ (		__A:						int				,__A:						bool = True				,__A:						float = math.inf				,__A:						float = -math.inf				,__A:						float = math.inf				,__A:						float = -math.inf				,__A:						bool = False				,__A:						int = 1_0_0_0_0				,):





					'''simple docstring'''






					__magic_name__				:						Union[str, Any]     				=				search_prob
					__magic_name__				:						int     				=				[]  # list to store the current score at each iteration
					__magic_name__				:						Any     				=				0
					__magic_name__				:						Optional[Any]     				=				False
					__magic_name__				:						Dict     				=				set()
					while not solution_found and iterations < max_iter:
										visited.add(__A					)
										iterations += 1
										__magic_name__				:						int     				=				current_state.score()
										scores.append(__A					)
										__magic_name__				:						Union[str, Any]     				=				current_state.get_neighbors()
										__magic_name__				:						str     				=				-math.inf
										__magic_name__				:						Optional[Any]     				=				math.inf
										__magic_name__				:						Any     				=				None  # to hold the next best neighbor
										for neighbor in neighbors:
															if neighbor in visited:
																				continue  # do not want to visit the same state again
															if (
															    neighbor.x > max_x
															    or neighbor.x < min_x
															    or neighbor.y > max_y
															    or neighbor.y < min_y
															):
																				continue  # neighbor outside our bounds
															__magic_name__				:						Tuple     				=				neighbor.score() - current_score
															if find_max:  # finding max
																				# going to direction with greatest ascent
																				if change > max_change and change > 0:
																									__magic_name__				:						List[str]     				=				change
																									__magic_name__				:						List[str]     				=				neighbor
															else:  # finding min
																				# to direction with greatest descent
																				if change < min_change and change < 0:
																									__magic_name__				:						List[Any]     				=				change
																									__magic_name__				:						Optional[int]     				=				neighbor
										if next_state is not None:
															# we found at least one neighbor which improved the current state
															__magic_name__				:						Any     				=				next_state
										else:
															# since we have no neighbor that improves the solution we stop the search
															__magic_name__				:						List[str]     				=				True

					if visualization:
										from matplotlib import pyplot as plt

										plt.plot(range(__A					)				,__A					)
										plt.xlabel('''Iterations'''					)
										plt.ylabel('''Function values'''					)
										plt.show()

					return current_state


if __name__ == "__main__":
	import doctest

	doctest.testmod()







	def 		lowercase__ (		__A:						Optional[Any]				,__A:						str					):





						'''simple docstring'''






						return (x**2) + (y**2)

	# starting the problem with initial coordinates (3, 4)
	__lowerCamelCase :    Any      						=      SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa)
	__lowerCamelCase :    Any      						=      hill_climbing(prob, find_max=False)
	print(
	    '''The minimum score for f(x, y) = x^2 + y^2 found via hill climbing: '''
	    F"""{local_min.score()}"""
	)

	# starting the problem with initial coordinates (12, 47)
	__lowerCamelCase :    Optional[int]      						=      SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa)
	__lowerCamelCase :    Union[str, Any]      						=      hill_climbing(
	    prob, find_max=False, max_x=1_00, min_x=5, max_y=50, min_y=-5, visualization=True
	)
	print(
	    '''The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 '''
	    F"""and 50 > y > - 5 found via hill climbing: {local_min.score()}"""
	)







	def 		lowercase__ (		__A:						List[Any]				,__A:						int					):





						'''simple docstring'''






						return (3 * x**2) - (6 * y)

	__lowerCamelCase :    List[str]      						=      SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa)
	__lowerCamelCase :    Optional[int]      						=      hill_climbing(prob, find_max=True)
	print(
	    '''The maximum score for f(x, y) = x^2 + y^2 found via hill climbing: '''
	    F"""{local_min.score()}"""
	)



