
import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import XLMRobertaTokenizerFast

from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel
from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()


class 						__lowerCAmelCase	(	UpperCamelCase_ , unittest.TestCase      ):



      """simple docstring"""







      A__	:			Any        =    KandinskyImgaImgPipeline
      A__	:			Optional[Any]        =    ['''prompt''', '''image_embeds''', '''negative_image_embeds''', '''image''']
      A__	:			Optional[int]        =    [
          '''prompt''',
          '''negative_prompt''',
          '''image_embeds''',
          '''negative_image_embeds''',
          '''image''',
      ]
      A__	:			str        =    [
          '''generator''',
          '''height''',
          '''width''',
          '''strength''',
          '''guidance_scale''',
          '''negative_prompt''',
          '''num_inference_steps''',
          '''return_dict''',
          '''guidance_scale''',
          '''num_images_per_prompt''',
          '''output_type''',
          '''return_dict''',
      ]
      A__	:			Any        =    False




      @property
      def 		snake_case_			(      self		:       Optional[int]				):
          return 32




      @property
      def 		snake_case_			(      self		:       Union[str, Any]				):
          return 32




      @property
      def 		snake_case_			(      self		:       List[Any]				):
          return self.time_input_dim




      @property
      def 		snake_case_			(      self		:       Union[str, Any]				):
          return self.time_input_dim * 4




      @property
      def 		snake_case_			(      self		:       Union[str, Any]				):
          return 100




      @property
      def 		snake_case_			(      self		:       str				):
          __lowercase		:			Union[str, Any]	    =       XLMRobertaTokenizerFast.from_pretrained('''YiYiXu/tiny-random-mclip-base'''				)
          return tokenizer




      @property
      def 		snake_case_			(      self		:       Optional[Any]				):
          torch.manual_seed(0				)
          __lowercase		:			Dict	    =       MCLIPConfig(
              numDims=self.cross_attention_dim				,       transformerDimensions=self.text_embedder_hidden_size				,       hidden_size=self.text_embedder_hidden_size				,       intermediate_size=37				,       num_attention_heads=4				,       num_hidden_layers=5				,       vocab_size=1005				,       )

          __lowercase		:			int	    =       MultilingualCLIP(__a				)
          __lowercase		:			Any	    =       text_encoder.eval()

          return text_encoder




      @property
      def 		snake_case_			(      self		:       int				):
          torch.manual_seed(0				)

          __lowercase		:			Optional[Any]	    =       {
              'in_channels': 4,
              # Out channels is double in channels because predicts mean and variance
              'out_channels': 8,
              'addition_embed_type': 'text_image',
              'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'),
              'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'),
              'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn',
              'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2),
              'layers_per_block': 1,
              'encoder_hid_dim': self.text_embedder_hidden_size,
              'encoder_hid_dim_type': 'text_image_proj',
              'cross_attention_dim': self.cross_attention_dim,
              'attention_head_dim': 4,
              'resnet_time_scale_shift': 'scale_shift',
              'class_embed_type': None,
          }

          __lowercase		:			List[str]	    =       UNetaDConditionModel(**__a				)
          return model




      @property
      def 		snake_case_			(      self		:       List[str]				):
          return {
              "block_out_channels": [32, 64],
              "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"],
              "in_channels": 3,
              "latent_channels": 4,
              "layers_per_block": 1,
              "norm_num_groups": 8,
              "norm_type": "spatial",
              "num_vq_embeddings": 12,
              "out_channels": 3,
              "up_block_types": [
                  "AttnUpDecoderBlock2D",
                  "UpDecoderBlock2D",
              ],
              "vq_embed_dim": 4,
          }




      @property
      def 		snake_case_			(      self		:       List[str]				):
          torch.manual_seed(0				)
          __lowercase		:			List[Any]	    =       VQModel(**self.dummy_movq_kwargs				)
          return model




      def 		snake_case_			(      self		:       Dict				):
          __lowercase		:			Tuple	    =       self.dummy_text_encoder
          __lowercase		:			Union[str, Any]	    =       self.dummy_tokenizer
          __lowercase		:			List[str]	    =       self.dummy_unet
          __lowercase		:			List[str]	    =       self.dummy_movq

          __lowercase		:			str	    =       {
              'num_train_timesteps': 1000,
              'beta_schedule': 'linear',
              'beta_start': 0.0_00_85,
              'beta_end': 0.0_12,
              'clip_sample': False,
              'set_alpha_to_one': False,
              'steps_offset': 0,
              'prediction_type': 'epsilon',
              'thresholding': False,
          }

          __lowercase		:			List[str]	    =       DDIMScheduler(**__a				)

          __lowercase		:			Any	    =       {
              'text_encoder': text_encoder,
              'tokenizer': tokenizer,
              'unet': unet,
              'scheduler': scheduler,
              'movq': movq,
          }

          return components




      def 		snake_case_			(      self		:       Dict				,       _snake_case		:       Tuple				,       _snake_case		:       List[str]=0				):
          __lowercase		:			str	    =       floats_tensor((1, self.cross_attention_dim)				,       rng=random.Random(__a				)				).to(__a				)
          __lowercase		:			Dict	    =       floats_tensor((1, self.cross_attention_dim)				,       rng=random.Random(seed + 1				)				).to(__a				)
          # create init_image
          __lowercase		:			List[str]	    =       floats_tensor((1, 3, 64, 64)				,       rng=random.Random(__a				)				).to(__a				)
          __lowercase		:			Union[str, Any]	    =       image.cpu().permute(0				,       2				,       3				,       1				)[0]
          __lowercase		:			Optional[int]	    =       Image.fromarray(np.uinta(__a				)				).convert('''RGB'''				).resize((256, 256)				)

          if str(__a				).startswith('''mps'''				):
              __lowercase		:			Optional[int]	    =       torch.manual_seed(__a				)
          else:
              __lowercase		:			Any	    =       torch.Generator(device=__a				).manual_seed(__a				)
          __lowercase		:			Union[str, Any]	    =       {
              'prompt': 'horse',
              'image': init_image,
              'image_embeds': image_embeds,
              'negative_image_embeds': negative_image_embeds,
              'generator': generator,
              'height': 64,
              'width': 64,
              'num_inference_steps': 10,
              'guidance_scale': 7.0,
              'strength': 0.2,
              'output_type': 'np',
          }
          return inputs




      def 		snake_case_			(      self		:       str				):
          __lowercase		:			Tuple	    =       'cpu'

          __lowercase		:			Dict	    =       self.get_dummy_components()

          __lowercase		:			Optional[int]	    =       self.pipeline_class(**__a				)
          __lowercase		:			Tuple	    =       pipe.to(__a				)

          pipe.set_progress_bar_config(disable=__a				)

          __lowercase		:			List[str]	    =       pipe(**self.get_dummy_inputs(__a				)				)
          __lowercase		:			List[str]	    =       output.images

          __lowercase		:			Union[str, Any]	    =       pipe(
              **self.get_dummy_inputs(__a				)				,       return_dict=__a				,       )[0]

          __lowercase		:			int	    =       image[0, -3:, -3:, -1]
          __lowercase		:			Any	    =       image_from_tuple[0, -3:, -3:, -1]

          assert image.shape == (1, 64, 64, 3)

          __lowercase		:			List[str]	    =       np.array(
              [0.61_47_49_43, 0.6_07_35_39, 0.43_30_85_44, 0.5_92_82_69, 0.47_49_35_95, 0.46_75_59_73, 0.4_61_38_38, 0.45_36_87_97, 0.50_11_92_33]				)
          assert (
              np.abs(image_slice.flatten() - expected_slice				).max() < 1E-2
          ), F' expected_slice {expected_slice}, but got {image_slice.flatten()}'
          assert (
              np.abs(image_from_tuple_slice.flatten() - expected_slice				).max() < 1E-2
          ), F' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}'






