

'''simple docstring'''




from typing import List

from packaging import version
from sklearn.metrics import fa_score

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


# TODO(Patrick/Anton)
snake_case__          =						"""\
"""

snake_case__          =						"""\
    XTREME-S is a benchmark to evaluate universal cross-lingual speech representations in many languages.
    XTREME-S covers four task families: speech recognition, classification, speech-to-text translation and retrieval.
"""

snake_case__          =						"""
Compute XTREME-S evaluation metric associated to each XTREME-S dataset.
Args:
    predictions: list of predictions to score.
        Each translation should be tokenized into a list of tokens.
    references: list of lists of references for each translation.
        Each reference should be tokenized into a list of tokens.
    bleu_kwargs: optional dict of keywords to be passed when computing 'bleu'.
        Keywords include Dict can be one of 'smooth_method', 'smooth_value', 'force', 'lowercase',
        'tokenize', 'use_effective_order'.
    wer_kwargs: optional dict of keywords to be passed when computing 'wer' and 'cer'.
        Keywords include 'concatenate_texts'.
Returns: depending on the XTREME-S task, one or several of:
    \"accuracy\": Accuracy - for 'fleurs-lang_id', 'minds14'
    \"f1\": F1 score - for 'minds14'
    \"wer\": Word error rate - for 'mls', 'fleurs-asr', 'voxpopuli', 'babel'
    \"cer\": Character error rate - for 'mls', 'fleurs-asr', 'voxpopuli', 'babel'
    \"bleu\": BLEU score according to the `sacrebleu` metric - for 'covost2'
Examples:

    >>> xtreme_s_metric = datasets.load_metric('xtreme_s', 'mls')  # 'mls', 'voxpopuli', 'fleurs-asr' or 'babel'
    >>> references = [\"it is sunny here\", \"paper and pen are essentials\"]
    >>> predictions = [\"it's sunny\", \"paper pen are essential\"]
    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)
    >>> print({k: round(v, 2) for k, v in results.items()})
    {'wer': 0.56, 'cer': 0.27}

    >>> xtreme_s_metric = datasets.load_metric('xtreme_s', 'covost2')
    >>> references = [\"bonjour paris\", \"il est necessaire de faire du sport de temps en temp\"]
    >>> predictions = [\"bonjour paris\", \"il est important de faire du sport souvent\"]
    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)
    >>> print({k: round(v, 2) for k, v in results.items()})
    {'bleu': 31.65}

    >>> xtreme_s_metric = datasets.load_metric('xtreme_s', 'fleurs-lang_id')
    >>> references = [0, 1, 0, 0, 1]
    >>> predictions = [0, 1, 1, 0, 0]
    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)
    >>> print({k: round(v, 2) for k, v in results.items()})
    {'accuracy': 0.6}

    >>> xtreme_s_metric = datasets.load_metric('xtreme_s', 'minds14')
    >>> references = [0, 1, 0, 0, 1]
    >>> predictions = [0, 1, 1, 0, 0]
    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)
    >>> print({k: round(v, 2) for k, v in results.items()})
    {'f1': 0.58, 'accuracy': 0.6}
"""

snake_case__          =						["""fleurs-asr""", """mls""", """voxpopuli""", """babel""", """covost2""", """fleurs-lang_id""", """minds14"""]
snake_case__          =						""""""

try:
							from jiwer import transforms as tr

							snake_case__          =						True
except ImportError:
							snake_case__          =						False

