






"""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)
__SCREAMING_SNAKE_CASE      =      '\\n'

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

__SCREAMING_SNAKE_CASE      =      '\nCompute XTREME-S evaluation metric associated to each XTREME-S dataset.\nArgs:\n    predictions: list of predictions to score.\n        Each translation should be tokenized into a list of tokens.\n    references: list of lists of references for each translation.\n        Each reference should be tokenized into a list of tokens.\n    bleu_kwargs: optional dict of keywords to be passed when computing \'bleu\'.\n        Keywords include Dict can be one of \'smooth_method\', \'smooth_value\', \'force\', \'lowercase\',\n        \'tokenize\', \'use_effective_order\'.\n    wer_kwargs: optional dict of keywords to be passed when computing \'wer\' and \'cer\'.\n        Keywords include \'concatenate_texts\'.\nReturns: depending on the XTREME-S task, one or several of:\n    "accuracy": Accuracy - for \'fleurs-lang_id\', \'minds14\'\n    "f1": F1 score - for \'minds14\'\n    "wer": Word error rate - for \'mls\', \'fleurs-asr\', \'voxpopuli\', \'babel\'\n    "cer": Character error rate - for \'mls\', \'fleurs-asr\', \'voxpopuli\', \'babel\'\n    "bleu": BLEU score according to the `sacrebleu` metric - for \'covost2\'\nExamples:\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'mls\')  # \'mls\', \'voxpopuli\', \'fleurs-asr\' or \'babel\'\n    >>> references = ["it is sunny here", "paper and pen are essentials"]\n    >>> predictions = ["it\'s sunny", "paper pen are essential"]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'wer\': 0.56, \'cer\': 0.27}\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'covost2\')\n    >>> references = ["bonjour paris", "il est necessaire de faire du sport de temps en temp"]\n    >>> predictions = ["bonjour paris", "il est important de faire du sport souvent"]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'bleu\': 31.65}\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'fleurs-lang_id\')\n    >>> references = [0, 1, 0, 0, 1]\n    >>> predictions = [0, 1, 1, 0, 0]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'accuracy\': 0.6}\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'minds14\')\n    >>> references = [0, 1, 0, 0, 1]\n    >>> predictions = [0, 1, 1, 0, 0]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'f1\': 0.58, \'accuracy\': 0.6}\n'

__SCREAMING_SNAKE_CASE      =      ['fleurs-asr', 'mls', 'voxpopuli', 'babel', 'covost2', 'fleurs-lang_id', 'minds14']
__SCREAMING_SNAKE_CASE      =      ''

try:
		from jiwer import transforms as tr

		__SCREAMING_SNAKE_CASE      =      True
except ImportError:
		__SCREAMING_SNAKE_CASE      =      False

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




		class 	__UpperCamelCase    (    tr.AbstractTransform	):
							def __init__(       self       :  str    ,   UpperCAmelCase       :  str = " "      )  ->					str:
													lowerCAmelCase      :Dict				  = sentence_delimiter
							def 					UpperCAmelCase__   (       self       :  Union[str, Any]    ,   UpperCAmelCase       :  str      )  ->					Any:
													return list(UpperCAmelCase      )
							def 					UpperCAmelCase__   (       self       :  Optional[Any]    ,   UpperCAmelCase       :  List[str]      )  ->					Dict:
													lowerCAmelCase      :Tuple				  = []
													for sent_idx, sentence in enumerate(UpperCAmelCase      ):
																			chars.extend(self.process_string(UpperCAmelCase      )      )
																			if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(UpperCAmelCase      ) - 1:
																									chars.append(self.sentence_delimiter      )
													return chars


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


def 					UpperCAmelCase			(				a__    ,			a__   )     ->   List[Any]:





						'''simple docstring'''






						return float((preds == labels).mean()   )


def 					UpperCAmelCase			(				a__    ,			a__   )     ->   Any:





						'''simple docstring'''






						return {
						    "f1": float(fa_score(y_true=a__    ,			y_pred=a__    ,			average='macro'   )   ),
						    "accuracy": simple_accuracy(a__    ,			a__   ),
						}


def 					UpperCAmelCase			(				a__    ,			a__    ,			a__="exp"    ,			a__=None    ,			a__=False    ,			a__=False    ,			a__=None    ,			a__=False    ,			)     ->   int:





						'''simple docstring'''






						lowerCAmelCase      :List[str]				  = [[label] for label in labels]
						lowerCAmelCase      :int				  = list(a__   )
						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"`.'   )

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


def 					UpperCAmelCase			(				a__    ,			a__    ,			a__    ,			a__   )     ->   str:





						'''simple docstring'''






						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:
												lowerCAmelCase      :str				  = compute_measures(a__    ,			a__   )['wer']

												lowerCAmelCase      :Any				  = compute_measures(a__    ,			a__    ,			truth_transform=a__    ,			hypothesis_transform=a__   )['wer']
												return {"wer": wer, "cer": cer}
						else:

												def compute_score(a__    ,			a__    ,			a__="wer"   ):
																		lowerCAmelCase      :int				  = 0
																		lowerCAmelCase      :str				  = 0
																		for prediction, reference in zip(a__    ,			a__   ):
																								if score_type == "wer":
																														lowerCAmelCase      :List[Any]				  = compute_measures(a__    ,			a__   )
																								elif score_type == "cer":
																														lowerCAmelCase      :int				  = 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

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







@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION      ,    _KWARGS_DESCRIPTION	)
class 	__UpperCamelCase    (    datasets.Metric	):
					def 					UpperCAmelCase__   (       self       :  Tuple      )  ->					int:
											if self.config_name not in _CONFIG_NAMES:
																	raise KeyError(f"""You should supply a configuration name selected in {_CONFIG_NAMES}"""      )

											lowerCAmelCase      :Tuple				  = '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(UpperCAmelCase      ), 'references': datasets.Value(UpperCAmelCase      )}      )    ,   codebase_urls=[]    ,   reference_urls=[]    ,   format='numpy'    ,   )
					def 					UpperCAmelCase__   (       self       :  Optional[Any]    ,   UpperCAmelCase       :  Tuple    ,   UpperCAmelCase       :  str    ,   UpperCAmelCase       :  Any=None    ,   UpperCAmelCase       :  Optional[int]=None      )  ->					Optional[Any]:
											lowerCAmelCase      :List[str]				  = bleu_kwargs if bleu_kwargs is not None else {}
											lowerCAmelCase      :List[Any]				  = wer_kwargs if wer_kwargs is not None else {}

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







"""simple docstring"""


import unittest

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


if is_torch_available():
		import torch

		from transformers import XLMProphetNetForConditionalGeneration, XLMProphetNetTokenizer




@require_torch
class 	__UpperCamelCase    (    unittest.TestCase	):
					@slow
					def 					UpperCAmelCase__   (       self       :  Tuple      )  ->					Union[str, Any]:
											lowerCAmelCase      :Any				  = XLMProphetNetForConditionalGeneration.from_pretrained('microsoft/xprophetnet-large-wiki100-cased'      )
											model.to(UpperCAmelCase      )

											# encoder-decoder outputs
											lowerCAmelCase      :Any				  = torch.tensor([[17, 9_6208, 10_3471, 2]]      ).to(UpperCAmelCase      )
											lowerCAmelCase      :Optional[int]				  = torch.tensor(
											    [[2, 250, 9953, 34, 6_9489, 1620, 32, 11_8424, 624, 210, 105, 2913, 1032, 351]]      ).to(UpperCAmelCase      )
											lowerCAmelCase      :Tuple				  = model(
											    input_ids=UpperCAmelCase    ,   attention_mask=UpperCAmelCase    ,   encoder_outputs=UpperCAmelCase    ,   decoder_input_ids=UpperCAmelCase      )
											lowerCAmelCase      :Tuple				  = output[0]
											lowerCAmelCase      :Any				  = torch.Size((1, 14, 25_0012)      )
											self.assertEqual(output_predited_logis.shape    ,   UpperCAmelCase      )
											lowerCAmelCase      :Optional[int]				  = torch.tensor(
											    [[[-6.3_9_8_6, -8.2_3_9_1, 1_2.5_1_8_9], [-6.3_2_8_9, -8.0_8_6_4, 1_2.6_2_1_1], [-6.2_4_1_8, -8.0_4_4_5, 1_2.7_9_6_8]]]      ).to(UpperCAmelCase      )
											self.assertTrue(torch.allclose(output_predited_logis[:, :3, :3]    ,   UpperCAmelCase    ,   atol=1e-4      )      )

											# encoder outputs
											lowerCAmelCase      :int				  = model.prophetnet.encoder(UpperCAmelCase      )[0]
											lowerCAmelCase      :int				  = torch.tensor(
											    [[[-1.4_2_6_0, -0.7_6_2_8, 0.8_4_5_3], [-1.4_7_1_9, -0.1_3_9_1, 0.7_8_0_7], [-1.7_6_7_8, 0.0_1_1_4, 0.4_6_4_6]]]      ).to(UpperCAmelCase      )
											lowerCAmelCase      :Optional[Any]				  = torch.Size((1, 4, 1024)      )
											self.assertEqual(encoder_outputs.shape    ,   UpperCAmelCase      )
											self.assertTrue(torch.allclose(encoder_outputs[:, :3, :3]    ,   UpperCAmelCase    ,   atol=1e-4      )      )

											# decoder outputs
											lowerCAmelCase      :Tuple				  = model.prophetnet.decoder(
											    UpperCAmelCase    ,   encoder_hidden_states=UpperCAmelCase    ,   )
											lowerCAmelCase      :Optional[Any]				  = decoder_outputs[1].view(1    ,   model.config.ngram    ,   14    ,   -1      )
											lowerCAmelCase      :Any				  = model.lm_head(UpperCAmelCase      )
											lowerCAmelCase      :Optional[int]				  = predicting_streams_logits[:, 0]
											self.assertTrue(torch.allclose(next_first_stream_logits[:, :3, :3]    ,   UpperCAmelCase    ,   atol=1e-4      )      )
					@slow
					def 					UpperCAmelCase__   (       self       :  List[Any]      )  ->					Optional[Any]:
											lowerCAmelCase      :Union[str, Any]				  = XLMProphetNetForConditionalGeneration.from_pretrained(
											    'microsoft/xprophetnet-large-wiki100-cased-xglue-ntg'      )
											model.to(UpperCAmelCase      )

											lowerCAmelCase      :Tuple				  = torch.tensor([[17, 9_6208, 10_3471, 2]]      ).to(UpperCAmelCase      )
											lowerCAmelCase      :Any				  = torch.tensor(
											    [[2, 250, 9953, 34, 6_9489, 1620, 32, 11_8424, 624, 210, 105, 2913, 1032, 351]]      ).to(UpperCAmelCase      )
											lowerCAmelCase      :Tuple				  = model(
											    input_ids=UpperCAmelCase    ,   attention_mask=UpperCAmelCase    ,   encoder_outputs=UpperCAmelCase    ,   decoder_input_ids=UpperCAmelCase      )
											lowerCAmelCase      :Dict				  = output[0]
											lowerCAmelCase      :List[str]				  = torch.Size((1, 14, 25_0012)      )
											self.assertEqual(output_predited_logis.shape    ,   UpperCAmelCase      )
											# compare the actual values for a slice.
											lowerCAmelCase      :List[Any]				  = torch.tensor(
											    [[[-9.2_2_5_3, -9.7_1_7_3, -6.3_5_2_9], [-7.6_7_0_1, -9.0_1_4_5, -1.9_3_8_2], [-8.0_1_9_5, -7.0_0_0_4, -0.1_5_2_3]]]      ).to(UpperCAmelCase      )

											self.assertTrue(torch.allclose(output_predited_logis[:, :3, :3]    ,   UpperCAmelCase    ,   atol=1e-4      )      )
					@slow
					def 					UpperCAmelCase__   (       self       :  Optional[Any]      )  ->					Optional[int]:
											lowerCAmelCase      :str				  = XLMProphetNetForConditionalGeneration.from_pretrained(
											    'microsoft/xprophetnet-large-wiki100-cased-xglue-ntg'      )
											model.to(UpperCAmelCase      )
											lowerCAmelCase      :Any				  = 512

											lowerCAmelCase      :Union[str, Any]				  = XLMProphetNetTokenizer.from_pretrained('microsoft/xprophetnet-large-wiki100-cased-xglue-ntg'      )

											lowerCAmelCase      :Optional[int]				  = (
											    'Microsoft Corporation intends to officially end free support for the Windows 7 operating system after'
											    ' January 14, 2020, according to the official portal of the organization. From that day, users of this'
											    ' system will not be able to receive security updates, which could make their computers vulnerable to'
											    ' cyber attacks.'
											)
											lowerCAmelCase      :Union[str, Any]				  = (
											    'орпорация Microsoft намерена официально прекратить бесплатную поддержку операционной системы Windows 7'
											    ' после 14 января 2020 года, сообщается на официальном портале организации . С указанного дня пользователи'
											    ' этой системы не смогут получать обновления безопасности, из-за чего их компьютеры могут стать уязвимыми'
											    ' к кибератакам.'
											)
											lowerCAmelCase      :Optional[Any]				  = (
											    '根据该组织的官方门户网站，微软公司打算在2020年1月14日之后正式终止对Windows 7操作系统的免费支持。从那时起，该系统的用户将无法接收安全更新，这可能会使他们的计算机容易受到网络攻击。'
											)

											lowerCAmelCase      :List[Any]				  = tokenizer(
											    [EN_SENTENCE, RU_SENTENCE, ZH_SENTENCE]    ,   padding=UpperCAmelCase    ,   max_length=255    ,   return_tensors='pt'      ).input_ids
											lowerCAmelCase      :List[str]				  = input_ids.to(UpperCAmelCase      )

											lowerCAmelCase      :Dict				  = model.generate(
											    UpperCAmelCase    ,   num_beams=10    ,   length_penalty=1.0    ,   no_repeat_ngram_size=3    ,   early_stopping=UpperCAmelCase      )
											lowerCAmelCase      :str				  = [tokenizer.decode(UpperCAmelCase    ,   skip_special_tokens=UpperCAmelCase      ) for g in summary_ids]
											lowerCAmelCase      :Dict				  = 'Microsoft to end Windows 7 free support after January 14, 2020'
											lowerCAmelCase      :Tuple				  = 'Microsoft намерена прекратить бесплатную поддержку Windows 7 после 14 января 2020 года'
											lowerCAmelCase      :Any				  = '微软打算终止对Windows 7操作系统的免费支持'
											self.assertListEqual(
											    [EXPECTED_TITLE_EN, EXPECTED_TITLE_RU, EXPECTED_TITLE_ZH]    ,   UpperCAmelCase    ,   )

											lowerCAmelCase      :List[str]				  = model.generate(
											    UpperCAmelCase    ,   num_beams=1    ,   length_penalty=1.0    ,   no_repeat_ngram_size=3    ,   early_stopping=UpperCAmelCase      )
											lowerCAmelCase      :Dict				  = [
											    tokenizer.convert_ids_to_tokens(UpperCAmelCase    ,   skip_special_tokens=UpperCAmelCase      ) for g in summary_ids_beama
											]
											lowerCAmelCase      :Optional[Any]				  = '▁Microsoft ▁to ▁end ▁free ▁support ▁for ▁Windows ▁7'.split(' '      )
											lowerCAmelCase      :Optional[int]				  = '▁Microsoft ▁намерен а ▁прекрати ть ▁бес плат ную ▁поддержку ▁Windows ▁7 ▁после ▁14 ▁января ▁2020 ▁года'.split(
											    ' '      )
											lowerCAmelCase      :Optional[Any]				  = '微软 公司 打算 终止 对 Windows ▁7 操作 系统的 免费 支持'.split(' '      )
											self.assertListEqual(
											    [EXPECTED_TITLE_EN_BEAM1_TOK, EXPECTED_TITLE_RU_BEAM1_TOK, EXPECTED_TITLE_ZH_BEAM1_TOK]    ,   UpperCAmelCase    ,   )