
"""simple docstring"""







import re
import string
from collections import Counter

import sacrebleu
import sacremoses
from packaging import version

import datasets


_SCREAMING_SNAKE_CASE	       =      """
@inproceedings{xu-etal-2016-optimizing,
    title = {Optimizing Statistical Machine Translation for Text Simplification},
    authors={Xu, Wei and Napoles, Courtney and Pavlick, Ellie and Chen, Quanze and Callison-Burch, Chris},
    journal = {Transactions of the Association for Computational Linguistics},
    volume = {4},
    year={2016},
    url = {https://www.aclweb.org/anthology/Q16-1029},
    pages = {401--415
},
@inproceedings{post-2018-call,
    title = \"A Call for Clarity in Reporting {BLEU} Scores\",
    author = \"Post, Matt\",
    booktitle = \"Proceedings of the Third Conference on Machine Translation: Research Papers\",
    month = oct,
    year = \"2018\",
    address = \"Belgium, Brussels\",
    publisher = \"Association for Computational Linguistics\",
    url = \"https://www.aclweb.org/anthology/W18-6319\",
    pages = \"186--191\",
}
"""

_SCREAMING_SNAKE_CASE	       =      """\
WIKI_SPLIT is the combination of three metrics SARI, EXACT and SACREBLEU
It can be used to evaluate the quality of machine-generated texts.
"""


