
import unittest

from cer import CER


UpperCAmelCase__				:  List[Any]       					=				CER()



class 			UpperCamelCase_			(						unittest.TestCase   ):




							'''simple docstring'''

							def        lowerCAmelCase__ (				self) ->	Optional[Any]:
										UpperCamelCase__    :		Optional[Any]     =				['White House']
										UpperCamelCase__    :		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_1818_1818) < 1E-6)
							def        lowerCAmelCase__ (				self) ->	Optional[int]:
										UpperCamelCase__    :		Union[str, Any]     =				['were wolf']
										UpperCamelCase__    :		Optional[int]     =				['werewolf']
										# S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
										UpperCamelCase__    :		Tuple     =				cer.compute(predictions=UpperCamelCase     ,						references=UpperCamelCase)
										self.assertTrue(abs(char_error_rate - 0.111_1111) < 1E-6)

										UpperCamelCase__    :		int     =				['werewolf']
										UpperCamelCase__    :		int     =				['weae     wolf']
										# S = 1, D = 1, I = 0, N = 8, CER = 0.25
										UpperCamelCase__    :		Union[str, 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__    :		Optional[int]     =				['were               wolf']
										# S = 0, D = 0, I = 0, N = 9, CER = 0
										UpperCamelCase__    :		Any     =				cer.compute(predictions=UpperCamelCase     ,						references=UpperCamelCase)
										self.assertTrue(abs(char_error_rate - 0.0) < 1E-6)

										# consecutive whitespaces case 2
										UpperCamelCase__    :		List[str]     =				['were   wolf']
										UpperCamelCase__    :		Union[str, Any]     =				['were               wolf']
										# S = 0, D = 0, I = 0, N = 9, CER = 0
										UpperCamelCase__    :		Any     =				cer.compute(predictions=UpperCamelCase     ,						references=UpperCamelCase)
										self.assertTrue(abs(char_error_rate - 0.0) < 1E-6)
							def        lowerCAmelCase__ (				self) ->	Tuple:
										UpperCamelCase__    :		Union[str, Any]     =				['werewolf']
										UpperCamelCase__    :		Any     =				['weaewolf']
										# S = 1, D = 0, I = 0, N = 8, CER = 0.125
										UpperCamelCase__    :		Optional[Any]     =				cer.compute(predictions=UpperCamelCase     ,						references=UpperCamelCase)
										self.assertTrue(abs(char_error_rate - 0.125) < 1E-6)
							def        lowerCAmelCase__ (				self) ->	Tuple:
										UpperCamelCase__    :		str     =				['werewolf']
										UpperCamelCase__    :		Dict     =				['wereawolf']
										# S = 0, D = 1, I = 0, N = 8, CER = 0.125
										UpperCamelCase__    :		List[Any]     =				cer.compute(predictions=UpperCamelCase     ,						references=UpperCamelCase)
										self.assertTrue(abs(char_error_rate - 0.125) < 1E-6)
							def        lowerCAmelCase__ (				self) ->	Dict:
										UpperCamelCase__    :		List[str]     =				['werewolf']
										UpperCamelCase__    :		Any     =				['wereolf']
										# S = 0, D = 0, I = 1, N = 8, CER = 0.125
										UpperCamelCase__    :		List[str]     =				cer.compute(predictions=UpperCamelCase     ,						references=UpperCamelCase)
										self.assertTrue(abs(char_error_rate - 0.125) < 1E-6)
							def        lowerCAmelCase__ (				self) ->	Optional[int]:
										UpperCamelCase__    :		List[Any]     =				['werewolf']
										UpperCamelCase__    :		Optional[int]     =				cer.compute(predictions=UpperCamelCase     ,						references=UpperCamelCase)
										self.assertEqual(UpperCamelCase     ,						0.0)
							def        lowerCAmelCase__ (				self) ->	str:
										UpperCamelCase__    :		List[Any]     =				['werewolf', 'I am your father']
										UpperCamelCase__    :		int     =				cer.compute(predictions=UpperCamelCase     ,						references=UpperCamelCase)
										self.assertEqual(UpperCamelCase     ,						0.0)

										UpperCamelCase__    :		List[str]     =				['werewolf', 'I am your father', 'doge']
										UpperCamelCase__    :		Dict     =				['werxwolf', 'I       am your father', 'doge']
										# S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
										UpperCamelCase__    :		List[Any]     =				cer.compute(predictions=UpperCamelCase     ,						references=UpperCamelCase)
										self.assertTrue(abs(char_error_rate - 0.0357_1428) < 1E-6)
							def        lowerCAmelCase__ (				self) ->	List[Any]:
										UpperCamelCase__    :		List[str]     =				['My hovercraft', 'is full of eels']
										UpperCamelCase__    :		str     =				['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_1428) < 1E-6)
							def        lowerCAmelCase__ (				self) ->	Optional[int]:
										UpperCamelCase__    :		Dict     =				['我能吞下玻璃而不伤身体']
										UpperCamelCase__    :		Tuple     =				[' 能吞虾玻璃而 不霜身体啦']
										# 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_4545_4545) < 1E-6)

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

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






							def        lowerCAmelCase__ (				self) ->	int:
										UpperCamelCase__    :		Any     =				['']
										UpperCamelCase__    :		Optional[Any]     =				['Hypothesis']
										with self.assertRaises(UpperCamelCase):
													cer.compute(predictions=UpperCamelCase     ,						references=UpperCamelCase)


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





from typing import Optional

from ..utils.logging import get_logger
from .audio_classificiation import AudioClassification
from .automatic_speech_recognition import AutomaticSpeechRecognition
from .base import TaskTemplate
from .image_classification import ImageClassification
from .language_modeling import LanguageModeling
from .question_answering import QuestionAnsweringExtractive
from .summarization import Summarization
from .text_classification import TextClassification


UpperCAmelCase__				:  Tuple       					=				[
    '''AutomaticSpeechRecognition''',
    '''AudioClassification''',
    '''ImageClassification''',
    '''LanguageModeling''',
    '''QuestionAnsweringExtractive''',
    '''Summarization''',
    '''TaskTemplate''',
    '''TextClassification''',
]

UpperCAmelCase__				:  Dict       					=				get_logger(__name__)


UpperCAmelCase__				:  Dict       					=				{
    AutomaticSpeechRecognition.task: AutomaticSpeechRecognition,
    AudioClassification.task: AudioClassification,
    ImageClassification.task: ImageClassification,
    LanguageModeling.task: LanguageModeling,
    QuestionAnsweringExtractive.task: QuestionAnsweringExtractive,
    Summarization.task: Summarization,
    TextClassification.task: TextClassification,
}



def       _lowercase					(      __SCREAMING_SNAKE_CASE )	->     Optional[TaskTemplate]:
			UpperCamelCase__    :		Any     =				task_template_dict.get('task' )
			if task_name is None:
						logger.warning(F"""Couldn't find template for task '{task_name}'. Available templates: {list(__SCREAMING_SNAKE_CASE )}""" )
						return None
			UpperCamelCase__    :		Any     =				NAMEaTEMPLATE.get(__SCREAMING_SNAKE_CASE )
			return template.from_dict(__SCREAMING_SNAKE_CASE )



