






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

A_     =				'''\
    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.
'''

A_     =				'''
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}
'''

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

try:
						from jiwer import transforms as tr

						A_     =				True
except ImportError:
						A_     =				False

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






						class 				__SCREAMING_SNAKE_CASE    (					tr.AbstractTransform		):


													def __init__(      self   :    List[str]      ,     snake_case   :    str = " "				):
																			'''simple docstring'''





																			A__    :	Optional[int]			=	sentence_delimiter


													def     _UpperCamelCase							(      self   :    List[str]      ,     snake_case   :    str				):
																			'''simple docstring'''





																			return list(snake_case				)


													def     _UpperCamelCase							(      self   :    Optional[Any]      ,     snake_case   :    List[str]				):
																			'''simple docstring'''





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




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




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




def   _lowerCAmelCase		(				UpperCAmelCase__							:       List[str],    UpperCAmelCase__							:       List[str]   )		->Union[str, Any]:
						return {
						    "f1": float(fa_score(y_true=UpperCAmelCase__,    y_pred=UpperCAmelCase__,    average="""macro"""   )   ),
						    "accuracy": simple_accuracy(UpperCAmelCase__,    UpperCAmelCase__   ),
						}




def   _lowerCAmelCase		(				UpperCAmelCase__							:       str,    UpperCAmelCase__							:       Optional[int],    UpperCAmelCase__							:       Tuple="exp",    UpperCAmelCase__							:       Optional[int]=None,    UpperCAmelCase__							:       List[str]=False,    UpperCAmelCase__							:       Tuple=False,    UpperCAmelCase__							:       List[str]=None,    UpperCAmelCase__							:       List[Any]=False,    )		->Optional[int]:
						# xtreme-s can only have one label
						A__    :	List[Any]			=	[[label] for label in labels]
						A__    :	Any			=	list(UpperCAmelCase__   )
						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__    :	Tuple			=	len(labels[0]   )
						if any(len(UpperCAmelCase__   ) != references_per_prediction for refs in labels   ):
												raise ValueError("""Sacrebleu requires the same number of references for each prediction"""   )
						A__    :	Dict			=	[[refs[i] for refs in labels] for i in range(UpperCAmelCase__   )]
						A__    :	Dict			=	scb.corpus_bleu(
						    UpperCAmelCase__,    UpperCAmelCase__,    smooth_method=UpperCAmelCase__,    smooth_value=UpperCAmelCase__,    force=UpperCAmelCase__,    lowercase=UpperCAmelCase__,    use_effective_order=UpperCAmelCase__,    **({"""tokenize""": tokenize} if tokenize else {}),    )
						return {"bleu": output.score}




def   _lowerCAmelCase		(				UpperCAmelCase__							:       Dict,    UpperCAmelCase__							:       Optional[Any],    UpperCAmelCase__							:       Optional[Any],    UpperCAmelCase__							:       Any   )		->List[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__    :	int			=	compute_measures(UpperCAmelCase__,    UpperCAmelCase__   )["""wer"""]

												A__    :	str			=	compute_measures(UpperCAmelCase__,    UpperCAmelCase__,    truth_transform=UpperCAmelCase__,    hypothesis_transform=UpperCAmelCase__   )["""wer"""]
												return {"wer": wer, "cer": cer}
						else:

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

												return {"wer": compute_score(UpperCAmelCase__,    UpperCAmelCase__,    """wer"""   ), "cer": compute_score(UpperCAmelCase__,    UpperCAmelCase__,    """cer"""   )}






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


							def     _UpperCamelCase							(      self   :    Dict				):
													'''simple docstring'''





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

													A__    :	List[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(snake_case				), """references""": datasets.Value(snake_case				)}				)      ,     codebase_urls=[]      ,     reference_urls=[]      ,     format="""numpy"""      ,     )


							def     _UpperCamelCase							(      self   :    List[Any]      ,     snake_case   :    Any      ,     snake_case   :    Dict      ,     snake_case   :    Optional[Any]=None      ,     snake_case   :    int=None				):
													'''simple docstring'''





													A__    :	int			=	bleu_kwargs if bleu_kwargs is not None else {}
													A__    :	Optional[Any]			=	wer_kwargs if wer_kwargs is not None else {}

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














"""simple docstring"""



import importlib
import inspect
from functools import wraps
from typing import TYPE_CHECKING, Optional, Union

