
'''simple docstring'''



import re
import string
from collections import Counter

import sacrebleu
import sacremoses
from packaging import version

import datasets


_lowercase    =		"""
@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    =		"""\
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    =		"""
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				(__lowerCamelCase				:Dict		):







						def remove_articles(__lowerCamelCase				:Optional[Any]		):
												_lowerCAmelCase  						=			re.compile(r"""\b(a|an|the)\b"""			,     re.UNICODE		)
												return re.sub(UpperCamelCase__			,     """ """			,     UpperCamelCase__		)

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

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

						def lower(__lowerCamelCase				:Tuple		):
												return text.lower()

						return white_space_fix(remove_articles(remove_punc(lower(UpperCamelCase__		)		)		)		)


def A				(__lowerCamelCase				:Optional[Any]			,     __lowerCamelCase				:Dict		):
						return int(normalize_answer(UpperCamelCase__		) == normalize_answer(UpperCamelCase__		)		)


def A				(__lowerCamelCase				:Any			,     __lowerCamelCase				:Union[str, Any]		):
						_lowerCAmelCase  						=			[any(compute_exact(UpperCamelCase__			,     UpperCamelCase__		) for ref in refs		) for pred, refs in zip(UpperCamelCase__			,     UpperCamelCase__		)]
						return (sum(UpperCamelCase__		) / len(UpperCamelCase__		)) * 100


def A				(__lowerCamelCase				:Union[str, Any]			,     __lowerCamelCase				:Any			,     __lowerCamelCase				:str			,     __lowerCamelCase				:str		):
						_lowerCAmelCase  						=			[rgram for rgrams in rgramslist for rgram in rgrams]
						_lowerCAmelCase  						=			Counter(UpperCamelCase__		)

						_lowerCAmelCase  						=			Counter(UpperCamelCase__		)
						_lowerCAmelCase  						=			Counter()
						for sgram, scount in sgramcounter.items():
												_lowerCAmelCase  						=			scount * numref

						_lowerCAmelCase  						=			Counter(UpperCamelCase__		)
						_lowerCAmelCase  						=			Counter()
						for cgram, ccount in cgramcounter.items():
												_lowerCAmelCase  						=			ccount * numref

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

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

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

						# ADDITION
						_lowerCAmelCase  						=			set(UpperCamelCase__		) - set(UpperCamelCase__		)
						_lowerCAmelCase  						=			set(UpperCamelCase__		) & set(UpperCamelCase__		)
						_lowerCAmelCase  						=			set(UpperCamelCase__		) - set(UpperCamelCase__		)

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

						return (keepscore, delscore_precision, addscore)


def A				(__lowerCamelCase				:Any			,     __lowerCamelCase				:List[Any]			,     __lowerCamelCase				:Optional[int]		):
						_lowerCAmelCase  						=			len(UpperCamelCase__		)

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

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

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

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

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


def A				(__lowerCamelCase				:List[str]			,     __lowerCamelCase				:bool = True			,     __lowerCamelCase				:str = "13a"			,     __lowerCamelCase				:bool = True		):







						if lowercase:
												_lowerCAmelCase  						=			sentence.lower()

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

						if not return_str:
												_lowerCAmelCase  						=			normalized_sent.split()

						return normalized_sent


def A				(__lowerCamelCase				:int			,     __lowerCamelCase				:Tuple			,     __lowerCamelCase				:Union[str, Any]		):







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


def A				(__lowerCamelCase				:Union[str, Any]			,     __lowerCamelCase				:int			,     __lowerCamelCase				:Union[str, Any]="exp"			,     __lowerCamelCase				:List[str]=None			,     __lowerCamelCase				:List[str]=False			,     __lowerCamelCase				:Dict=False			,     __lowerCamelCase				:str=False			,     ):
						_lowerCAmelCase  						=			len(references[0]		)
						if any(len(UpperCamelCase__		) != references_per_prediction for refs in references		):
												raise ValueError("""Sacrebleu requires the same number of references for each prediction"""		)
						_lowerCAmelCase  						=			[[refs[i] for refs in references] for i in range(UpperCamelCase__		)]
						_lowerCAmelCase  						=			sacrebleu.corpus_bleu(
						    UpperCamelCase__			,     UpperCamelCase__			,     smooth_method=UpperCamelCase__			,     smooth_value=UpperCamelCase__			,     force=UpperCamelCase__			,     lowercase=UpperCamelCase__			,     use_effective_order=UpperCamelCase__			,     )
						return output.score





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






						'''simple docstring'''






						def  _lowercase     ( self				):

												"""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  _lowercase     ( self   ,       _lowercase   ,       _lowercase   ,       _lowercase				):

												"""simple docstring"""



												_lowerCAmelCase  						=			{}
												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 collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_lowercase    =		logging.get_logger(__name__)

