





from typing import List

import jiwer
import jiwer.transforms as tr
from packaging import version

import datasets
from datasets.config import PY_VERSION


if PY_VERSION < version.parse('''3.8'''):
				import importlib_metadata
else:
				import importlib.metadata as importlib_metadata


UpperCAmelCase		:  List[Any]         =	''''''


if version.parse(importlib_metadata.version('''jiwer''')) < version.parse('''2.3.0'''):





				class 		_A(   tr.AbstractTransform  ):


								"""simple docstring"""


								def __init__(     self	,   _A = " "	):
													__A      :		List[Any]         =							sentence_delimiter



								def    UpperCAmelCase_						(     self	,   _A	):
													return list(_A	)





								def    UpperCAmelCase_						(     self	,   _A	):
													__A      :		str         =							[]
													for sent_idx, sentence in enumerate(_A	):
																		chars.extend(self.process_string(_A	)	)
																		if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(_A	) - 1:
																							chars.append(self.sentence_delimiter	)
													return chars




				UpperCAmelCase		:  Optional[Any]         =	tr.Compose(
				    [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
				)
else:
				UpperCAmelCase		:  Optional[int]         =	tr.Compose(
				    [
				        tr.RemoveMultipleSpaces(),
				        tr.Strip(),
				        tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
				        tr.ReduceToListOfListOfChars(),
				    ]
				)


UpperCAmelCase		:  str         =	'''\
@inproceedings{inproceedings,
    author = {Morris, Andrew and Maier, Viktoria and Green, Phil},
    year = {2004},
    month = {01},
    pages = {},
    title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.}
}
'''

UpperCAmelCase		:  List[str]         =	'''\
Character error rate (CER) is a common metric of the performance of an automatic speech recognition system.

CER is similar to Word Error Rate (WER), but operates on character instead of word. Please refer to docs of WER for further information.

Character error rate can be computed as:

CER = (S + D + I) / N = (S + D + I) / (S + D + C)

where

S is the number of substitutions,
D is the number of deletions,
I is the number of insertions,
C is the number of correct characters,
N is the number of characters in the reference (N=S+D+C).

CER\'s output is not always a number between 0 and 1, in particular when there is a high number of insertions. This value is often associated to the percentage of characters that were incorrectly predicted. The lower the value, the better the
performance of the ASR system with a CER of 0 being a perfect score.
'''

UpperCAmelCase		:  str         =	'''
Computes CER score of transcribed segments against references.
Args:
    references: list of references for each speech input.
    predictions: list of transcribtions to score.
    concatenate_texts: Whether or not to concatenate sentences before evaluation, set to True for more accurate result.
Returns:
    (float): the character error rate

Examples:

    >>> predictions = ["this is the prediction", "there is an other sample"]
    >>> references = ["this is the reference", "there is another one"]
    >>> cer = datasets.load_metric("cer")
    >>> cer_score = cer.compute(predictions=predictions, references=references)
    >>> print(cer_score)
    0.34146341463414637
'''





@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION							,					_KWARGS_DESCRIPTION  )
class 		_A(   datasets.Metric  ):


				"""simple docstring"""


				def    UpperCAmelCase_						(     self	):
									return datasets.MetricInfo(
									    description=_DESCRIPTION	,   citation=_CITATION	,   inputs_description=_KWARGS_DESCRIPTION	,   features=datasets.Features(
									        {
									            'predictions': datasets.Value('string'	,   id='sequence'	),
									            'references': datasets.Value('string'	,   id='sequence'	),
									        }	)	,   codebase_urls=['https://github.com/jitsi/jiwer/']	,   reference_urls=[
									        'https://en.wikipedia.org/wiki/Word_error_rate',
									        'https://sites.google.com/site/textdigitisation/qualitymeasures/computingerrorrates',
									    ]	,   )





				def    UpperCAmelCase_						(     self	,   _A	,   _A	,   _A=False	):
									if concatenate_texts:
														return jiwer.compute_measures(
														    _A	,   _A	,   truth_transform=_A	,   hypothesis_transform=_A	,   )["wer"]

									__A      :		Union[str, Any]         =							0
									__A      :		Tuple         =							0
									for prediction, reference in zip(_A	,   _A	):
														__A      :		int         =							jiwer.compute_measures(
														    _A	,   _A	,   truth_transform=_A	,   hypothesis_transform=_A	,   )
														incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
														total += measures["substitutions"] + measures["deletions"] + measures["hits"]

									return incorrect / total






import unittest

from transformers import AlbertTokenizer, AlbertTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


__A							=get_tests_dir('''fixtures/spiece.model''')




