


'''simple docstring'''



import re
import string
from collections import Counter

import sacrebleu
import sacremoses
from packaging import version

import datasets


_lowercase      :      List[str]            =					"""
@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\",
}
"""

_lowercase      :      Union[str, Any]            =					"""\
WIKI_SPLIT is the combination of three metrics SARI, EXACT and SACREBLEU
It can be used to evaluate the quality of machine-generated texts.
"""


_lowercase      :      Union[str, Any]            =					"""
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 				lowerCamelCase__				(	A       :      List[Any]					):




						'''simple docstring'''






						def remove_articles(A       :      Optional[int]					):
												UpperCAmelCase					    =     re.compile(R'''\b(a|an|the)\b'''				,       re.UNICODE					)
												return re.sub(A				,       ''' '''				,       A					)

						def white_space_fix(A       :      Union[str, Any]					):
												return " ".join(text.split()					)

						def remove_punc(A       :      List[str]					):
												UpperCAmelCase					    =     set(string.punctuation					)
												return "".join(ch for ch in text if ch not in exclude					)

						def lower(A       :      List[Any]					):
												return text.lower()

						return white_space_fix(remove_articles(remove_punc(lower(A					)					)					)					)


def 				lowerCamelCase__				(	A       :      Optional[Any]				,       A       :      Dict					):




						'''simple docstring'''






						return int(normalize_answer(A					) == normalize_answer(A					)					)


def 				lowerCamelCase__				(	A       :      Optional[int]				,       A       :      Dict					):




						'''simple docstring'''






						UpperCAmelCase					    =     [any(compute_exact(A				,       A					) for ref in refs					) for pred, refs in zip(A				,       A					)]
						return (sum(A					) / len(A					)) * 1_00


def 				lowerCamelCase__				(	A       :      Union[str, Any]				,       A       :      List[str]				,       A       :      List[Any]				,       A       :      str					):




						'''simple docstring'''






						UpperCAmelCase					    =     [rgram for rgrams in rgramslist for rgram in rgrams]
						UpperCAmelCase					    =     Counter(A					)

						UpperCAmelCase					    =     Counter(A					)
						UpperCAmelCase					    =     Counter()
						for sgram, scount in sgramcounter.items():
												UpperCAmelCase					    =     scount * numref

						UpperCAmelCase					    =     Counter(A					)
						UpperCAmelCase					    =     Counter()
						for cgram, ccount in cgramcounter.items():
												UpperCAmelCase					    =     ccount * numref

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

						UpperCAmelCase					    =     0
						UpperCAmelCase					    =     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.
						UpperCAmelCase					    =     1
						UpperCAmelCase					    =     1
						if len(A					) > 0:
												UpperCAmelCase					    =     keeptmpscorea / len(A					)
						if len(A					) > 0:
												# Fix an alleged bug [2] in the keep score computation.
												# keepscore_recall = keeptmpscore2 / len(keepgramcounterall_rep)
												UpperCAmelCase					    =     keeptmpscorea / sum(keepgramcounterall_rep.values()					)
						UpperCAmelCase					    =     0
						if keepscore_precision > 0 or keepscore_recall > 0:
												UpperCAmelCase					    =     2 * keepscore_precision * keepscore_recall / (keepscore_precision + keepscore_recall)

						# DELETION
						UpperCAmelCase					    =     sgramcounter_rep - cgramcounter_rep
						UpperCAmelCase					    =     delgramcounter_rep - rgramcounter
						UpperCAmelCase					    =     sgramcounter_rep - rgramcounter
						UpperCAmelCase					    =     0
						UpperCAmelCase					    =     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.
						UpperCAmelCase					    =     1
						if len(A					) > 0:
												UpperCAmelCase					    =     deltmpscorea / len(A					)

						# ADDITION
						UpperCAmelCase					    =     set(A					) - set(A					)
						UpperCAmelCase					    =     set(A					) & set(A					)
						UpperCAmelCase					    =     set(A					) - set(A					)

						UpperCAmelCase					    =     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.
						UpperCAmelCase					    =     1
						UpperCAmelCase					    =     1
						if len(A					) > 0:
												UpperCAmelCase					    =     addtmpscore / len(A					)
						if len(A					) > 0:
												UpperCAmelCase					    =     addtmpscore / len(A					)
						UpperCAmelCase					    =     0
						if addscore_precision > 0 or addscore_recall > 0:
												UpperCAmelCase					    =     2 * addscore_precision * addscore_recall / (addscore_precision + addscore_recall)

						return (keepscore, delscore_precision, addscore)


def 				lowerCamelCase__				(	A       :      List[Any]				,       A       :      Union[str, Any]				,       A       :      Dict					):




						'''simple docstring'''






						UpperCAmelCase					    =     len(A					)

						UpperCAmelCase					    =     ssent.split(''' '''					)
						UpperCAmelCase					    =     csent.split(''' '''					)
						UpperCAmelCase					    =     []
						UpperCAmelCase					    =     []
						UpperCAmelCase					    =     []
						UpperCAmelCase					    =     []
						UpperCAmelCase					    =     []
						UpperCAmelCase					    =     []

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

						for i in range(0				,       len(A					) - 1					):
												if i < len(A					) - 1:
																		UpperCAmelCase					    =     sagrams[i] + ''' ''' + sagrams[i + 1]
																		sagrams.append(A					)
												if i < len(A					) - 2:
																		UpperCAmelCase					    =     sagrams[i] + ''' ''' + sagrams[i + 1] + ''' ''' + sagrams[i + 2]
																		sagrams.append(A					)
												if i < len(A					) - 3:
																		UpperCAmelCase					    =     sagrams[i] + ''' ''' + sagrams[i + 1] + ''' ''' + sagrams[i + 2] + ''' ''' + sagrams[i + 3]
																		sagrams.append(A					)

						for i in range(0				,       len(A					) - 1					):
												if i < len(A					) - 1:
																		UpperCAmelCase					    =     cagrams[i] + ''' ''' + cagrams[i + 1]
																		cagrams.append(A					)
												if i < len(A					) - 2:
																		UpperCAmelCase					    =     cagrams[i] + ''' ''' + cagrams[i + 1] + ''' ''' + cagrams[i + 2]
																		cagrams.append(A					)
												if i < len(A					) - 3:
																		UpperCAmelCase					    =     cagrams[i] + ''' ''' + cagrams[i + 1] + ''' ''' + cagrams[i + 2] + ''' ''' + cagrams[i + 3]
																		cagrams.append(A					)

						((UpperCAmelCase)    ,    (UpperCAmelCase)    ,    (UpperCAmelCase))					    =     SARIngram(A				,       A				,       A				,       A					)
						((UpperCAmelCase)    ,    (UpperCAmelCase)    ,    (UpperCAmelCase))					    =     SARIngram(A				,       A				,       A				,       A					)
						((UpperCAmelCase)    ,    (UpperCAmelCase)    ,    (UpperCAmelCase))					    =     SARIngram(A				,       A				,       A				,       A					)
						((UpperCAmelCase)    ,    (UpperCAmelCase)    ,    (UpperCAmelCase))					    =     SARIngram(A				,       A				,       A				,       A					)
						UpperCAmelCase					    =     sum([keepascore, keepascore, keepascore, keepascore]					) / 4
						UpperCAmelCase					    =     sum([delascore, delascore, delascore, delascore]					) / 4
						UpperCAmelCase					    =     sum([addascore, addascore, addascore, addascore]					) / 4
						UpperCAmelCase					    =     (avgkeepscore + avgdelscore + avgaddscore) / 3
						return finalscore


def 				lowerCamelCase__				(	A       :      str				,       A       :      bool = True				,       A       :      str = "13a"				,       A       :      bool = True					):




						'''simple docstring'''






						if lowercase:
												UpperCAmelCase					    =     sentence.lower()

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

						if not return_str:
												UpperCAmelCase					    =     normalized_sent.split()

						return normalized_sent


def 				lowerCamelCase__				(	A       :      Union[str, Any]				,       A       :      List[Any]				,       A       :      Any					):




						'''simple docstring'''






						if not (len(A					) == len(A					) == len(A					)):
												raise ValueError('''Sources length must match predictions and references lengths.'''					)
						UpperCAmelCase					    =     0
						for src, pred, refs in zip(A				,       A				,       A					):
												sari_score += SARIsent(normalize(A					)				,       normalize(A					)				,       [normalize(A					) for sent in refs]					)
						UpperCAmelCase					    =     sari_score / len(A					)
						return 1_00 * sari_score


def 				lowerCamelCase__				(	A       :      Union[str, Any]				,       A       :      int				,       A       :      int="exp"				,       A       :      List[Any]=None				,       A       :      Union[str, Any]=False				,       A       :      Union[str, Any]=False				,       A       :      Any=False				,       ):




						'''simple docstring'''






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






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




				def 					a__(		self      :			Any				)->					Optional[Any]:

										"""simple docstring"""




										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 					a__(		self      :			Any				,   lowerCAmelCase      :			str				,   lowerCAmelCase      :			List[str]				,   lowerCAmelCase      :			List[Any]				)->					Union[str, Any]:

										"""simple docstring"""




										UpperCAmelCase					    =     {}
										result.update({'''sari''': compute_sari(sources=lowerCAmelCase				,   predictions=lowerCAmelCase				,   references=lowerCAmelCase				)}				)
										result.update({'''sacrebleu''': compute_sacrebleu(predictions=lowerCAmelCase				,   references=lowerCAmelCase				)}				)
										result.update({'''exact''': compute_em(predictions=lowerCAmelCase				,   references=lowerCAmelCase				)}				)
										return result









'''simple docstring'''



from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowercase      :      int            =					logging.get_logger(__name__)

_lowercase      :      Optional[Any]            =					{"""openai-gpt""": """https://huggingface.co/openai-gpt/resolve/main/config.json"""}






