





'''simple docstring'''





import operator as op


snake_case_							=			'''scaler.pt'''
snake_case_							=			'''pytorch_model'''
snake_case_							=			'''random_states'''
snake_case_							=			'''optimizer'''
snake_case_							=			'''scheduler'''
snake_case_							=			'''pytorch_model.bin'''
snake_case_							=			'''pytorch_model.bin.index.json'''
snake_case_							=			'''model.safetensors'''
snake_case_							=			'''model.safetensors.index.json'''
snake_case_							=			'''1.10.2'''
snake_case_							=			'''py38'''
snake_case_							=			'''4.17.0'''
snake_case_							=			['''ml.p3.16xlarge''', '''ml.p3dn.24xlarge''', '''ml.p4dn.24xlarge''']
snake_case_							=			['''FULL_SHARD''', '''SHARD_GRAD_OP''', '''NO_SHARD''', '''HYBRID_SHARD''', '''HYBRID_SHARD_ZERO2''']
snake_case_							=			['''TRANSFORMER_BASED_WRAP''', '''SIZE_BASED_WRAP''', '''NO_WRAP''']
snake_case_							=			['''BACKWARD_PRE''', '''BACKWARD_POST''', '''NO_PREFETCH''']
snake_case_							=			['''FULL_STATE_DICT''', '''LOCAL_STATE_DICT''', '''SHARDED_STATE_DICT''']
snake_case_							=			'''2.0.1'''
snake_case_							=			['''pdsh''', '''standard''', '''openmpi''', '''mvapich''']
snake_case_							=			['''default''', '''reduce-overhead''', '''max-autotune''']

snake_case_							=			{'''>''': op.gt, '''>=''': op.ge, '''==''': op.eq, '''!=''': op.ne, '''<=''': op.le, '''<''': op.lt}

# These are the args for `torch.distributed.launch` for pytorch < 1.9
snake_case_							=			[
    '''nnodes''',
    '''nproc_per_node''',
    '''rdzv_backend''',
    '''rdzv_endpoint''',
    '''rdzv_id''',
    '''rdzv_conf''',
    '''standalone''',
    '''max_restarts''',
    '''monitor_interval''',
    '''start_method''',
    '''role''',
    '''module''',
    '''m''',
    '''no_python''',
    '''run_path''',
    '''log_dir''',
    '''r''',
    '''redirects''',
    '''t''',
    '''tee''',
    '''node_rank''',
    '''master_addr''',
    '''master_port''',
]

snake_case_							=			['''DEEPSPEED''', '''MULTI_GPU''', '''FSDP''', '''MEGATRON_LM''']
snake_case_							=			['''DEEPSPEED''', '''MULTI_XPU''', '''FSDP''']








import re
import string
from collections import Counter

import sacrebleu
import sacremoses
from packaging import version

import datasets


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",
}
'''

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.
'''


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 					A__	(       SCREAMING_SNAKE_CASE_		)   ->     Tuple:

		def remove_articles(SCREAMING_SNAKE_CASE_		):
				lowerCamelCase   : Tuple								=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_		):
				lowerCamelCase   : int								=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 					A__	(       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)   ->     Union[str, Any]:
		return int(normalize_answer(SCREAMING_SNAKE_CASE_		) == normalize_answer(SCREAMING_SNAKE_CASE_		)		)