_lowercase    =		{
    """YituTech/conv-bert-base""": """https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json""",
    """YituTech/conv-bert-medium-small""": (
        """https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json"""
    ),
    """YituTech/conv-bert-small""": """https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json""",
    # See all ConvBERT models at https://huggingface.co/models?filter=convbert
}




class   UpperCAmelCase_					(		_SCREAMING_SNAKE_CASE			):






						'''simple docstring'''






						_lowercase		:				List[Any]									=       '''convbert'''




						def __init__( self   ,       _lowercase=30_522   ,       _lowercase=768   ,       _lowercase=12   ,       _lowercase=12   ,       _lowercase=3_072   ,       _lowercase="gelu"   ,       _lowercase=0.1   ,       _lowercase=0.1   ,       _lowercase=512   ,       _lowercase=2   ,       _lowercase=0.02   ,       _lowercase=1e-12   ,       _lowercase=1   ,       _lowercase=0   ,       _lowercase=2   ,       _lowercase=768   ,       _lowercase=2   ,       _lowercase=9   ,       _lowercase=1   ,       _lowercase=None   ,       **_lowercase   ,       ):

												"""simple docstring"""



												super().__init__(
												    pad_token_id=_lowercase   ,       bos_token_id=_lowercase   ,       eos_token_id=_lowercase   ,       **_lowercase   ,       )

												_lowerCAmelCase  						=			vocab_size
												_lowerCAmelCase  						=			hidden_size
												_lowerCAmelCase  						=			num_hidden_layers
												_lowerCAmelCase  						=			num_attention_heads
												_lowerCAmelCase  						=			intermediate_size
												_lowerCAmelCase  						=			hidden_act
												_lowerCAmelCase  						=			hidden_dropout_prob
												_lowerCAmelCase  						=			attention_probs_dropout_prob
												_lowerCAmelCase  						=			max_position_embeddings
												_lowerCAmelCase  						=			type_vocab_size
												_lowerCAmelCase  						=			initializer_range
												_lowerCAmelCase  						=			layer_norm_eps
												_lowerCAmelCase  						=			embedding_size
												_lowerCAmelCase  						=			head_ratio
												_lowerCAmelCase  						=			conv_kernel_size
												_lowerCAmelCase  						=			num_groups
												_lowerCAmelCase  						=			classifier_dropout






class   UpperCAmelCase_					(		_SCREAMING_SNAKE_CASE			):






						'''simple docstring'''






						@property
						def  _lowercase     ( self				):

												"""simple docstring"""



												if self.task == "multiple-choice":
																		_lowerCAmelCase  						=			{0: """batch""", 1: """choice""", 2: """sequence"""}
												else:
																		_lowerCAmelCase  						=			{0: """batch""", 1: """sequence"""}
												return OrderedDict(
												    [
												        ("""input_ids""", dynamic_axis),
												        ("""attention_mask""", dynamic_axis),
												        ("""token_type_ids""", dynamic_axis),
												    ]				)
