import unittest

from cer import CER


__a  :					Optional[Any]          =  CER()




class        _UpperCamelCase				(						unittest.TestCase			):


			"""simple docstring"""






			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> int:

							'''simple docstring'''


							__lowercase			      =							['''White House''']
							__lowercase			      =							['''white house''']
							# S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
							__lowercase			      =							cer.compute(predictions=lowerCAmelCase__				,       references=lowerCAmelCase__						)
							self.assertTrue(abs(char_error_rate - 0.18_1818_1818						) < 1E-6						)





			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> List[str]:

							'''simple docstring'''


							__lowercase			      =							['''were wolf''']
							__lowercase			      =							['''werewolf''']
							# S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
							__lowercase			      =							cer.compute(predictions=lowerCAmelCase__				,       references=lowerCAmelCase__						)
							self.assertTrue(abs(char_error_rate - 0.111_1111						) < 1E-6						)

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

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

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





			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> Any:

							'''simple docstring'''


							__lowercase			      =							['''werewolf''']
							__lowercase			      =							['''weaewolf''']
							# S = 1, D = 0, I = 0, N = 8, CER = 0.125
							__lowercase			      =							cer.compute(predictions=lowerCAmelCase__				,       references=lowerCAmelCase__						)
							self.assertTrue(abs(char_error_rate - 0.125						) < 1E-6						)





			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> List[Any]:

							'''simple docstring'''


							__lowercase			      =							['''werewolf''']
							__lowercase			      =							['''wereawolf''']
							# S = 0, D = 1, I = 0, N = 8, CER = 0.125
							__lowercase			      =							cer.compute(predictions=lowerCAmelCase__				,       references=lowerCAmelCase__						)
							self.assertTrue(abs(char_error_rate - 0.125						) < 1E-6						)





			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> int:

							'''simple docstring'''


							__lowercase			      =							['''werewolf''']
							__lowercase			      =							['''wereolf''']
							# S = 0, D = 0, I = 1, N = 8, CER = 0.125
							__lowercase			      =							cer.compute(predictions=lowerCAmelCase__				,       references=lowerCAmelCase__						)
							self.assertTrue(abs(char_error_rate - 0.125						) < 1E-6						)





			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> Dict:

							'''simple docstring'''


							__lowercase			      =							['''werewolf''']
							__lowercase			      =							cer.compute(predictions=lowerCAmelCase__				,       references=lowerCAmelCase__						)
							self.assertEqual(lowerCAmelCase__				,       0.0						)





			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> int:

							'''simple docstring'''


							__lowercase			      =							['''werewolf''', '''I am your father''']
							__lowercase			      =							cer.compute(predictions=lowerCAmelCase__				,       references=lowerCAmelCase__						)
							self.assertEqual(lowerCAmelCase__				,       0.0						)

							__lowercase			      =							['''werewolf''', '''I am your father''', '''doge''']
							__lowercase			      =							['''werxwolf''', '''I       am your father''', '''doge''']
							# S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
							__lowercase			      =							cer.compute(predictions=lowerCAmelCase__				,       references=lowerCAmelCase__						)
							self.assertTrue(abs(char_error_rate - 0.0357_1428						) < 1E-6						)





			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> Dict:

							'''simple docstring'''


							__lowercase			      =							['''My hovercraft''', '''is full of eels''']
							__lowercase			      =							['''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
							__lowercase			      =							cer.compute(predictions=lowerCAmelCase__				,       references=lowerCAmelCase__				,       concatenate_texts=lowerCAmelCase__						)
							self.assertTrue(abs(char_error_rate - 0.07_1428						) < 1E-6						)





			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> Union[str, Any]:

							'''simple docstring'''


							__lowercase			      =							['''我能吞下玻璃而不伤身体''']
							__lowercase			      =							[''' 能吞虾玻璃而 不霜身体啦''']
							# S = 3, D = 2, I = 0, N = 11, CER = 5 / 11
							__lowercase			      =							cer.compute(predictions=lowerCAmelCase__				,       references=lowerCAmelCase__						)
							self.assertTrue(abs(char_error_rate - 0.45_4545_4545						) < 1E-6						)

							__lowercase			      =							['''我能吞下玻璃''', '''而不伤身体''']
							__lowercase			      =							['''我    能 吞 下 玻 璃''', '''而不伤身体''']
							# S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
							__lowercase			      =							cer.compute(predictions=lowerCAmelCase__				,       references=lowerCAmelCase__						)
							self.assertTrue(abs(char_error_rate - 0.4545_4545_4545						) < 1E-6						)

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





			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> List[str]:

							'''simple docstring'''


							__lowercase			      =							['''''']
							__lowercase			      =							['''Hypothesis''']
							with self.assertRaises(lowerCAmelCase__						):
											cer.compute(predictions=lowerCAmelCase__				,       references=lowerCAmelCase__						)


if __name__ == "__main__":
		unittest.main()
from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PretrainedConfig, PreTrainedTokenizer
from ...onnx import OnnxConfig, PatchingSpec
from ...utils import TensorType, is_torch_available, logging


__a  :					int          =  logging.get_logger(__name__)

__a  :					Optional[Any]          =  {
    """microsoft/layoutlm-base-uncased""": (
        """https://huggingface.co/microsoft/layoutlm-base-uncased/resolve/main/config.json"""
    ),
    """microsoft/layoutlm-large-uncased""": (
        """https://huggingface.co/microsoft/layoutlm-large-uncased/resolve/main/config.json"""
    ),
}




class        _UpperCamelCase				(						_UpperCAmelCase			):


			"""simple docstring"""






			__a  :			List[Any]      			=						'''layoutlm'''





			def __init__(       self				,       lowerCAmelCase__=3_05_22				,       lowerCAmelCase__=7_68				,       lowerCAmelCase__=12				,       lowerCAmelCase__=12				,       lowerCAmelCase__=30_72				,       lowerCAmelCase__="gelu"				,       lowerCAmelCase__=0.1				,       lowerCAmelCase__=0.1				,       lowerCAmelCase__=5_12				,       lowerCAmelCase__=2				,       lowerCAmelCase__=0.02				,       lowerCAmelCase__=1E-12				,       lowerCAmelCase__=0				,       lowerCAmelCase__="absolute"				,       lowerCAmelCase__=True				,       lowerCAmelCase__=10_24				,       **lowerCAmelCase__				,       )					-> Tuple:

							'''simple docstring'''


							super().__init__(pad_token_id=lowerCAmelCase__				,       **lowerCAmelCase__						)
							__lowercase			      =							vocab_size
							__lowercase			      =							hidden_size
							__lowercase			      =							num_hidden_layers
							__lowercase			      =							num_attention_heads
							__lowercase			      =							hidden_act
							__lowercase			      =							intermediate_size
							__lowercase			      =							hidden_dropout_prob
							__lowercase			      =							attention_probs_dropout_prob
							__lowercase			      =							max_position_embeddings
							__lowercase			      =							type_vocab_size
							__lowercase			      =							initializer_range
							__lowercase			      =							layer_norm_eps
							__lowercase			      =							position_embedding_type
							__lowercase			      =							use_cache
							__lowercase			      =							max_ad_position_embeddings




class        _UpperCamelCase				(						_UpperCAmelCase			):


			"""simple docstring"""






			def __init__(       self				,       lowerCAmelCase__				,       lowerCAmelCase__ = "default"				,       lowerCAmelCase__ = None				,       )					-> str:

							'''simple docstring'''


							super().__init__(lowerCAmelCase__				,       task=lowerCAmelCase__				,       patching_specs=lowerCAmelCase__						)
							__lowercase			      =							config.max_ad_position_embeddings - 1





			@property
			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> Mapping[str, Mapping[int, str]]:

							'''simple docstring'''


							return OrderedDict(
							    [
							        ('''input_ids''', {0: '''batch''', 1: '''sequence'''}),
							        ('''bbox''', {0: '''batch''', 1: '''sequence'''}),
							        ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}),
							        ('''token_type_ids''', {0: '''batch''', 1: '''sequence'''}),
							    ]						)





			def 	_SCREAMING_SNAKE_CASE      (       self				,       lowerCAmelCase__				,       lowerCAmelCase__ = -1				,       lowerCAmelCase__ = -1				,       lowerCAmelCase__ = False				,       lowerCAmelCase__ = None				,       )					-> Mapping[str, Any]:

							'''simple docstring'''


							__lowercase			      =							super().generate_dummy_inputs(
							    lowerCAmelCase__				,       batch_size=lowerCAmelCase__				,       seq_length=lowerCAmelCase__				,       is_pair=lowerCAmelCase__				,       framework=lowerCAmelCase__						)

							# Generate a dummy bbox
							__lowercase			      =							[48, 84, 73, 1_28]

							if not framework == TensorType.PYTORCH:
											raise NotImplementedError('''Exporting LayoutLM to ONNX is currently only supported for PyTorch.'''						)

							if not is_torch_available():
											raise ValueError('''Cannot generate dummy inputs without PyTorch installed.'''						)
							import torch

							__lowercase      ,					__lowercase			      =							input_dict['''input_ids'''].shape
							__lowercase			      =							torch.tensor([*[box] * seq_length]						).tile(lowerCAmelCase__				,       1				,       1						)
							return input_dict