def 					A__	(       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)   ->     Union[str, Any]:
		lowerCamelCase   : Union[str, Any]								=[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_0_0


def 					A__	(       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)   ->     str:
		lowerCamelCase   : Any								=[rgram for rgrams in rgramslist for rgram in rgrams]
		lowerCamelCase   : int								=Counter(SCREAMING_SNAKE_CASE_		)

		lowerCamelCase   : Dict								=Counter(SCREAMING_SNAKE_CASE_		)
		lowerCamelCase   : Any								=Counter()
		for sgram, scount in sgramcounter.items():
				lowerCamelCase   : Tuple								=scount * numref

		lowerCamelCase   : Optional[int]								=Counter(SCREAMING_SNAKE_CASE_		)
		lowerCamelCase   : Tuple								=Counter()
		for cgram, ccount in cgramcounter.items():
				lowerCamelCase   : Tuple								=ccount * numref

		# KEEP
		lowerCamelCase   : str								=sgramcounter_rep & cgramcounter_rep
		lowerCamelCase   : Union[str, Any]								=keepgramcounter_rep & rgramcounter
		lowerCamelCase   : Optional[Any]								=sgramcounter_rep & rgramcounter

		lowerCamelCase   : Optional[Any]								=0
		lowerCamelCase   : List[Any]								=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.
		lowerCamelCase   : Tuple								=1
		lowerCamelCase   : int								=1
		if len(SCREAMING_SNAKE_CASE_		) > 0:
				lowerCamelCase   : Tuple								=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)
				lowerCamelCase   : Any								=keeptmpscorea / sum(keepgramcounterall_rep.values()		)
		lowerCamelCase   : Optional[Any]								=0
		if keepscore_precision > 0 or keepscore_recall > 0:
				lowerCamelCase   : Optional[int]								=2 * keepscore_precision * keepscore_recall / (keepscore_precision + keepscore_recall)

		# DELETION
		lowerCamelCase   : int								=sgramcounter_rep - cgramcounter_rep
		lowerCamelCase   : Dict								=delgramcounter_rep - rgramcounter
		lowerCamelCase   : Dict								=sgramcounter_rep - rgramcounter
		lowerCamelCase   : Optional[int]								=0
		lowerCamelCase   : List[Any]								=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.
		lowerCamelCase   : str								=1
		if len(SCREAMING_SNAKE_CASE_		) > 0:
				lowerCamelCase   : Optional[int]								=deltmpscorea / len(SCREAMING_SNAKE_CASE_		)

		# ADDITION
		lowerCamelCase   : List[Any]								=set(SCREAMING_SNAKE_CASE_		) - set(SCREAMING_SNAKE_CASE_		)
		lowerCamelCase   : int								=set(SCREAMING_SNAKE_CASE_		) & set(SCREAMING_SNAKE_CASE_		)
		lowerCamelCase   : Optional[Any]								=set(SCREAMING_SNAKE_CASE_		) - set(SCREAMING_SNAKE_CASE_		)

		lowerCamelCase   : int								=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.
		lowerCamelCase   : int								=1
		lowerCamelCase   : List[Any]								=1
		if len(SCREAMING_SNAKE_CASE_		) > 0:
				lowerCamelCase   : str								=addtmpscore / len(SCREAMING_SNAKE_CASE_		)
		if len(SCREAMING_SNAKE_CASE_		) > 0:
				lowerCamelCase   : List[str]								=addtmpscore / len(SCREAMING_SNAKE_CASE_		)
		lowerCamelCase   : Optional[Any]								=0
		if addscore_precision > 0 or addscore_recall > 0:
				lowerCamelCase   : Optional[Any]								=2 * addscore_precision * addscore_recall / (addscore_precision + addscore_recall)

		return (keepscore, delscore_precision, addscore)