class 	UpperCamelCase__(       lowerCAmelCase	):
				__magic_name__    :				Union[str, Any]  		=	"openai-gpt"
				__magic_name__    :				Optional[int]  		=	{
				    "max_position_embeddings": "n_positions",
				    "hidden_size": "n_embd",
				    "num_attention_heads": "n_head",
				    "num_hidden_layers": "n_layer",
				}




				def __init__(		self      :			Optional[int]				,   lowerCAmelCase      :			Optional[Any]=40478				,   lowerCAmelCase      :			str=512				,   lowerCAmelCase      :			List[Any]=768				,   lowerCAmelCase      :			Tuple=12				,   lowerCAmelCase      :			int=12				,   lowerCAmelCase      :			List[str]="gelu"				,   lowerCAmelCase      :			List[str]=0.1				,   lowerCAmelCase      :			List[Any]=0.1				,   lowerCAmelCase      :			Dict=0.1				,   lowerCAmelCase      :			Tuple=1E-5				,   lowerCAmelCase      :			Tuple=0.02				,   lowerCAmelCase      :			Optional[int]="cls_index"				,   lowerCAmelCase      :			Dict=True				,   lowerCAmelCase      :			Optional[int]=None				,   lowerCAmelCase      :			Dict=True				,   lowerCAmelCase      :			int=0.1				,   **lowerCAmelCase      :			Optional[int]				,   )->					str:

										"""simple docstring"""




										UpperCAmelCase					    =     vocab_size
										UpperCAmelCase					    =     n_positions
										UpperCAmelCase					    =     n_embd
										UpperCAmelCase					    =     n_layer
										UpperCAmelCase					    =     n_head
										UpperCAmelCase					    =     afn
										UpperCAmelCase					    =     resid_pdrop
										UpperCAmelCase					    =     embd_pdrop
										UpperCAmelCase					    =     attn_pdrop
										UpperCAmelCase					    =     layer_norm_epsilon
										UpperCAmelCase					    =     initializer_range
										UpperCAmelCase					    =     summary_type
										UpperCAmelCase					    =     summary_use_proj
										UpperCAmelCase					    =     summary_activation
										UpperCAmelCase					    =     summary_first_dropout
										UpperCAmelCase					    =     summary_proj_to_labels
										super().__init__(**lowerCAmelCase				)