@slow
@require_torch_gpu
class 						__lowerCAmelCase	(	unittest.TestCase      ):



      """simple docstring"""







      def 		snake_case_			(      self		:       Union[str, Any]				):
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()




      def 		snake_case_			(      self		:       List[Any]				):
          __lowercase		:			int	    =       load_numpy(
              '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
              '''/kandinsky/kandinsky_img2img_frog.npy'''				)

          __lowercase		:			List[str]	    =       load_image(
              '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png'''				)
          __lowercase		:			int	    =       'A red cartoon frog, 4k'

          __lowercase		:			Tuple	    =       KandinskyPriorPipeline.from_pretrained(
              '''kandinsky-community/kandinsky-2-1-prior'''				,       torch_dtype=torch.floataa				)
          pipe_prior.to(__a				)

          __lowercase		:			Tuple	    =       KandinskyImgaImgPipeline.from_pretrained(
              '''kandinsky-community/kandinsky-2-1'''				,       torch_dtype=torch.floataa				)
          __lowercase		:			Any	    =       pipeline.to(__a				)

          pipeline.set_progress_bar_config(disable=__a				)

          __lowercase		:			Dict	    =       torch.Generator(device='''cpu'''				).manual_seed(0				)
          __lowercase		:			Optional[int]	    =       pipe_prior(
              __a				,       generator=__a				,       num_inference_steps=5				,       negative_prompt=''''''				,       ).to_tuple()

          __lowercase		:			List[Any]	    =       pipeline(
              __a				,       image=__a				,       image_embeds=__a				,       negative_image_embeds=__a				,       generator=__a				,       num_inference_steps=100				,       height=768				,       width=768				,       strength=0.2				,       output_type='''np'''				,       )

          __lowercase		:			Tuple	    =       output.images[0]

          assert image.shape == (768, 768, 3)

          assert_mean_pixel_difference(__a				,       __a				)




