






"""simple docstring"""





import re
import string
from collections import Counter

import sacrebleu
import sacremoses
from packaging import version

import datasets


UpperCamelCase_	:				int										= """
@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\",
}
"""

UpperCamelCase_	:				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.
"""


UpperCamelCase_	:				Optional[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     __lowercase					(    a :				Union[str, Any] )					->					str:

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

      def white_space_fix(a :				int ):
            return " ".join(text.split() )

      def remove_punc(a :				List[Any] ):
            __snake_case      :    Union[str, Any]  			=set(string.punctuation )
            return "".join(ch for ch in text if ch not in exclude )

      def lower(a :				Union[str, Any] ):
            return text.lower()

      return white_space_fix(remove_articles(remove_punc(lower(a ) ) ) )



def     __lowercase					(    a :				Any       ,		a :				Optional[Any] )					->					List[str]:
      return int(normalize_answer(a ) == normalize_answer(a ) )



def     __lowercase					(    a :				Dict       ,		a :				List[Any] )					->					Optional[int]:
      __snake_case      :    Union[str, Any]  			=[any(compute_exact(a       ,		a ) for ref in refs ) for pred, refs in zip(a       ,		a )]
      return (sum(a ) / len(a )) * 100



def     __lowercase					(    a :				List[str]       ,		a :				Optional[int]       ,		a :				Optional[int]       ,		a :				Optional[Any] )					->					str:
      __snake_case      :    str  			=[rgram for rgrams in rgramslist for rgram in rgrams]
      __snake_case      :    int  			=Counter(a )

      __snake_case      :    Optional[Any]  			=Counter(a )
      __snake_case      :    List[Any]  			=Counter()
      for sgram, scount in sgramcounter.items():
            __snake_case      :    Dict  			=scount * numref

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

      # KEEP
      __snake_case      :    Tuple  			=sgramcounter_rep & cgramcounter_rep
      __snake_case      :    Union[str, Any]  			=keepgramcounter_rep & rgramcounter
      __snake_case      :    Any  			=sgramcounter_rep & rgramcounter

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

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

      # ADDITION
      __snake_case      :    List[Any]  			=set(a ) - set(a )
      __snake_case      :    str  			=set(a ) & set(a )
      __snake_case      :    Any  			=set(a ) - set(a )

      __snake_case      :    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.
      __snake_case      :    Optional[Any]  			=1
      __snake_case      :    Optional[int]  			=1
      if len(a ) > 0:
            __snake_case      :    int  			=addtmpscore / len(a )
      if len(a ) > 0:
            __snake_case      :    Union[str, Any]  			=addtmpscore / len(a )
      __snake_case      :    Tuple  			=0
      if addscore_precision > 0 or addscore_recall > 0:
            __snake_case      :    Union[str, Any]  			=2 * addscore_precision * addscore_recall / (addscore_precision + addscore_recall)

      return (keepscore, delscore_precision, addscore)



def     __lowercase					(    a :				Optional[int]       ,		a :				Any       ,		a :				Dict )					->					str:
      __snake_case      :    int  			=len(a )

      __snake_case      :    int  			=ssent.split(''' ''' )
      __snake_case      :    Any  			=csent.split(''' ''' )
      __snake_case      :    Dict  			=[]
      __snake_case      :    str  			=[]
      __snake_case      :    Dict  			=[]
      __snake_case      :    Any  			=[]
      __snake_case      :    Tuple  			=[]
      __snake_case      :    int  			=[]

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

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



def     __lowercase					(    a :				Optional[Any]       ,		a :				bool = True       ,		a :				str = "13a"       ,		a :				bool = True )					->					Optional[int]:
      # 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:
            __snake_case      :    int  			=sentence.lower()

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

      if not return_str:
            __snake_case      :    Any  			=normalized_sent.split()

      return normalized_sent



def     __lowercase					(    a :				Dict       ,		a :				int       ,		a :				Any )					->					List[Any]:
      if not (len(a ) == len(a ) == len(a )):
            raise ValueError('''Sources length must match predictions and references lengths.''' )
      __snake_case      :    Tuple  			=0
      for src, pred, refs in zip(a       ,		a       ,		a ):
            sari_score += SARIsent(normalize(a )       ,		normalize(a )       ,		[normalize(a ) for sent in refs] )
      __snake_case      :    str  			=sari_score / len(a )
      return 100 * sari_score



def     __lowercase					(    a :				List[str]       ,		a :				Dict       ,		a :				int="exp"       ,		a :				Any=None       ,		a :				Any=False       ,		a :				int=False       ,		a :				str=False       ,		)					->					Any:
      __snake_case      :    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''' )
      __snake_case      :    Union[str, Any]  			=[[refs[i] for refs in references] for i in range(a )]
      __snake_case      :    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      _lowercase (     datasets.Metric						):


      def _UpperCamelCase						(    self				:	Union[str, 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 _UpperCamelCase						(    self				:	int							,   a				:	Any							,   a				:	int							,   a				:	Any						):





            """simple docstring"""







            __snake_case      :    str  			={}
            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"""





import unittest

import numpy as np

from transformers.testing_utils import is_flaky, require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import DonutImageProcessor