@require_sentencepiece
@require_tokenizers
class        _SCREAMING_SNAKE_CASE	( snake_case_     ,			unittest.TestCase		):
	lowerCAmelCase__   =					AlbertTokenizer
	lowerCAmelCase__   =					AlbertTokenizerFast
	lowerCAmelCase__   =					True
	lowerCAmelCase__   =					True
	lowerCAmelCase__   =					True



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Any:
					super().setUp()

					# We have a SentencePiece fixture for testing
					lowerCamelCase_         =     AlbertTokenizer(lowercase					)
					tokenizer.save_pretrained(self.tmpdirname					)



	def        SCREAMING_SNAKE_CASE_(       self		,    lowercase					)							->			str:
					lowerCamelCase_         =     "this is a test"
					lowerCamelCase_         =     "this is a test"
					return input_text, output_text



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Dict:
					lowerCamelCase_         =     "<pad>"
					lowerCamelCase_         =     0

					self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowercase					)		,    lowercase					)
					self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowercase					)		,    lowercase					)



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Any:
					lowerCamelCase_         =     list(self.get_tokenizer().get_vocab().keys()					)

					self.assertEqual(vocab_keys[0]		,    "<pad>"					)
					self.assertEqual(vocab_keys[1]		,    "<unk>"					)
					self.assertEqual(vocab_keys[-1]		,    "▁eloquent"					)
					self.assertEqual(len(lowercase					)		,    30000					)



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Union[str, Any]:
					self.assertEqual(self.get_tokenizer().vocab_size		,    30000					)



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Union[str, Any]:
					if not self.test_rust_tokenizer:
									return

					lowerCamelCase_         =     self.get_tokenizer()
					lowerCamelCase_         =     self.get_rust_tokenizer()

					lowerCamelCase_         =     "I was born in 92000, and this is falsé."

					lowerCamelCase_         =     tokenizer.tokenize(lowercase					)
					lowerCamelCase_         =     rust_tokenizer.tokenize(lowercase					)
					self.assertListEqual(lowercase		,    lowercase					)

					lowerCamelCase_         =     tokenizer.encode(lowercase		,    add_special_tokens=lowercase					)
					lowerCamelCase_         =     rust_tokenizer.encode(lowercase		,    add_special_tokens=lowercase					)
					self.assertListEqual(lowercase		,    lowercase					)

					lowerCamelCase_         =     self.get_rust_tokenizer()
					lowerCamelCase_         =     tokenizer.encode(lowercase					)
					lowerCamelCase_         =     rust_tokenizer.encode(lowercase					)
					self.assertListEqual(lowercase		,    lowercase					)



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Optional[Any]:
					lowerCamelCase_         =     AlbertTokenizer(lowercase		,    keep_accents=lowercase					)

					lowerCamelCase_         =     tokenizer.tokenize("This is a test"					)
					self.assertListEqual(lowercase		,    ["▁this", "▁is", "▁a", "▁test"]					)

					self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase					)		,    [48, 25, 21, 1289]					)

					lowerCamelCase_         =     tokenizer.tokenize("I was born in 92000, and this is falsé."					)
					self.assertListEqual(
					    lowercase		,    ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "é", "."]					)
					lowerCamelCase_         =     tokenizer.convert_tokens_to_ids(lowercase					)
					self.assertListEqual(lowercase		,    [31, 23, 386, 19, 561, 3050, 15, 17, 48, 25, 8256, 18, 1, 9]					)

					lowerCamelCase_         =     tokenizer.convert_ids_to_tokens(lowercase					)
					self.assertListEqual(
					    lowercase		,    ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "."]		,    )



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Any:
					lowerCamelCase_         =     AlbertTokenizer(lowercase					)

					lowerCamelCase_         =     tokenizer.encode("sequence builders"					)
					lowerCamelCase_         =     tokenizer.encode("multi-sequence build"					)

					lowerCamelCase_         =     tokenizer.build_inputs_with_special_tokens(lowercase					)
					lowerCamelCase_         =     tokenizer.build_inputs_with_special_tokens(lowercase		,    lowercase					)

					assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
					assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [
					    tokenizer.sep_token_id
					]



	@slow
	def        SCREAMING_SNAKE_CASE_(       self					)							->			Optional[Any]:
					# fmt: off
					lowerCamelCase_         =     {"attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "input_ids": [[2, 21970, 13, 5, 6092, 167, 28, 7103, 2153, 673, 8, 7028, 12051, 18, 17, 7103, 2153, 673, 8, 3515, 18684, 8, 4461, 6, 1927, 297, 8, 12060, 2607, 18, 13, 5, 4461, 15, 10538, 38, 8, 135, 15, 822, 58, 15, 993, 10363, 15, 1460, 8005, 4461, 15, 993, 255, 2328, 9, 9, 9, 6, 26, 1112, 816, 3260, 13, 5, 103, 2377, 6, 17, 1112, 816, 2782, 13, 5, 103, 10641, 6, 29, 84, 2512, 2430, 782, 18684, 2761, 19, 808, 2430, 2556, 17, 855, 1480, 9477, 4091, 128, 11712, 15, 7103, 2153, 673, 17, 24883, 9990, 9, 3], [2, 11502, 25, 1006, 20, 782, 8, 11809, 855, 1732, 19393, 18667, 37, 367, 21018, 69, 1854, 34, 11860, 19124, 27, 156, 225, 17, 193, 4141, 19, 65, 9124, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 14, 2231, 886, 2385, 17659, 84, 14, 16792, 1952, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "token_type_ids": [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
					# fmt: on

					self.tokenizer_integration_test_util(
					    expected_encoding=lowercase		,    model_name="albert-base-v2"		,    revision="6b6560eaf5ff2e250b00c50f380c5389a9c2d82e"		,    )


