



'''simple docstring'''







import unittest

from cer import CER


lowercase__  :	str            =      CER()

class    lowerCamelCase_				(						unittest.TestCase	):






   '''simple docstring'''

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


   def 						_UpperCamelCase							(	self			):
       UpperCamelCase__	     =      ["""were wolf"""]
       UpperCamelCase__	     =      ["""werewolf"""]
       # S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
       UpperCamelCase__	     =      cer.compute(predictions=SCREAMING_SNAKE_CASE__  ,    references=SCREAMING_SNAKE_CASE__			)
       self.assertTrue(abs(char_error_rate - 0.1_111_111			) < 1e-6			)

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

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

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


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


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


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


   def 						_UpperCamelCase							(	self			):
       UpperCamelCase__	     =      ["""werewolf"""]
       UpperCamelCase__	     =      cer.compute(predictions=SCREAMING_SNAKE_CASE__  ,    references=SCREAMING_SNAKE_CASE__			)
       self.assertEqual(SCREAMING_SNAKE_CASE__  ,    0.0			)


   def 						_UpperCamelCase							(	self			):
       UpperCamelCase__	     =      ["""werewolf""", """I am your father"""]
       UpperCamelCase__	     =      cer.compute(predictions=SCREAMING_SNAKE_CASE__  ,    references=SCREAMING_SNAKE_CASE__			)
       self.assertEqual(SCREAMING_SNAKE_CASE__  ,    0.0			)

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


   def 						_UpperCamelCase							(	self			):
       UpperCamelCase__	     =      ["""My hovercraft""", """is full of eels"""]
       UpperCamelCase__	     =      ["""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__	     =      cer.compute(predictions=SCREAMING_SNAKE_CASE__  ,    references=SCREAMING_SNAKE_CASE__  ,    concatenate_texts=SCREAMING_SNAKE_CASE__			)
       self.assertTrue(abs(char_error_rate - 0.071_428			) < 1e-6			)


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

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

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







   def 						_UpperCamelCase							(	self			):
       UpperCamelCase__	     =      [""""""]
       UpperCamelCase__	     =      ["""Hypothesis"""]
       with self.assertRaises(SCREAMING_SNAKE_CASE__			):
           cer.compute(predictions=SCREAMING_SNAKE_CASE__  ,    references=SCREAMING_SNAKE_CASE__			)


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









# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from typing import List
def   UpperCAmelCase							(		A__				,  A__     )							->					Any:
	if name != "training_function" and name != "main":
		raise ValueError(f'''Incorrect function name passed: {name}, choose either \'main\' or \'training_function\''''     )
	_snake_case     , _snake_case					:							Union[str, Any]					     =   [], False
	for line in lines:
		if not found_start and f'''def {name}''' in line:
			_snake_case					:							Tuple					     =   True
			good_lines.append(A__     )
			continue
		if found_start:
			if name == "training_function" and "def main" in line:
				return good_lines
			if name == "main" and "if __name__" in line:
				return good_lines
			good_lines.append(A__     )
def   UpperCAmelCase							(		A__     )							->					Dict:
	return [line for line in lines if not line.lstrip().startswith("""#"""     ) and line != "\n"]


def   UpperCAmelCase							(		A__				,  A__				,  A__				,  A__ = None     )							->					str:
	with open(A__				,  """r"""     ) as f:
		_snake_case					:							Tuple					     =   f.readlines()
	with open(os.path.abspath(os.path.join("""examples"""				,  """nlp_example.py"""     )     )				,  """r"""     ) as f:
		_snake_case					:							Optional[Any]					     =   f.readlines()
	with open(A__				,  """r"""     ) as f:
		_snake_case					:							Optional[Any]					     =   f.readlines()
	if secondary_filename is not None:
		with open(A__				,  """r"""     ) as f:
			_snake_case					:							List[str]					     =   f.readlines()

    # This is our base, we remove all the code from here in our `full_filename` and `feature_filename` to find the new content
	if parser_only:
		_snake_case					:							Dict					     =   clean_lines(get_function_contents_by_name(A__				,  """main"""     )     )
		_snake_case					:							int					     =   clean_lines(get_function_contents_by_name(A__				,  """main"""     )     )
		_snake_case					:							Optional[Any]					     =   clean_lines(get_function_contents_by_name(A__				,  """main"""     )     )
		if secondary_filename is not None:
			_snake_case					:							Optional[Any]					     =   clean_lines(get_function_contents_by_name(A__				,  """main"""     )     )
	else:
		_snake_case					:							str					     =   clean_lines(get_function_contents_by_name(A__				,  """training_function"""     )     )
		_snake_case					:							List[Any]					     =   clean_lines(get_function_contents_by_name(A__				,  """training_function"""     )     )
		_snake_case					:							Union[str, Any]					     =   clean_lines(get_function_contents_by_name(A__				,  """training_function"""     )     )
		if secondary_filename is not None:
			_snake_case					:							List[Any]					     =   clean_lines(
			    get_function_contents_by_name(A__				,  """training_function"""     )     )

	_snake_case					:							str					     =   """train_dataloader, eval_dataloader = get_dataloaders(accelerator, batch_size)\n"""

	# Specific code in our script that differs from the full version, aka what is new
	_snake_case					:							Dict					     =   []
	_snake_case					:							str					     =   []  # We keep track of the idxs just in case it's a repeated statement
	_snake_case					:							Optional[Any]					     =   iter(A__     )
	for i in range(len(A__     ) - 1     ):
		if i not in passed_idxs:
			_snake_case					:							Any					     =   next(A__     )
			if (line not in full_file_func) and (line.lstrip() != _dl_line):
				if "TESTING_MOCKED_DATALOADERS" not in line:
					new_feature_code.append(A__     )
					passed_idxs.append(A__     )
				else:
					# Skip over the `config['num_epochs'] = 2` statement
					_snake_case					:							Optional[int]					     =   next(A__     )

    # Extract out just the new parts from the full_file_training_func
	_snake_case					:							Any					     =   []
	_snake_case					:							Tuple					     =   []  # We keep track of the idxs just in case it's a repeated statement
	for i, line in enumerate(A__     ):
		if i not in passed_idxs:
			if (line not in full_file_func) and (line.lstrip() != _dl_line):
				if "TESTING_MOCKED_DATALOADERS" not in line:
					new_full_example_parts.append(A__     )
					passed_idxs.append(A__     )

    # Finally, get the overall diff
	_snake_case					:							Optional[int]					     =   [line for line in new_feature_code if line not in new_full_example_parts]
	if secondary_filename is not None:
		_snake_case					:							Optional[int]					     =   [line for line in full_file_contents if line not in secondary_file_func]
		_snake_case					:							int					     =   [line for line in diff_from_example if line not in diff_from_two]

	return diff_from_example