def 					A__	(       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)   ->     Dict:
		lowerCamelCase   : Optional[int]								=len(SCREAMING_SNAKE_CASE_		)

		lowerCamelCase   : Dict								=ssent.split(''' '''		)
		lowerCamelCase   : Any								=csent.split(''' '''		)
		lowerCamelCase   : str								=[]
		lowerCamelCase   : Optional[Any]								=[]
		lowerCamelCase   : List[Any]								=[]
		lowerCamelCase   : List[str]								=[]
		lowerCamelCase   : Tuple								=[]
		lowerCamelCase   : Optional[Any]								=[]

		lowerCamelCase   : int								=[]
		lowerCamelCase   : List[str]								=[]
		lowerCamelCase   : Dict								=[]
		lowerCamelCase   : Any								=[]
		for rsent in rsents:
				lowerCamelCase   : Any								=rsent.split(''' '''		)
				lowerCamelCase   : int								=[]
				lowerCamelCase   : Optional[Any]								=[]
				lowerCamelCase   : List[Any]								=[]
				ragramslist.append(SCREAMING_SNAKE_CASE_		)
				for i in range(0							,       len(SCREAMING_SNAKE_CASE_		) - 1		):
						if i < len(SCREAMING_SNAKE_CASE_		) - 1:
								lowerCamelCase   : Optional[int]								=ragrams[i] + ''' ''' + ragrams[i + 1]
								ragrams.append(SCREAMING_SNAKE_CASE_		)
						if i < len(SCREAMING_SNAKE_CASE_		) - 2:
								lowerCamelCase   : str								=ragrams[i] + ''' ''' + ragrams[i + 1] + ''' ''' + ragrams[i + 2]
								ragrams.append(SCREAMING_SNAKE_CASE_		)
						if i < len(SCREAMING_SNAKE_CASE_		) - 3:
								lowerCamelCase   : int								=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:
						lowerCamelCase   : Optional[int]								=sagrams[i] + ''' ''' + sagrams[i + 1]
						sagrams.append(SCREAMING_SNAKE_CASE_		)
				if i < len(SCREAMING_SNAKE_CASE_		) - 2:
						lowerCamelCase   : List[Any]								=sagrams[i] + ''' ''' + sagrams[i + 1] + ''' ''' + sagrams[i + 2]
						sagrams.append(SCREAMING_SNAKE_CASE_		)
				if i < len(SCREAMING_SNAKE_CASE_		) - 3:
						lowerCamelCase   : Optional[int]								=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:
						lowerCamelCase   : Optional[int]								=cagrams[i] + ''' ''' + cagrams[i + 1]
						cagrams.append(SCREAMING_SNAKE_CASE_		)
				if i < len(SCREAMING_SNAKE_CASE_		) - 2:
						lowerCamelCase   : List[str]								=cagrams[i] + ''' ''' + cagrams[i + 1] + ''' ''' + cagrams[i + 2]
						cagrams.append(SCREAMING_SNAKE_CASE_		)
				if i < len(SCREAMING_SNAKE_CASE_		) - 3:
						lowerCamelCase   : str								=cagrams[i] + ''' ''' + cagrams[i + 1] + ''' ''' + cagrams[i + 2] + ''' ''' + cagrams[i + 3]
						cagrams.append(SCREAMING_SNAKE_CASE_		)

		((lowerCamelCase)		,   (lowerCamelCase)		,   (lowerCamelCase))   : Any								=SARIngram(SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)
		((lowerCamelCase)		,   (lowerCamelCase)		,   (lowerCamelCase))   : Optional[Any]								=SARIngram(SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)
		((lowerCamelCase)		,   (lowerCamelCase)		,   (lowerCamelCase))   : List[Any]								=SARIngram(SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)
		((lowerCamelCase)		,   (lowerCamelCase)		,   (lowerCamelCase))   : List[str]								=SARIngram(SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)
		lowerCamelCase   : List[Any]								=sum([keepascore, keepascore, keepascore, keepascore]		) / 4
		lowerCamelCase   : List[str]								=sum([delascore, delascore, delascore, delascore]		) / 4
		lowerCamelCase   : int								=sum([addascore, addascore, addascore, addascore]		) / 4
		lowerCamelCase   : Any								=(avgkeepscore + avgdelscore + avgaddscore) / 3
		return finalscore


def 					A__	(       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_ = True							,       SCREAMING_SNAKE_CASE_ = "13a"							,       SCREAMING_SNAKE_CASE_ = True		)   ->     Any:
		# Normalization is requried for the ASSET dataset (one of the primary
		# datasets in sentence simplification) to allow using space
		# to split the sentence. Even though Wiki-Auto and TURK datasets,
		# do not require normalization, we do it for consistency.
		# Code adapted from the EASSE library [1] written by the authors of the ASSET dataset.
		# [1] https://github.com/feralvam/easse/blob/580bba7e1378fc8289c663f864e0487188fe8067/easse/utils/preprocessing.py#L7

		if lowercase:
				lowerCamelCase   : Union[str, Any]								=sentence.lower()

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

		if not return_str:
				lowerCamelCase   : Union[str, Any]								=normalized_sent.split()

		return normalized_sent


def 					A__	(       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)   ->     str:
		if not (len(SCREAMING_SNAKE_CASE_		) == len(SCREAMING_SNAKE_CASE_		) == len(SCREAMING_SNAKE_CASE_		)):
				raise ValueError('''Sources length must match predictions and references lengths.'''		)
		lowerCamelCase   : Dict								=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]		)
		lowerCamelCase   : str								=sari_score / len(SCREAMING_SNAKE_CASE_		)
		return 1_0_0 * sari_score


def 					A__	(       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							,       )   ->     Dict:
		lowerCamelCase   : Optional[int]								=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'''		)
		lowerCamelCase   : Optional[int]								=[[refs[i] for refs in references] for i in range(SCREAMING_SNAKE_CASE_		)]
		lowerCamelCase   : Union[str, Any]								=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        snake_case_		(			datasets.Metric):







		def 			__lowercase   (		self   )				->   List[str]:
				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 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase   )				->   Tuple:
				lowerCamelCase   : str								={}
				result.update({'''sari''': compute_sari(sources=__lowercase			,     predictions=__lowercase			,     references=__lowercase   )}   )
				result.update({'''sacrebleu''': compute_sacrebleu(predictions=__lowercase			,     references=__lowercase   )}   )
				result.update({'''exact''': compute_em(predictions=__lowercase			,     references=__lowercase   )}   )
				return result