_SCREAMING_SNAKE_CASE	       =      """
Calculates sari score (between 0 and 100) given a list of source and predicted
sentences, and a list of lists of reference sentences. It also computes the BLEU score as well as the exact match score.
Args:
    sources: list of source sentences where each sentence should be a string.
    predictions: list of predicted sentences where each sentence should be a string.
    references: list of lists of reference sentences where each sentence should be a string.
Returns:
    sari: sari score
    sacrebleu: sacrebleu score
    exact: exact score

Examples:
    >>> sources=[\"About 95 species are currently accepted .\"]
    >>> predictions=[\"About 95 you now get in .\"]
    >>> references=[[\"About 95 species are currently known .\"]]
    >>> wiki_split = datasets.load_metric(\"wiki_split\")
    >>> results = wiki_split.compute(sources=sources, predictions=predictions, references=references)
    >>> print(results)
    {'sari': 21.805555555555557, 'sacrebleu': 14.535768424205482, 'exact': 0.0}
"""
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE )  ->     Union[str, Any]:







							"""simple docstring"""


							def remove_articles(SCREAMING_SNAKE_CASE ):
														__snake_case     			=       re.compile(r"\b(a|an|the)\b"							,    re.UNICODE )
														return re.sub(SCREAMING_SNAKE_CASE							,    " "							,    SCREAMING_SNAKE_CASE )

							def white_space_fix(SCREAMING_SNAKE_CASE ):
														return " ".join(text.split() )

							def remove_punc(SCREAMING_SNAKE_CASE ):
														__snake_case     			=       set(string.punctuation )
														return "".join(ch for ch in text if ch not in exclude )

							def lower(SCREAMING_SNAKE_CASE ):
														return text.lower()

							return white_space_fix(remove_articles(remove_punc(lower(SCREAMING_SNAKE_CASE ) ) ) )
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )  ->     Tuple:







							"""simple docstring"""


							return int(normalize_answer(SCREAMING_SNAKE_CASE ) == normalize_answer(SCREAMING_SNAKE_CASE ) )
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )  ->     int:







							"""simple docstring"""


							__snake_case     			=       [any(compute_exact(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE ) for ref in refs ) for pred, refs in zip(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )]
							return (sum(SCREAMING_SNAKE_CASE ) / len(SCREAMING_SNAKE_CASE )) * 1_00
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )  ->     str:







							"""simple docstring"""


							__snake_case     			=       [rgram for rgrams in rgramslist for rgram in rgrams]
							__snake_case     			=       Counter(SCREAMING_SNAKE_CASE )

							__snake_case     			=       Counter(SCREAMING_SNAKE_CASE )
							__snake_case     			=       Counter()
							for sgram, scount in sgramcounter.items():
														__snake_case     			=       scount * numref

							__snake_case     			=       Counter(SCREAMING_SNAKE_CASE )
							__snake_case     			=       Counter()
							for cgram, ccount in cgramcounter.items():
														__snake_case     			=       ccount * numref

							# KEEP
							__snake_case     			=       sgramcounter_rep & cgramcounter_rep
							__snake_case     			=       keepgramcounter_rep & rgramcounter
							__snake_case     			=       sgramcounter_rep & rgramcounter

							__snake_case     			=       0
							__snake_case     			=       0
							for keepgram in keepgramcountergood_rep:
														keeptmpscorea += keepgramcountergood_rep[keepgram] / keepgramcounter_rep[keepgram]
														# Fix an alleged bug [2] in the keep score computation.
														# keeptmpscore2 += keepgramcountergood_rep[keepgram] / keepgramcounterall_rep[keepgram]
														keeptmpscorea += keepgramcountergood_rep[keepgram]
							# Define 0/0=1 instead of 0 to give higher scores for predictions that match
							#      a target exactly.
							__snake_case     			=       1
							__snake_case     			=       1
							if len(SCREAMING_SNAKE_CASE ) > 0:
														__snake_case     			=       keeptmpscorea / len(SCREAMING_SNAKE_CASE )
							if len(SCREAMING_SNAKE_CASE ) > 0:
														# Fix an alleged bug [2] in the keep score computation.
														# keepscore_recall = keeptmpscore2 / len(keepgramcounterall_rep)
														__snake_case     			=       keeptmpscorea / sum(keepgramcounterall_rep.values() )
							__snake_case     			=       0
							if keepscore_precision > 0 or keepscore_recall > 0:
														__snake_case     			=       2 * keepscore_precision * keepscore_recall / (keepscore_precision + keepscore_recall)

							# DELETION
							__snake_case     			=       sgramcounter_rep - cgramcounter_rep
							__snake_case     			=       delgramcounter_rep - rgramcounter
							__snake_case     			=       sgramcounter_rep - rgramcounter
							__snake_case     			=       0
							__snake_case     			=       0
							for delgram in delgramcountergood_rep:
														deltmpscorea += delgramcountergood_rep[delgram] / delgramcounter_rep[delgram]
														deltmpscorea += delgramcountergood_rep[delgram] / delgramcounterall_rep[delgram]
							# Define 0/0=1 instead of 0 to give higher scores for predictions that match
							# a target exactly.
							__snake_case     			=       1
							if len(SCREAMING_SNAKE_CASE ) > 0:
														__snake_case     			=       deltmpscorea / len(SCREAMING_SNAKE_CASE )

							# ADDITION
							__snake_case     			=       set(SCREAMING_SNAKE_CASE ) - set(SCREAMING_SNAKE_CASE )
							__snake_case     			=       set(SCREAMING_SNAKE_CASE ) & set(SCREAMING_SNAKE_CASE )
							__snake_case     			=       set(SCREAMING_SNAKE_CASE ) - set(SCREAMING_SNAKE_CASE )

							__snake_case     			=       0
							for addgram in addgramcountergood:
														addtmpscore += 1

							# Define 0/0=1 instead of 0 to give higher scores for predictions that match
							# a target exactly.
							__snake_case     			=       1
							__snake_case     			=       1
							if len(SCREAMING_SNAKE_CASE ) > 0:
														__snake_case     			=       addtmpscore / len(SCREAMING_SNAKE_CASE )
							if len(SCREAMING_SNAKE_CASE ) > 0:
														__snake_case     			=       addtmpscore / len(SCREAMING_SNAKE_CASE )
							__snake_case     			=       0
							if addscore_precision > 0 or addscore_recall > 0:
														__snake_case     			=       2 * addscore_precision * addscore_recall / (addscore_precision + addscore_recall)

							return (keepscore, delscore_precision, addscore)
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )  ->     Union[str, Any]:







							"""simple docstring"""


							__snake_case     			=       len(SCREAMING_SNAKE_CASE )

							__snake_case     			=       ssent.split(" " )
							__snake_case     			=       csent.split(" " )
							__snake_case     			=       []
							__snake_case     			=       []
							__snake_case     			=       []
							__snake_case     			=       []
							__snake_case     			=       []
							__snake_case     			=       []

							__snake_case     			=       []
							__snake_case     			=       []
							__snake_case     			=       []
							__snake_case     			=       []
							for rsent in rsents:
														__snake_case     			=       rsent.split(" " )
														__snake_case     			=       []
														__snake_case     			=       []
														__snake_case     			=       []
														ragramslist.append(SCREAMING_SNAKE_CASE )
														for i in range(0							,    len(SCREAMING_SNAKE_CASE ) - 1 ):
																					if i < len(SCREAMING_SNAKE_CASE ) - 1:
																												__snake_case     			=       ragrams[i] + " " + ragrams[i + 1]
																												ragrams.append(SCREAMING_SNAKE_CASE )
																					if i < len(SCREAMING_SNAKE_CASE ) - 2:
																												__snake_case     			=       ragrams[i] + " " + ragrams[i + 1] + " " + ragrams[i + 2]
																												ragrams.append(SCREAMING_SNAKE_CASE )
																					if i < len(SCREAMING_SNAKE_CASE ) - 3:
																												__snake_case     			=       ragrams[i] + " " + ragrams[i + 1] + " " + ragrams[i + 2] + " " + ragrams[i + 3]
																												ragrams.append(SCREAMING_SNAKE_CASE )
														ragramslist.append(SCREAMING_SNAKE_CASE )
														ragramslist.append(SCREAMING_SNAKE_CASE )
														ragramslist.append(SCREAMING_SNAKE_CASE )

							for i in range(0							,    len(SCREAMING_SNAKE_CASE ) - 1 ):
														if i < len(SCREAMING_SNAKE_CASE ) - 1:
																					__snake_case     			=       sagrams[i] + " " + sagrams[i + 1]
																					sagrams.append(SCREAMING_SNAKE_CASE )
														if i < len(SCREAMING_SNAKE_CASE ) - 2:
																					__snake_case     			=       sagrams[i] + " " + sagrams[i + 1] + " " + sagrams[i + 2]
																					sagrams.append(SCREAMING_SNAKE_CASE )
														if i < len(SCREAMING_SNAKE_CASE ) - 3:
																					__snake_case     			=       sagrams[i] + " " + sagrams[i + 1] + " " + sagrams[i + 2] + " " + sagrams[i + 3]
																					sagrams.append(SCREAMING_SNAKE_CASE )

							for i in range(0							,    len(SCREAMING_SNAKE_CASE ) - 1 ):
														if i < len(SCREAMING_SNAKE_CASE ) - 1:
																					__snake_case     			=       cagrams[i] + " " + cagrams[i + 1]
																					cagrams.append(SCREAMING_SNAKE_CASE )
														if i < len(SCREAMING_SNAKE_CASE ) - 2:
																					__snake_case     			=       cagrams[i] + " " + cagrams[i + 1] + " " + cagrams[i + 2]
																					cagrams.append(SCREAMING_SNAKE_CASE )
														if i < len(SCREAMING_SNAKE_CASE ) - 3:
																					__snake_case     			=       cagrams[i] + " " + cagrams[i + 1] + " " + cagrams[i + 2] + " " + cagrams[i + 3]
																					cagrams.append(SCREAMING_SNAKE_CASE )

							((__snake_case)			,   (__snake_case)			,   (__snake_case))     			=       SARIngram(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )
							((__snake_case)			,   (__snake_case)			,   (__snake_case))     			=       SARIngram(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )
							((__snake_case)			,   (__snake_case)			,   (__snake_case))     			=       SARIngram(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )
							((__snake_case)			,   (__snake_case)			,   (__snake_case))     			=       SARIngram(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )
							__snake_case     			=       sum([keepascore, keepascore, keepascore, keepascore] ) / 4
							__snake_case     			=       sum([delascore, delascore, delascore, delascore] ) / 4
							__snake_case     			=       sum([addascore, addascore, addascore, addascore] ) / 4
							__snake_case     			=       (avgkeepscore + avgdelscore + avgaddscore) / 3
							return finalscore
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE = True							,    SCREAMING_SNAKE_CASE = "13a"							,    SCREAMING_SNAKE_CASE = True )  ->     Any:







							"""simple docstring"""


							if lowercase:
														__snake_case     			=       sentence.lower()

							if tokenizer in ["13a", "intl"]:
														if version.parse(sacrebleu.__version__ ).major >= 2:
																					__snake_case     			=       sacrebleu.metrics.bleu._get_tokenizer(SCREAMING_SNAKE_CASE )()(SCREAMING_SNAKE_CASE )
														else:
																					__snake_case     			=       sacrebleu.TOKENIZERS[tokenizer]()(SCREAMING_SNAKE_CASE )
							elif tokenizer == "moses":
														__snake_case     			=       sacremoses.MosesTokenizer().tokenize(SCREAMING_SNAKE_CASE							,    return_str=SCREAMING_SNAKE_CASE							,    escape=SCREAMING_SNAKE_CASE )
							elif tokenizer == "penn":
														__snake_case     			=       sacremoses.MosesTokenizer().penn_tokenize(SCREAMING_SNAKE_CASE							,    return_str=SCREAMING_SNAKE_CASE )
							else:
														__snake_case     			=       sentence

							if not return_str:
														__snake_case     			=       normalized_sent.split()

							return normalized_sent
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )  ->     List[Any]:







							"""simple docstring"""


							if not (len(SCREAMING_SNAKE_CASE ) == len(SCREAMING_SNAKE_CASE ) == len(SCREAMING_SNAKE_CASE )):
														raise ValueError("Sources length must match predictions and references lengths." )
							__snake_case     			=       0
							for src, pred, refs in zip(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE ):
														sari_score += SARIsent(normalize(SCREAMING_SNAKE_CASE )							,    normalize(SCREAMING_SNAKE_CASE )							,    [normalize(SCREAMING_SNAKE_CASE ) for sent in refs] )
							__snake_case     			=       sari_score / len(SCREAMING_SNAKE_CASE )
							return 1_00 * sari_score
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE="exp"							,    SCREAMING_SNAKE_CASE=None							,    SCREAMING_SNAKE_CASE=False							,    SCREAMING_SNAKE_CASE=False							,    SCREAMING_SNAKE_CASE=False							,    )  ->     List[str]:







							"""simple docstring"""


							__snake_case     			=       len(references[0] )
							if any(len(SCREAMING_SNAKE_CASE ) != references_per_prediction for refs in references ):
														raise ValueError("Sacrebleu requires the same number of references for each prediction" )
							__snake_case     			=       [[refs[i] for refs in references] for i in range(SCREAMING_SNAKE_CASE )]
							__snake_case     			=       sacrebleu.corpus_bleu(
							    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    smooth_method=SCREAMING_SNAKE_CASE							,    smooth_value=SCREAMING_SNAKE_CASE							,    force=SCREAMING_SNAKE_CASE							,    lowercase=SCREAMING_SNAKE_CASE							,    use_effective_order=SCREAMING_SNAKE_CASE							,    )
							return output.score







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





					def 							lowerCAmelCase      (				self   :							Union[str, Any]    ):
												return datasets.MetricInfo(
												    description=_DESCRIPTION							,						citation=_CITATION							,						inputs_description=_KWARGS_DESCRIPTION							,						features=datasets.Features(
												        {
												            "predictions": datasets.Value("string"							,						id="sequence"    ),
												            "references": datasets.Sequence(datasets.Value("string"							,						id="sequence"    )							,						id="references"    ),
												        }    )							,						codebase_urls=[
												        "https://github.com/huggingface/transformers/blob/master/src/transformers/data/metrics/squad_metrics.py",
												        "https://github.com/cocoxu/simplification/blob/master/SARI.py",
												        "https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/utils/sari_hook.py",
												        "https://github.com/mjpost/sacreBLEU",
												    ]							,						reference_urls=[
												        "https://www.aclweb.org/anthology/Q16-1029.pdf",
												        "https://github.com/mjpost/sacreBLEU",
												        "https://en.wikipedia.org/wiki/BLEU",
												        "https://towardsdatascience.com/evaluating-text-output-in-nlp-bleu-at-your-own-risk-e8609665a213",
												    ]							,						)





					def 							lowerCAmelCase      (				self   :							List[Any]							,						snake_case_   :							List[Any]							,						snake_case_   :							List[str]							,						snake_case_   :							Optional[Any]    ):
												__snake_case     			=       {}
												result.update({"sari": compute_sari(sources=snake_case_							,						predictions=snake_case_							,						references=snake_case_    )}    )
												result.update({"sacrebleu": compute_sacrebleu(predictions=snake_case_							,						references=snake_case_    )}    )
												result.update({"exact": compute_em(predictions=snake_case_							,						references=snake_case_    )}    )
												return result


