






'''simple docstring'''







import unittest

from cer import CER


UpperCamelCase_					=				CER()




class 					__UpperCAmelCase (		unittest.TestCase	):

      '''simple docstring'''





      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Any												=	['''White House''']
             UpperCAmelCase__	:   Optional[Any]												=	['''white house''']
             # S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
             UpperCAmelCase__	:   Dict												=	cer.compute(predictions=_UpperCAmelCase  ,		references=_UpperCAmelCase						)
             self.assertTrue(abs(char_error_rate - 0.18_18_18_18_18						) < 1E-6						)




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Optional[Any]												=	['''were wolf''']
             UpperCAmelCase__	:   Dict												=	['''werewolf''']
             # S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
             UpperCAmelCase__	:   Optional[Any]												=	cer.compute(predictions=_UpperCAmelCase  ,		references=_UpperCAmelCase						)
             self.assertTrue(abs(char_error_rate - 0.1_11_11_11						) < 1E-6						)

             UpperCAmelCase__	:   List[Any]												=	['''werewolf''']
             UpperCAmelCase__	:   Tuple												=	['''weae     wolf''']
             # S = 1, D = 1, I = 0, N = 8, CER = 0.25
             UpperCAmelCase__	:   Any												=	cer.compute(predictions=_UpperCAmelCase  ,		references=_UpperCAmelCase						)
             self.assertTrue(abs(char_error_rate - 0.25						) < 1E-6						)

             # consecutive whitespaces case 1
             UpperCAmelCase__	:   Any												=	['''were wolf''']
             UpperCAmelCase__	:   Dict												=	['''were               wolf''']
             # S = 0, D = 0, I = 0, N = 9, CER = 0
             UpperCAmelCase__	:   Optional[Any]												=	cer.compute(predictions=_UpperCAmelCase  ,		references=_UpperCAmelCase						)
             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__	:   Dict												=	cer.compute(predictions=_UpperCAmelCase  ,		references=_UpperCAmelCase						)
             self.assertTrue(abs(char_error_rate - 0.0						) < 1E-6						)




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




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   str												=	['''werewolf''']
             UpperCAmelCase__	:   Optional[Any]												=	['''wereawolf''']
             # S = 0, D = 1, I = 0, N = 8, CER = 0.125
             UpperCAmelCase__	:   Any												=	cer.compute(predictions=_UpperCAmelCase  ,		references=_UpperCAmelCase						)
             self.assertTrue(abs(char_error_rate - 0.1_25						) < 1E-6						)




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




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Optional[int]												=	['''werewolf''']
             UpperCAmelCase__	:   str												=	cer.compute(predictions=_UpperCAmelCase  ,		references=_UpperCAmelCase						)
             self.assertEqual(_UpperCAmelCase  ,		0.0						)




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Union[str, Any]												=	['''werewolf''', '''I am your father''']
             UpperCAmelCase__	:   Tuple												=	cer.compute(predictions=_UpperCAmelCase  ,		references=_UpperCAmelCase						)
             self.assertEqual(_UpperCAmelCase  ,		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__	:   Dict												=	cer.compute(predictions=_UpperCAmelCase  ,		references=_UpperCAmelCase						)
             self.assertTrue(abs(char_error_rate - 0.03_57_14_28						) < 1E-6						)




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Optional[Any]												=	['''My hovercraft''', '''is full of eels''']
             UpperCAmelCase__	:   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
             UpperCAmelCase__	:   Optional[Any]												=	cer.compute(predictions=_UpperCAmelCase  ,		references=_UpperCAmelCase  ,		concatenate_texts=_UpperCAmelCase						)
             self.assertTrue(abs(char_error_rate - 0.07_14_28						) < 1E-6						)




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

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

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




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Union[str, Any]												=	['''''']
             UpperCAmelCase__	:   List[str]												=	['''Hypothesis''']
             with self.assertRaises(_UpperCAmelCase						):
                    cer.compute(predictions=_UpperCAmelCase  ,		references=_UpperCAmelCase						)


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







'''simple docstring'''







import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
  import torch

  from transformers import CamembertModel




@require_torch
@require_sentencepiece
@require_tokenizers
class 					__UpperCAmelCase (		unittest.TestCase	):

      '''simple docstring'''





      @slow
      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Optional[int]												=	CamembertModel.from_pretrained('''camembert-base'''						)
             model.to(_UpperCAmelCase						)

             UpperCAmelCase__	:   Optional[int]												=	torch.tensor(
                 [[5, 121, 11, 660, 16, 730, 2_5543, 110, 83, 6]]  ,		device=_UpperCAmelCase  ,		dtype=torch.long  ,		)  # J'aime le camembert !
             with torch.no_grad():
                    UpperCAmelCase__	:   Tuple												=	model(_UpperCAmelCase						)['''last_hidden_state''']
             UpperCAmelCase__	:   List[str]												=	torch.Size((1, 10, 768)						)
             self.assertEqual(output.shape  ,		_UpperCAmelCase						)
             # compare the actual values for a slice.
             UpperCAmelCase__	:   List[Any]												=	torch.tensor(
                 [[[-0.02_54, 0.02_35, 0.10_27], [0.06_06, -0.18_11, -0.04_18], [-0.15_61, -0.11_27, 0.26_87]]]  ,		device=_UpperCAmelCase  ,		dtype=torch.float  ,		)
             # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
             # camembert.eval()
             # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

             self.assertTrue(torch.allclose(output[:, :3, :3]  ,		_UpperCAmelCase  ,		atol=1E-4						)						)