class      _lowercase (     unittest.TestCase						):


      def __init__(    self				:	List[Any]							,   a				:	int							,   a				:	Dict=7							,   a				:	int=3							,   a				:	int=1_8							,   a				:	Dict=3_0							,   a				:	Dict=4_0_0							,   a				:	Optional[Any]=True							,   a				:	Dict=None							,   a				:	int=True							,   a				:	Dict=False							,   a				:	int=True							,   a				:	str=True							,   a				:	List[str]=[0.5, 0.5, 0.5]							,   a				:	Optional[Any]=[0.5, 0.5, 0.5]							,   ):





            """simple docstring"""







            __snake_case      :    List[Any]  			=parent
            __snake_case      :    List[Any]  			=batch_size
            __snake_case      :    str  			=num_channels
            __snake_case      :    Dict  			=image_size
            __snake_case      :    str  			=min_resolution
            __snake_case      :    Tuple  			=max_resolution
            __snake_case      :    str  			=do_resize
            __snake_case      :    Any  			=size if size is not None else {'''height''': 1_8, '''width''': 2_0}
            __snake_case      :    List[Any]  			=do_thumbnail
            __snake_case      :    Tuple  			=do_align_axis
            __snake_case      :    Any  			=do_pad
            __snake_case      :    Dict  			=do_normalize
            __snake_case      :    List[Any]  			=image_mean
            __snake_case      :    Any  			=image_std







      def _UpperCamelCase						(    self				:	str						):





            """simple docstring"""







            return {
                "do_resize": self.do_resize,
                "size": self.size,
                "do_thumbnail": self.do_thumbnail,
                "do_align_long_axis": self.do_align_axis,
                "do_pad": self.do_pad,
                "do_normalize": self.do_normalize,
                "image_mean": self.image_mean,
                "image_std": self.image_std,
            }



@require_torch
@require_vision
class      _lowercase (     lowerCAmelCase		,  unittest.TestCase						):
      _a   :     str								=   DonutImageProcessor if is_vision_available() else None


      def _UpperCamelCase						(    self				:	Optional[int]						):





            """simple docstring"""







            __snake_case      :    Optional[Any]  			=DonutImageProcessingTester(self						)


      @property
      def _UpperCamelCase						(    self				:	Optional[int]						):





            """simple docstring"""







            return self.image_processor_tester.prepare_image_processor_dict()


      def _UpperCamelCase						(    self				:	List[str]						):





            """simple docstring"""







            __snake_case      :    Optional[Any]  			=self.image_processing_class(**self.image_processor_dict						)
            self.assertTrue(hasattr(a							,   '''do_resize'''						)						)
            self.assertTrue(hasattr(a							,   '''size'''						)						)
            self.assertTrue(hasattr(a							,   '''do_thumbnail'''						)						)
            self.assertTrue(hasattr(a							,   '''do_align_long_axis'''						)						)
            self.assertTrue(hasattr(a							,   '''do_pad'''						)						)
            self.assertTrue(hasattr(a							,   '''do_normalize'''						)						)
            self.assertTrue(hasattr(a							,   '''image_mean'''						)						)
            self.assertTrue(hasattr(a							,   '''image_std'''						)						)


      def _UpperCamelCase						(    self				:	Tuple						):





            """simple docstring"""







            __snake_case      :    Dict  			=self.image_processing_class.from_dict(self.image_processor_dict						)
            self.assertEqual(image_processor.size							,   {'''height''': 1_8, '''width''': 2_0}						)

            __snake_case      :    List[Any]  			=self.image_processing_class.from_dict(self.image_processor_dict							,   size=4_2						)
            self.assertEqual(image_processor.size							,   {'''height''': 4_2, '''width''': 4_2}						)

            # Previous config had dimensions in (width, height) order
            __snake_case      :    Tuple  			=self.image_processing_class.from_dict(self.image_processor_dict							,   size=(4_2, 8_4)						)
            self.assertEqual(image_processor.size							,   {'''height''': 8_4, '''width''': 4_2}						)


      def _UpperCamelCase						(    self				:	Any						):





            """simple docstring"""







            pass


      @is_flaky()
      def _UpperCamelCase						(    self				:	Union[str, Any]						):





            """simple docstring"""







            __snake_case      :    List[Any]  			=self.image_processing_class(**self.image_processor_dict						)
            # create random PIL images
            __snake_case      :    List[Any]  			=prepare_image_inputs(self.image_processor_tester							,   equal_resolution=a						)
            for image in image_inputs:
                  self.assertIsInstance(a							,   Image.Image						)

            # Test not batched input
            __snake_case      :    str  			=image_processing(image_inputs[0]							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )

            # Test batched
            __snake_case      :    Optional[int]  			=image_processing(a							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )


      @is_flaky()
      def _UpperCamelCase						(    self				:	Tuple						):





            """simple docstring"""







            __snake_case      :    Tuple  			=self.image_processing_class(**self.image_processor_dict						)
            # create random numpy tensors
            __snake_case      :    Optional[int]  			=prepare_image_inputs(self.image_processor_tester							,   equal_resolution=a							,   numpify=a						)
            for image in image_inputs:
                  self.assertIsInstance(a							,   np.ndarray						)

            # Test not batched input
            __snake_case      :    List[Any]  			=image_processing(image_inputs[0]							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )

            # Test batched
            __snake_case      :    int  			=image_processing(a							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )







      @is_flaky()
      def _UpperCamelCase						(    self				:	List[str]						):





            """simple docstring"""







            __snake_case      :    Dict  			=self.image_processing_class(**self.image_processor_dict						)
            # create random PyTorch tensors
            __snake_case      :    Dict  			=prepare_image_inputs(self.image_processor_tester							,   equal_resolution=a							,   torchify=a						)
            for image in image_inputs:
                  self.assertIsInstance(a							,   torch.Tensor						)

            # Test not batched input
            __snake_case      :    Dict  			=image_processing(image_inputs[0]							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )

            # Test batched
            __snake_case      :    Optional[int]  			=image_processing(a							,   return_tensors='''pt'''						).pixel_values
            self.assertEqual(
                encoded_images.shape							,   (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.size['''height'''],
                    self.image_processor_tester.size['''width'''],
                )							,   )