"""simple docstring"""







from pathlib import Path
from typing import List

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import get_tests_dir, is_tool_test
from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image


_SCREAMING_SNAKE_CASE	       =      ["""text""", """image""", """audio"""]
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE )  ->     List[Any]:







							"""simple docstring"""


							__snake_case     			=       []

							for input_type in input_types:
														if input_type == "text":
																					inputs.append("Text input" )
														elif input_type == "image":
																					inputs.append(
																					    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO" ) ) / "000000039769.png" ).resize((5_12, 5_12) ) )
														elif input_type == "audio":
																					inputs.append(torch.ones(30_00 ) )
														elif isinstance(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE ):
																					inputs.append(create_inputs(SCREAMING_SNAKE_CASE ) )
														else:
																					raise ValueError(F'''Invalid type requested: {input_type}''' )

							return inputs
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE )  ->     Optional[Any]:







							"""simple docstring"""


							__snake_case     			=       []

							for output in outputs:
														if isinstance(SCREAMING_SNAKE_CASE							,    (str, AgentText) ):
																					output_types.append("text" )
														elif isinstance(SCREAMING_SNAKE_CASE							,    (Image.Image, AgentImage) ):
																					output_types.append("image" )
														elif isinstance(SCREAMING_SNAKE_CASE							,    (torch.Tensor, AgentAudio) ):
																					output_types.append("audio" )
														else:
																					raise ValueError(F'''Invalid output: {output}''' )

							return output_types