import re
import string
from collections import Counter

import sacrebleu
import sacremoses
from packaging import version

import datasets


__lowerCAmelCase					:			Tuple			      =		"\n@inproceedings{xu-etal-2016-optimizing,\n    title = {Optimizing Statistical Machine Translation for Text Simplification},\n    authors={Xu, Wei and Napoles, Courtney and Pavlick, Ellie and Chen, Quanze and Callison-Burch, Chris},\n    journal = {Transactions of the Association for Computational Linguistics},\n    volume = {4},\n    year={2016},\n    url = {https://www.aclweb.org/anthology/Q16-1029},\n    pages = {401--415\n},\n@inproceedings{post-2018-call,\n    title = \"A Call for Clarity in Reporting {BLEU} Scores\",\n    author = \"Post, Matt\",\n    booktitle = \"Proceedings of the Third Conference on Machine Translation: Research Papers\",\n    month = oct,\n    year = \"2018\",\n    address = \"Belgium, Brussels\",\n    publisher = \"Association for Computational Linguistics\",\n    url = \"https://www.aclweb.org/anthology/W18-6319\",\n    pages = \"186--191\",\n}\n"

__lowerCAmelCase					:			Dict			      =		"\\nWIKI_SPLIT is the combination of three metrics SARI, EXACT and SACREBLEU\nIt can be used to evaluate the quality of machine-generated texts.\n"


__lowerCAmelCase					:			Optional[int]			      =		"\nCalculates sari score (between 0 and 100) given a list of source and predicted\nsentences, and a list of lists of reference sentences. It also computes the BLEU score as well as the exact match score.\nArgs:\n    sources: list of source sentences where each sentence should be a string.\n    predictions: list of predicted sentences where each sentence should be a string.\n    references: list of lists of reference sentences where each sentence should be a string.\nReturns:\n    sari: sari score\n    sacrebleu: sacrebleu score\n    exact: exact score\n\nExamples:\n    >>> sources=[\"About 95 species are currently accepted .\"]\n    >>> predictions=[\"About 95 you now get in .\"]\n    >>> references=[[\"About 95 species are currently known .\"]]\n    >>> wiki_split = datasets.load_metric(\"wiki_split\")\n    >>> results = wiki_split.compute(sources=sources, predictions=predictions, references=references)\n    >>> print(results)\n    {'sari': 21.805555555555557, 'sacrebleu': 14.535768424205482, 'exact': 0.0}\n"


def     UpperCAmelCase_  (						__lowerCAmelCase	) ->						str:

    def remove_articles(__lowerCAmelCase	):
        __lowercase		:			int	    =       re.compile(R'''\b(a|an|the)\b'''	,					re.UNICODE	)
        return re.sub(__lowerCAmelCase	,					''' '''	,					__lowerCAmelCase	)

    def white_space_fix(__lowerCAmelCase	):
        return " ".join(text.split()	)

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

    def lower(__lowerCAmelCase	):
        return text.lower()

    return white_space_fix(remove_articles(remove_punc(lower(__lowerCAmelCase	)	)	)	)


def     UpperCAmelCase_  (						__lowerCAmelCase	,					__lowerCAmelCase	) ->						List[str]:
    return int(normalize_answer(__lowerCAmelCase	) == normalize_answer(__lowerCAmelCase	)	)


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


def     UpperCAmelCase_  (						__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	) ->						Optional[int]:
    __lowercase		:			Union[str, Any]	    =       [rgram for rgrams in rgramslist for rgram in rgrams]
    __lowercase		:			Dict	    =       Counter(__lowerCAmelCase	)

    __lowercase		:			Union[str, Any]	    =       Counter(__lowerCAmelCase	)
    __lowercase		:			str	    =       Counter()
    for sgram, scount in sgramcounter.items():
        __lowercase		:			List[Any]	    =       scount * numref

    __lowercase		:			Optional[Any]	    =       Counter(__lowerCAmelCase	)
    __lowercase		:			Union[str, Any]	    =       Counter()
    for cgram, ccount in cgramcounter.items():
        __lowercase		:			Union[str, Any]	    =       ccount * numref

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

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

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

    # ADDITION
    __lowercase		:			int	    =       set(__lowerCAmelCase	) - set(__lowerCAmelCase	)
    __lowercase		:			Tuple	    =       set(__lowerCAmelCase	) & set(__lowerCAmelCase	)
    __lowercase		:			Any	    =       set(__lowerCAmelCase	) - set(__lowerCAmelCase	)

    __lowercase		:			Tuple	    =       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.
    __lowercase		:			int	    =       1
    __lowercase		:			Any	    =       1
    if len(__lowerCAmelCase	) > 0:
        __lowercase		:			Any	    =       addtmpscore / len(__lowerCAmelCase	)
    if len(__lowerCAmelCase	) > 0:
        __lowercase		:			str	    =       addtmpscore / len(__lowerCAmelCase	)
    __lowercase		:			int	    =       0
    if addscore_precision > 0 or addscore_recall > 0:
        __lowercase		:			Any	    =       2 * addscore_precision * addscore_recall / (addscore_precision + addscore_recall)

    return (keepscore, delscore_precision, addscore)