from .download.streaming_download_manager import (
    xbasename,
    xdirname,
    xet_parse,
    xexists,
    xgetsize,
    xglob,
    xgzip_open,
    xisdir,
    xisfile,
    xjoin,
    xlistdir,
    xnumpy_load,
    xopen,
    xpandas_read_csv,
    xpandas_read_excel,
    xPath,
    xrelpath,
    xsio_loadmat,
    xsplit,
    xsplitext,
    xwalk,
    xxml_dom_minidom_parse,
)
from .utils.logging import get_logger
from .utils.patching import patch_submodule
from .utils.py_utils import get_imports


A_     =				get_logger(__name__)


if TYPE_CHECKING:
						from .builder import DatasetBuilder




def   _lowerCAmelCase		(				UpperCAmelCase__							:       Optional[int],    UpperCAmelCase__							:       Optional[Union[str, bool]] = None   )		->Optional[Any]:
						A__    :	int			=	importlib.import_module(UpperCAmelCase__   )

						# TODO(QL): always update the module to add subsequent new authentication
						if hasattr(UpperCAmelCase__,    """_patched_for_streaming"""   ) and module._patched_for_streaming:
												return

						def wrap_auth(UpperCAmelCase__							:       List[str]   ):
												@wraps(UpperCAmelCase__   )
												def wrapper(*UpperCAmelCase__							:       Dict,    **UpperCAmelCase__							:       Dict   ):
																		return function(*UpperCAmelCase__,    use_auth_token=UpperCAmelCase__,    **UpperCAmelCase__   )

												A__    :	List[Any]			=	"""wrap_auth"""
												return wrapper

						# open files in a streaming fashion
						patch_submodule(UpperCAmelCase__,    """open""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """os.listdir""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """os.walk""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """glob.glob""",    wrap_auth(UpperCAmelCase__   )   ).start()
						# allow to navigate in remote zip files
						patch_submodule(UpperCAmelCase__,    """os.path.join""",    UpperCAmelCase__   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.dirname""",    UpperCAmelCase__   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.basename""",    UpperCAmelCase__   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.relpath""",    UpperCAmelCase__   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.split""",    UpperCAmelCase__   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.splitext""",    UpperCAmelCase__   ).start()
						# allow checks on paths
						patch_submodule(UpperCAmelCase__,    """os.path.exists""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.isdir""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.isfile""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.getsize""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """pathlib.Path""",    UpperCAmelCase__   ).start()
						# file readers
						patch_submodule(UpperCAmelCase__,    """gzip.open""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """numpy.load""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """pandas.read_csv""",    wrap_auth(UpperCAmelCase__   ),    attrs=["""__version__"""]   ).start()
						patch_submodule(UpperCAmelCase__,    """pandas.read_excel""",    wrap_auth(UpperCAmelCase__   ),    attrs=["""__version__"""]   ).start()
						patch_submodule(UpperCAmelCase__,    """scipy.io.loadmat""",    wrap_auth(UpperCAmelCase__   ),    attrs=["""__version__"""]   ).start()
						patch_submodule(UpperCAmelCase__,    """xml.etree.ElementTree.parse""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """xml.dom.minidom.parse""",    wrap_auth(UpperCAmelCase__   )   ).start()
						A__    :	Any			=	True




def   _lowerCAmelCase		(				UpperCAmelCase__							:       "DatasetBuilder"   )		->Tuple:
						extend_module_for_streaming(builder.__module__,    use_auth_token=builder.use_auth_token   )
						# if needed, we also have to extend additional internal imports (like wmt14 -> wmt_utils)
						if not builder.__module__.startswith("""datasets."""   ):  # check that it's not a packaged builder like csv
												for imports in get_imports(inspect.getfile(builder.__class__   )   ):
																		if imports[0] == "internal":
																								A__    :	Any			=	imports[1]
																								A__    :	Any			=	""".""".join(builder.__module__.split("""."""   )[:-1] + [internal_import_name]   )
																								extend_module_for_streaming(UpperCAmelCase__,    use_auth_token=builder.use_auth_token   )

    # builders can inherit from other builders that might use streaming functionality
    # (for example, ImageFolder and AudioFolder inherit from FolderBuilder which implements examples generation)
    # but these parents builders are not patched automatically as they are not instantiated, so we patch them here
						from .builder import DatasetBuilder

						A__    :	Any			=	[
						    cls.__module__
						    for cls in type(UpperCAmelCase__   ).__mro__[1:]  # make sure it's not the same module we've already patched
						    if issubclass(cls,    UpperCAmelCase__   ) and cls.__module__ != DatasetBuilder.__module__
						]  # check it's not a standard builder from datasets.builder
						for module in parent_builder_modules:
												extend_module_for_streaming(UpperCAmelCase__,    use_auth_token=builder.use_auth_token   )






