



import unittest

from cer import CER


UpperCAmelCase								=   CER()

class 	__snake_case (				unittest.TestCase):







				'''simple docstring'''






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


				def _a (							self						):
								a__			 =						["""were wolf"""]
								a__			 =						["""werewolf"""]
								# S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
								a__			 =						cer.compute(predictions=a_					,					references=a_						)
								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=a_					,					references=a_						)
								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=a_					,					references=a_						)
								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=a_					,					references=a_						)
								self.assertTrue(abs(char_error_rate - 0.0						) < 1E-6						)


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


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


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


				def _a (							self						):
								a__			 =						["""werewolf"""]
								a__			 =						cer.compute(predictions=a_					,					references=a_						)
								self.assertEqual(a_					,					0.0						)


				def _a (							self						):
								a__			 =						["""werewolf""", """I am your father"""]
								a__			 =						cer.compute(predictions=a_					,					references=a_						)
								self.assertEqual(a_					,					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=a_					,					references=a_						)
								self.assertTrue(abs(char_error_rate - 0.03_571_428						) < 1E-6						)


				def _a (							self						):
								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=a_					,					references=a_					,					concatenate_texts=a_						)
								self.assertTrue(abs(char_error_rate - 0.071_428						) < 1E-6						)


				def _a (							self						):
								a__			 =						["""我能吞下玻璃而不伤身体"""]
								a__			 =						[""" 能吞虾玻璃而 不霜身体啦"""]
								# S = 3, D = 2, I = 0, N = 11, CER = 5 / 11
								a__			 =						cer.compute(predictions=a_					,					references=a_						)
								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=a_					,					references=a_						)
								self.assertTrue(abs(char_error_rate - 0.454_545_454_545						) < 1E-6						)

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







				def _a (							self						):
								a__			 =						[""""""]
								a__			 =						["""Hypothesis"""]
								with self.assertRaises(a_						):
												cer.compute(predictions=a_					,					references=a_						)


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






import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
	from PIL import Image

	from transformers import AutoProcessor, BertTokenizer, CLIPImageProcessor, GitProcessor, PreTrainedTokenizerFast

@require_vision
class 	__snake_case (				unittest.TestCase):







							'''simple docstring'''






							def _a (							self						):
											a__			 =						tempfile.mkdtemp()

											a__			 =						CLIPImageProcessor()
											a__			 =						BertTokenizer.from_pretrained(
											    """hf-internal-testing/tiny-random-BertModel"""					,					model_input_names=["""input_ids""", """attention_mask"""]						)

											a__			 =						GitProcessor(a_					,					a_						)

											processor.save_pretrained(self.tmpdirname						)


							def _a (							self					,					**a_						):
											return AutoProcessor.from_pretrained(self.tmpdirname					,					**a_						).tokenizer


							def _a (							self					,					**a_						):
											return AutoProcessor.from_pretrained(self.tmpdirname					,					**a_						).image_processor


							def _a (							self						):
											shutil.rmtree(self.tmpdirname						)


							def _a (							self						):
											a__			 =						[np.random.randint(255					,					size=(3, 30, 400)					,					dtype=np.uinta						)]

											a__			 =						[Image.fromarray(np.moveaxis(a_					,					0					,					-1						)						) for x in image_inputs]

											return image_inputs


							def _a (							self						):
											a__			 =						GitProcessor(tokenizer=self.get_tokenizer()					,					image_processor=self.get_image_processor()						)
											processor.save_pretrained(self.tmpdirname						)

											a__			 =						self.get_tokenizer(bos_token="""(BOS)"""					,					eos_token="""(EOS)"""						)
											a__			 =						self.get_image_processor(do_normalize=a_					,					padding_value=1.0						)

											a__			 =						GitProcessor.from_pretrained(
											    self.tmpdirname					,					bos_token="""(BOS)"""					,					eos_token="""(EOS)"""					,					do_normalize=a_					,					padding_value=1.0						)

											self.assertEqual(processor.tokenizer.get_vocab()					,					tokenizer_add_kwargs.get_vocab()						)
											self.assertIsInstance(processor.tokenizer					,					a_						)

											self.assertEqual(processor.image_processor.to_json_string()					,					image_processor_add_kwargs.to_json_string()						)
											self.assertIsInstance(processor.image_processor					,					a_						)


							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						self.prepare_image_inputs()

											a__			 =						image_processor(a_					,					return_tensors="""np"""						)
											a__			 =						processor(images=a_					,					return_tensors="""np"""						)

											for key in input_feat_extract.keys():
															self.assertAlmostEqual(input_feat_extract[key].sum()					,					input_processor[key].sum()					,					delta=1E-2						)


							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						"""lower newer"""

											a__			 =						processor(text=a_						)

											a__			 =						tokenizer(a_					,					return_token_type_ids=a_						)

											for key in encoded_tok.keys():
															self.assertListEqual(encoded_tok[key]					,					encoded_processor[key]						)


							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						"""lower newer"""
											a__			 =						self.prepare_image_inputs()

											a__			 =						processor(text=a_					,					images=a_						)

											self.assertListEqual(list(inputs.keys()						)					,					["""input_ids""", """attention_mask""", """pixel_values"""]						)

											# test if it raises when no input is passed
											with pytest.raises(a_						):
															processor()


							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

											a__			 =						processor.batch_decode(a_						)
											a__			 =						tokenizer.batch_decode(a_						)

											self.assertListEqual(a_					,					a_						)







							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						"""lower newer"""
											a__			 =						self.prepare_image_inputs()

											a__			 =						processor(text=a_					,					images=a_						)

											# For now the processor supports only ['input_ids', 'attention_mask', 'pixel_values']
											self.assertListEqual(list(inputs.keys()						)					,					["""input_ids""", """attention_mask""", """pixel_values"""]						)