if _jiwer_available and version.parse(importlib_metadata.version("""jiwer""")) < version.parse("""2.3.0"""):






							class     UpperCamelCase_     (tr.AbstractTransform     ):







													"""simple docstring"""


													def __init__(     self      :       Optional[Any]							, _lowerCamelCase      :       str = " "    ):





																		"""simple docstring"""







																		A_ :     Any =							sentence_delimiter







													def       _a    (     self      :       Optional[int]							, _lowerCamelCase      :       str    ):





																		"""simple docstring"""







																		return list(_lowerCamelCase    )







													def       _a    (     self      :       Tuple							, _lowerCamelCase      :       List[str]    ):





																		"""simple docstring"""







																		A_ :     Any =							[]
																		for sent_idx, sentence in enumerate(_lowerCamelCase    ):
																							chars.extend(self.process_string(_lowerCamelCase    )    )
																							if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(_lowerCamelCase    ) - 1:
																												chars.append(self.sentence_delimiter    )
																		return chars






							snake_case__          =						tr.Compose(
							    [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
							)
elif _jiwer_available:
							snake_case__          =						tr.Compose(
							    [
							        tr.RemoveMultipleSpaces(),
							        tr.Strip(),
							        tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
							        tr.ReduceToListOfListOfChars(),
							    ]
							)
else:
							snake_case__          =						None




def 						snake_case__    ( lowerCamelCase__				:   Optional[Any]			,		lowerCamelCase__				:   Optional[Any]			)   ->   Union[str, Any]:
					return float((preds == labels).mean()			)




def 						snake_case__    ( lowerCamelCase__				:   Dict			,		lowerCamelCase__				:   List[str]			)   ->   Dict:
					return {
					    "f1": float(fa_score(y_true=lowerCamelCase__			,		y_pred=lowerCamelCase__			,		average='''macro'''			)			),
					    "accuracy": simple_accuracy(lowerCamelCase__			,		lowerCamelCase__			),
					}




def 						snake_case__    ( lowerCamelCase__				:   int			,		lowerCamelCase__				:   int			,		lowerCamelCase__				:   List[str]="exp"			,		lowerCamelCase__				:   List[str]=None			,		lowerCamelCase__				:   List[str]=False			,		lowerCamelCase__				:   List[str]=False			,		lowerCamelCase__				:   Union[str, Any]=None			,		lowerCamelCase__				:   Any=False			,		)   ->   List[str]:
					# xtreme-s can only have one label
					A_ :     str =							[[label] for label in labels]
					A_ :     Optional[Any] =							list(lowerCamelCase__			)
					try:
										import sacrebleu as scb
					except ImportError:
										raise ValueError(
										    '''sacrebleu has to be installed in order to apply the bleu metric for covost2.'''
										    '''You can install it via `pip install sacrebleu`.'''			)

					if version.parse(scb.__version__			) < version.parse('''1.4.12'''			):
										raise ImportWarning(
										    '''To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn\'t match this condition.\n'''
										    '''You can install it with `pip install "sacrebleu>=1.4.12"`.'''			)

					A_ :     str =							len(labels[0]			)
					if any(len(lowerCamelCase__			) != references_per_prediction for refs in labels			):
										raise ValueError('''Sacrebleu requires the same number of references for each prediction'''			)
					A_ :     Optional[int] =							[[refs[i] for refs in labels] for i in range(lowerCamelCase__			)]
					A_ :     Any =							scb.corpus_bleu(
					    lowerCamelCase__			,		lowerCamelCase__			,		smooth_method=lowerCamelCase__			,		smooth_value=lowerCamelCase__			,		force=lowerCamelCase__			,		lowercase=lowerCamelCase__			,		use_effective_order=lowerCamelCase__			,		**({'''tokenize''': tokenize} if tokenize else {})			,		)
					return {"bleu": output.score}




def 						snake_case__    ( lowerCamelCase__				:   Tuple			,		lowerCamelCase__				:   int			,		lowerCamelCase__				:   Dict			,		lowerCamelCase__				:   Tuple			)   ->   Optional[Any]:
					try:
										from jiwer import compute_measures
					except ImportError:
										raise ValueError(
										    f'jiwer has to be installed in order to apply the wer metric for {config_name}.'
										    '''You can install it via `pip install jiwer`.'''			)

					if concatenate_texts:
										A_ :     Any =							compute_measures(lowerCamelCase__			,		lowerCamelCase__			)['''wer''']

										A_ :     List[Any] =							compute_measures(lowerCamelCase__			,		lowerCamelCase__			,		truth_transform=lowerCamelCase__			,		hypothesis_transform=lowerCamelCase__			)['''wer''']
										return {"wer": wer, "cer": cer}
					else:

										def compute_score(lowerCamelCase__				:   Optional[int]			,		lowerCamelCase__				:   List[Any]			,		lowerCamelCase__				:   int="wer"			):
															A_ :     Any =							0
															A_ :     List[Any] =							0
															for prediction, reference in zip(lowerCamelCase__			,		lowerCamelCase__			):
																				if score_type == "wer":
																									A_ :     Any =							compute_measures(lowerCamelCase__			,		lowerCamelCase__			)
																				elif score_type == "cer":
																									A_ :     str =							compute_measures(
																									    lowerCamelCase__			,		lowerCamelCase__			,		truth_transform=lowerCamelCase__			,		hypothesis_transform=lowerCamelCase__			)
																				incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
																				total += measures["substitutions"] + measures["deletions"] + measures["hits"]
															return incorrect / total

										return {"wer": compute_score(lowerCamelCase__			,		lowerCamelCase__			,		'''wer'''			), "cer": compute_score(lowerCamelCase__			,		lowerCamelCase__			,		'''cer'''			)}






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







						"""simple docstring"""


						def       _a    (     self      :       Optional[Any]    ):





											"""simple docstring"""







											if self.config_name not in _CONFIG_NAMES:
																raise KeyError(f'You should supply a configuration name selected in {_CONFIG_NAMES}'    )

											A_ :     Any =							'''int64''' if self.config_name in ['''fleurs-lang_id''', '''minds14'''] else '''string'''

											return datasets.MetricInfo(
											    description=_DESCRIPTION							, citation=_CITATION							, inputs_description=_KWARGS_DESCRIPTION							, features=datasets.Features(
											        {'''predictions''': datasets.Value(_lowerCamelCase    ), '''references''': datasets.Value(_lowerCamelCase    )}    )							, codebase_urls=[]							, reference_urls=[]							, format='''numpy'''							, )







						def       _a    (     self      :       Union[str, Any]							, _lowerCamelCase      :       Dict							, _lowerCamelCase      :       List[str]							, _lowerCamelCase      :       List[Any]=None							, _lowerCamelCase      :       str=None    ):





											"""simple docstring"""







											A_ :     Dict =							bleu_kwargs if bleu_kwargs is not None else {}
											A_ :     Any =							wer_kwargs if wer_kwargs is not None else {}

											if self.config_name == "fleurs-lang_id":
																return {"accuracy": simple_accuracy(_lowerCamelCase							, _lowerCamelCase    )}
											elif self.config_name == "minds14":
																return fa_and_simple_accuracy(_lowerCamelCase							, _lowerCamelCase    )
											elif self.config_name == "covost2":
																A_ :     int =							bleu_kwargs.pop('''smooth_method'''							, '''exp'''    )
																A_ :     List[Any] =							bleu_kwargs.pop('''smooth_value'''							, _lowerCamelCase    )
																A_ :     str =							bleu_kwargs.pop('''force'''							, _lowerCamelCase    )
																A_ :     Union[str, Any] =							bleu_kwargs.pop('''lowercase'''							, _lowerCamelCase    )
																A_ :     int =							bleu_kwargs.pop('''tokenize'''							, _lowerCamelCase    )
																A_ :     List[str] =							bleu_kwargs.pop('''use_effective_order'''							, _lowerCamelCase    )
																return bleu(
																    preds=_lowerCamelCase							, labels=_lowerCamelCase							, smooth_method=_lowerCamelCase							, smooth_value=_lowerCamelCase							, force=_lowerCamelCase							, lowercase=_lowerCamelCase							, tokenize=_lowerCamelCase							, use_effective_order=_lowerCamelCase							, )
											elif self.config_name in ["fleurs-asr", "mls", "voxpopuli", "babel"]:
																A_ :     List[str] =							wer_kwargs.pop('''concatenate_texts'''							, _lowerCamelCase    )
																return wer_and_cer(_lowerCamelCase							, _lowerCamelCase							, _lowerCamelCase							, self.config_name    )
											else:
																raise KeyError(f'You should supply a configuration name selected in {_CONFIG_NAMES}'    )









'''simple docstring'''




def 						snake_case__    ( lowerCamelCase__				:   float			)   ->   float:
					return -num if num < 0 else num




def 						snake_case__    ( lowerCamelCase__				:   list[int]			)   ->   int:
					if len(lowerCamelCase__			) == 0:
										raise ValueError('''abs_min() arg is an empty sequence'''			)
					A_ :     Any =							x[0]
					for i in x:
										if abs_val(lowerCamelCase__			) < abs_val(lowerCamelCase__			):
															A_ :     Tuple =							i
					return j




def 						snake_case__    ( lowerCamelCase__				:   list[int]			)   ->   int:
					if len(lowerCamelCase__			) == 0:
										raise ValueError('''abs_max() arg is an empty sequence'''			)
					A_ :     List[str] =							x[0]
					for i in x:
										if abs(lowerCamelCase__			) > abs(lowerCamelCase__			):
															A_ :     Any =							i
					return j




def 						snake_case__    ( lowerCamelCase__				:   list[int]			)   ->   int:
					if len(lowerCamelCase__			) == 0:
										raise ValueError('''abs_max_sort() arg is an empty sequence'''			)
					return sorted(lowerCamelCase__			,		key=lowerCamelCase__			)[-1]




def 						snake_case__    ( )   ->   Optional[Any]:
					assert abs_val(0			) == 0
					assert abs_val(3_4			) == 3_4
					assert abs_val(-1_0_0_0_0_0_0_0_0_0_0_0			) == 1_0_0_0_0_0_0_0_0_0_0_0

					A_ :     Dict =							[-3, -1, 2, -1_1]
					assert abs_max(lowerCamelCase__			) == -1_1
					assert abs_max_sort(lowerCamelCase__			) == -1_1
					assert abs_min(lowerCamelCase__			) == -1


if __name__ == "__main__":
							import doctest

							doctest.testmod()

							test_abs_val()
							print(abs_val(-34))  # --> 34