def     UpperCAmelCase_  (						__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	) ->						Union[str, Any]:
    __lowercase		:			Tuple	    =       len(__lowerCAmelCase	)

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

    __lowercase		:			Dict	    =       []
    __lowercase		:			str	    =       []
    __lowercase		:			Dict	    =       []
    __lowercase		:			List[Any]	    =       []
    for rsent in rsents:
        __lowercase		:			Any	    =       rsent.split(''' '''	)
        __lowercase		:			Optional[int]	    =       []
        __lowercase		:			List[Any]	    =       []
        __lowercase		:			Tuple	    =       []
        ragramslist.append(__lowerCAmelCase	)
        for i in range(0	,					len(__lowerCAmelCase	) - 1	):
            if i < len(__lowerCAmelCase	) - 1:
                __lowercase		:			List[str]	    =       ragrams[i] + ''' ''' + ragrams[i + 1]
                ragrams.append(__lowerCAmelCase	)
            if i < len(__lowerCAmelCase	) - 2:
                __lowercase		:			Union[str, Any]	    =       ragrams[i] + ''' ''' + ragrams[i + 1] + ''' ''' + ragrams[i + 2]
                ragrams.append(__lowerCAmelCase	)
            if i < len(__lowerCAmelCase	) - 3:
                __lowercase		:			int	    =       ragrams[i] + ''' ''' + ragrams[i + 1] + ''' ''' + ragrams[i + 2] + ''' ''' + ragrams[i + 3]
                ragrams.append(__lowerCAmelCase	)
        ragramslist.append(__lowerCAmelCase	)
        ragramslist.append(__lowerCAmelCase	)
        ragramslist.append(__lowerCAmelCase	)

    for i in range(0	,					len(__lowerCAmelCase	) - 1	):
        if i < len(__lowerCAmelCase	) - 1:
            __lowercase		:			Dict	    =       sagrams[i] + ''' ''' + sagrams[i + 1]
            sagrams.append(__lowerCAmelCase	)
        if i < len(__lowerCAmelCase	) - 2:
            __lowercase		:			int	    =       sagrams[i] + ''' ''' + sagrams[i + 1] + ''' ''' + sagrams[i + 2]
            sagrams.append(__lowerCAmelCase	)
        if i < len(__lowerCAmelCase	) - 3:
            __lowercase		:			str	    =       sagrams[i] + ''' ''' + sagrams[i + 1] + ''' ''' + sagrams[i + 2] + ''' ''' + sagrams[i + 3]
            sagrams.append(__lowerCAmelCase	)

    for i in range(0	,					len(__lowerCAmelCase	) - 1	):
        if i < len(__lowerCAmelCase	) - 1:
            __lowercase		:			int	    =       cagrams[i] + ''' ''' + cagrams[i + 1]
            cagrams.append(__lowerCAmelCase	)
        if i < len(__lowerCAmelCase	) - 2:
            __lowercase		:			Any	    =       cagrams[i] + ''' ''' + cagrams[i + 1] + ''' ''' + cagrams[i + 2]
            cagrams.append(__lowerCAmelCase	)
        if i < len(__lowerCAmelCase	) - 3:
            __lowercase		:			Optional[int]	    =       cagrams[i] + ''' ''' + cagrams[i + 1] + ''' ''' + cagrams[i + 2] + ''' ''' + cagrams[i + 3]
            cagrams.append(__lowerCAmelCase	)

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


def     UpperCAmelCase_  (						__lowerCAmelCase	,					__lowerCAmelCase = True	,					__lowerCAmelCase = "13a"	,					__lowerCAmelCase = True	) ->						List[str]:
    # 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:
        __lowercase		:			List[Any]	    =       sentence.lower()

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

    if not return_str:
        __lowercase		:			List[Any]	    =       normalized_sent.split()

    return normalized_sent


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


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





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



      """simple docstring"""







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