@is_tool_test
class 							__magic_name__     :





					def 							lowerCAmelCase      (				self   :							Optional[int]    ):
												self.assertTrue(hasattr(self.tool							,						"inputs"    )    )
												self.assertTrue(hasattr(self.tool							,						"outputs"    )    )

												__snake_case     			=       self.tool.inputs
												for _input in inputs:
																			if isinstance(_input							,						snake_case_    ):
																										for __input in _input:
																																	self.assertTrue(__input in authorized_types    )
																			else:
																										self.assertTrue(_input in authorized_types    )

												__snake_case     			=       self.tool.outputs
												for _output in outputs:
																			self.assertTrue(_output in authorized_types    )





					def 							lowerCAmelCase      (				self   :							int    ):
												__snake_case     			=       create_inputs(self.tool.inputs    )
												__snake_case     			=       self.tool(*snake_case_    )

												# There is a single output
												if len(self.tool.outputs    ) == 1:
																			__snake_case     			=       [outputs]

												self.assertListEqual(output_types(snake_case_    )							,						self.tool.outputs    )





					def 							lowerCAmelCase      (				self   :							Union[str, Any]    ):
												self.assertTrue(hasattr(self.tool							,						"description"    )    )
												self.assertTrue(hasattr(self.tool							,						"default_checkpoint"    )    )
												self.assertTrue(self.tool.description.startswith("This is a tool that"    )    )





					def 							lowerCAmelCase      (				self   :							Any    ):
												__snake_case     			=       create_inputs(self.tool.inputs    )
												__snake_case     			=       self.tool(*snake_case_    )

												if not isinstance(snake_case_							,						snake_case_    ):
																			__snake_case     			=       [outputs]

												self.assertEqual(len(snake_case_    )							,						len(self.tool.outputs    )    )

												for output, output_type in zip(snake_case_							,						self.tool.outputs    ):
																			__snake_case     			=       AGENT_TYPE_MAPPING[output_type]
																			self.assertTrue(isinstance(snake_case_							,						snake_case_    )    )





					def 							lowerCAmelCase      (				self   :							Tuple    ):
												__snake_case     			=       create_inputs(self.tool.inputs    )

												__snake_case     			=       []

												for _input, input_type in zip(snake_case_							,						self.tool.inputs    ):
																			if isinstance(snake_case_							,						snake_case_    ):
																										_inputs.append([AGENT_TYPE_MAPPING[_input_type](_input    ) for _input_type in input_type]    )
																			else:
																										_inputs.append(AGENT_TYPE_MAPPING[input_type](_input    )    )

        # Should not raise an error
												__snake_case     			=       self.tool(*snake_case_    )

												if not isinstance(snake_case_							,						snake_case_    ):
																			__snake_case     			=       [outputs]

												self.assertEqual(len(snake_case_    )							,						len(self.tool.outputs    )    )
