

import re
import string
from collections import Counter

import sacrebleu
import sacremoses
from packaging import version

import datasets


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

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


a__												=						'''
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					(      __a :     Optional[int]      )     ->	int:

					"""simple docstring"""

					def remove_articles(__a :     Tuple      ):
										_a      :       Union[str, Any]   =						re.compile(R'''\b(a|an|the)\b'''						,re.UNICODE      )
										return re.sub(__a						,''' '''						,__a      )

					def white_space_fix(__a :     Any      ):
										return " ".join(text.split()      )

					def remove_punc(__a :     str      ):
										_a      :       Tuple   =						set(string.punctuation      )
										return "".join(ch for ch in text if ch not in exclude      )

					def lower(__a :     int      ):
										return text.lower()

					return white_space_fix(remove_articles(remove_punc(lower(__a      )      )      )      )



def   __UpperCAmelCase					(      __a :     Any						,__a :     int      )     ->	Optional[Any]:

					"""simple docstring"""

					return int(normalize_answer(__a      ) == normalize_answer(__a      )      )



def   __UpperCAmelCase					(      __a :     Union[str, Any]						,__a :     Optional[int]      )     ->	int:

					"""simple docstring"""

					_a      :       Dict   =						[any(compute_exact(__a						,__a      ) for ref in refs      ) for pred, refs in zip(__a						,__a      )]
					return (sum(__a      ) / len(__a      )) * 100



def   __UpperCAmelCase					(      __a :     List[str]						,__a :     Union[str, Any]						,__a :     Optional[int]						,__a :     Union[str, Any]      )     ->	Tuple:

					"""simple docstring"""

					_a      :       Optional[Any]   =						[rgram for rgrams in rgramslist for rgram in rgrams]
					_a      :       Any   =						Counter(__a      )

					_a      :       Dict   =						Counter(__a      )
					_a      :       Tuple   =						Counter()
					for sgram, scount in sgramcounter.items():
										_a      :       List[str]   =						scount * numref

					_a      :       Optional[Any]   =						Counter(__a      )
					_a      :       Dict   =						Counter()
					for cgram, ccount in cgramcounter.items():
										_a      :       int   =						ccount * numref

					# KEEP
					_a      :       Optional[Any]   =						sgramcounter_rep & cgramcounter_rep
					_a      :       Dict   =						keepgramcounter_rep & rgramcounter
					_a      :       str   =						sgramcounter_rep & rgramcounter

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

					# DELETION
					_a      :       Tuple   =						sgramcounter_rep - cgramcounter_rep
					_a      :       Tuple   =						delgramcounter_rep - rgramcounter
					_a      :       List[Any]   =						sgramcounter_rep - rgramcounter
					_a      :       int   =						0
					_a      :       Union[str, 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.
					_a      :       int   =						1
					if len(__a      ) > 0:
										_a      :       Any   =						deltmpscorea / len(__a      )

					# ADDITION
					_a      :       Union[str, Any]   =						set(__a      ) - set(__a      )
					_a      :       str   =						set(__a      ) & set(__a      )
					_a      :       Optional[int]   =						set(__a      ) - set(__a      )

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

					return (keepscore, delscore_precision, addscore)



def   __UpperCAmelCase					(      __a :     int						,__a :     Tuple						,__a :     Optional[int]      )     ->	str:

					"""simple docstring"""

					_a      :       List[Any]   =						len(__a      )

					_a      :       int   =						ssent.split(''' '''      )
					_a      :       List[Any]   =						csent.split(''' '''      )
					_a      :       List[Any]   =						[]
					_a      :       Tuple   =						[]
					_a      :       Union[str, Any]   =						[]
					_a      :       Dict   =						[]
					_a      :       Dict   =						[]
					_a      :       Any   =						[]

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

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



def   __UpperCAmelCase					(      __a :     str						,__a :     bool = True						,__a :     str = "13a"						,__a :     bool = True      )     ->	Optional[int]:

					"""simple docstring"""

					if lowercase:
										_a      :       List[str]   =						sentence.lower()

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

					if not return_str:
										_a      :       Optional[Any]   =						normalized_sent.split()

					return normalized_sent



def   __UpperCAmelCase					(      __a :     Optional[Any]						,__a :     Optional[Any]						,__a :     Optional[int]      )     ->	List[str]:

					"""simple docstring"""

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



def   __UpperCAmelCase					(      __a :     Optional[int]						,__a :     Optional[int]						,__a :     Any="exp"						,__a :     str=None						,__a :     str=False						,__a :     Dict=False						,__a :     List[str]=False						,)     ->	Optional[Any]:

					"""simple docstring"""

					_a      :       List[str]   =						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'''      )
					_a      :       str   =						[[refs[i] for refs in references] for i in range(__a      )]
					_a      :       Union[str, Any]   =						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  ):
							"""simple docstring"""







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





"""simple docstring"""






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


__SCREAMING_SNAKE_CASE							=     {
    'configuration_mega': ['MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MegaConfig', 'MegaOnnxConfig'],
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __SCREAMING_SNAKE_CASE							=     [
     'MEGA_PRETRAINED_MODEL_ARCHIVE_LIST',
     'MegaForCausalLM',
     'MegaForMaskedLM',
     'MegaForMultipleChoice',
     'MegaForQuestionAnswering',
     'MegaForSequenceClassification',
     'MegaForTokenClassification',
     'MegaModel',
     'MegaPreTrainedModel',
 ]

if TYPE_CHECKING:
 from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_mega import (
      MEGA_PRETRAINED_MODEL_ARCHIVE_LIST,
      MegaForCausalLM,
      MegaForMaskedLM,
      MegaForMultipleChoice,
      MegaForQuestionAnswering,
      MegaForSequenceClassification,
      MegaForTokenClassification,
      MegaModel,
      MegaPreTrainedModel,
  )

else:
 import sys

 __SCREAMING_SNAKE_CASE							=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
