






'''simple docstring'''



from scipy.stats import pearsonr, spearmanr
from sklearn.metrics import fa_score, matthews_corrcoef

import datasets


__UpperCamelCase   :							Optional[int]         =   """\
@inproceedings{wang2019glue,
  title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding},
  author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.},
  note={In the Proceedings of ICLR.},
  year={2019}
}
"""

__UpperCamelCase   :							Tuple         =   """\
GLUE, the General Language Understanding Evaluation benchmark
(https://gluebenchmark.com/) is a collection of resources for training,
evaluating, and analyzing natural language understanding systems.
"""

__UpperCamelCase   :							Optional[int]         =   """
Compute GLUE evaluation metric associated to each GLUE dataset.
Args:
    predictions: list of predictions to score.
        Each translation should be tokenized into a list of tokens.
    references: list of lists of references for each translation.
        Each reference should be tokenized into a list of tokens.
Returns: depending on the GLUE subset, one or several of:
    \"accuracy\": Accuracy
    \"f1\": F1 score
    \"pearson\": Pearson Correlation
    \"spearmanr\": Spearman Correlation
    \"matthews_correlation\": Matthew Correlation
Examples:

    >>> glue_metric = datasets.load_metric('glue', 'sst2')  # 'sst2' or any of [\"mnli\", \"mnli_mismatched\", \"mnli_matched\", \"qnli\", \"rte\", \"wnli\", \"hans\"]
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0}

    >>> glue_metric = datasets.load_metric('glue', 'mrpc')  # 'mrpc' or 'qqp'
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0, 'f1': 1.0}

    >>> glue_metric = datasets.load_metric('glue', 'stsb')
    >>> references = [0., 1., 2., 3., 4., 5.]
    >>> predictions = [0., 1., 2., 3., 4., 5.]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print({\"pearson\": round(results[\"pearson\"], 2), \"spearmanr\": round(results[\"spearmanr\"], 2)})
    {'pearson': 1.0, 'spearmanr': 1.0}

    >>> glue_metric = datasets.load_metric('glue', 'cola')
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'matthews_correlation': 1.0}
"""



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

   """simple docstring"""
   return float((preds == labels).mean()     )



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

   """simple docstring"""
   __a					  =     simple_accuracy(SCREAMING_SNAKE_CASE__,       SCREAMING_SNAKE_CASE__     )
   __a					  =     float(fa_score(y_true=SCREAMING_SNAKE_CASE__,       y_pred=SCREAMING_SNAKE_CASE__     )     )
   return {
       "accuracy": acc,
       "f1": fa,
   }



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

   """simple docstring"""
   __a					  =     float(pearsonr(SCREAMING_SNAKE_CASE__,       SCREAMING_SNAKE_CASE__     )[0]     )
   __a					  =     float(spearmanr(SCREAMING_SNAKE_CASE__,       SCREAMING_SNAKE_CASE__     )[0]     )
   return {
       "pearson": pearson_corr,
       "spearmanr": spearman_corr,
   }



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




      def 			__UpperCamelCase				(	self   )				->int:



         '''simple docstring'''
         if self.config_name not in [
             "sst2",
             "mnli",
             "mnli_mismatched",
             "mnli_matched",
             "cola",
             "stsb",
             "mrpc",
             "qqp",
             "qnli",
             "rte",
             "wnli",
             "hans",
         ]:
            raise KeyError(
                'You should supply a configuration name selected in '
                '["sst2", "mnli", "mnli_mismatched", "mnli_matched", '
                '"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]'   )
         return datasets.MetricInfo(
             description=_DESCRIPTION			, citation=_CITATION			, inputs_description=_KWARGS_DESCRIPTION			, features=datasets.Features(
                 {
                     'predictions': datasets.Value('int64' if self.config_name != 'stsb' else 'float32'   ),
                     'references': datasets.Value('int64' if self.config_name != 'stsb' else 'float32'   ),
                 }   )			, codebase_urls=[]			, reference_urls=[]			, format='numpy'			, )






      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase   )				->Tuple:



         '''simple docstring'''
         if self.config_name == "cola":
            return {"matthews_correlation": matthews_corrcoef(lowerCamelCase			, lowerCamelCase   )}
         elif self.config_name == "stsb":
            return pearson_and_spearman(lowerCamelCase			, lowerCamelCase   )
         elif self.config_name in ["mrpc", "qqp"]:
            return acc_and_fa(lowerCamelCase			, lowerCamelCase   )
         elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]:
            return {"accuracy": simple_accuracy(lowerCamelCase			, lowerCamelCase   )}
         else:
            raise KeyError(
                'You should supply a configuration name selected in '
                '["sst2", "mnli", "mnli_mismatched", "mnli_matched", '
                '"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]'   )







'''simple docstring'''



import warnings
from typing import Dict, List, Optional, Tuple

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


__UpperCamelCase   :							int         =   logging.get_logger(__name__)
class 				__SCREAMING_SNAKE_CASE							(				_lowerCAmelCase  ):
      __a	       =["input_ids", "attention_mask"]




      def __init__(	self			, lowerCamelCase="</s>"			, lowerCamelCase="<unk>"			, lowerCamelCase="<pad>"			, lowerCamelCase=125			, lowerCamelCase=None			, **lowerCamelCase			, )				->None:



         '''simple docstring'''
         # Add extra_ids to the special token list
         if extra_ids > 0 and additional_special_tokens is None:
            __a					  =     [F"""<extra_id_{i}>""" for i in range(lowerCamelCase   )]
         elif extra_ids > 0 and additional_special_tokens is not None:
            # Check that we have the right number of extra_id special tokens
            __a					  =     len(set(filter(lambda lowerCamelCase   : bool('extra_id' in str(lowerCamelCase   )   )			, lowerCamelCase   )   )   )
            if extra_tokens != extra_ids:
               raise ValueError(
                   F"""Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are"""
                   ' provided to ByT5Tokenizer. In this case the additional_special_tokens must include the'
                   ' extra_ids tokens'   )

         __a					  =     AddedToken(lowerCamelCase			, lstrip=lowerCamelCase			, rstrip=lowerCamelCase   ) if isinstance(lowerCamelCase			, lowerCamelCase   ) else pad_token
         __a					  =     AddedToken(lowerCamelCase			, lstrip=lowerCamelCase			, rstrip=lowerCamelCase   ) if isinstance(lowerCamelCase			, lowerCamelCase   ) else eos_token
         __a					  =     AddedToken(lowerCamelCase			, lstrip=lowerCamelCase			, rstrip=lowerCamelCase   ) if isinstance(lowerCamelCase			, lowerCamelCase   ) else unk_token

         super().__init__(
             eos_token=lowerCamelCase			, unk_token=lowerCamelCase			, pad_token=lowerCamelCase			, extra_ids=lowerCamelCase			, additional_special_tokens=lowerCamelCase			, **lowerCamelCase			, )

         __a					  =     extra_ids

         __a					  =     2**8  # utf is 8 bits

         # define special tokens dict
         __a					  =     {
             self.pad_token: 0,
             self.eos_token: 1,
             self.unk_token: 2,
         }
         __a					  =     len(self.special_tokens_encoder   )
         __a					  =     len(lowerCamelCase   )
         for i, token in enumerate(lowerCamelCase   ):
            __a					  =     self.vocab_size + i - n
         __a					  =     {v: k for k, v in self.special_tokens_encoder.items()}




      @property
      def 			__UpperCamelCase				(	self   )				->Any:



         '''simple docstring'''
         return self._utf_vocab_size + self._num_special_tokens + self._extra_ids




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase = None			, lowerCamelCase = False   )				->List[int]:



         '''simple docstring'''
         if already_has_special_tokens:
            return super().get_special_tokens_mask(
                token_ids_a=lowerCamelCase			, token_ids_a=lowerCamelCase			, already_has_special_tokens=lowerCamelCase   )

         # normal case: some special tokens
         if token_ids_a is None:
            return ([0] * len(lowerCamelCase   )) + [1]
         return ([0] * len(lowerCamelCase   )) + [1] + ([0] * len(lowerCamelCase   )) + [1]




      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->List[int]:



         '''simple docstring'''
         if len(lowerCamelCase   ) > 0 and token_ids[-1] == self.eos_token_id:
            warnings.warn(
                F"""This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated"""
                ' eos tokens being added.'   )
            return token_ids
         else:
            return token_ids + [self.eos_token_id]




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase = None   )				->List[int]:



         '''simple docstring'''
         __a					  =     [self.eos_token_id]

         if token_ids_a is None:
            return len(token_ids_a + eos   ) * [0]
         return len(token_ids_a + eos + token_ids_a + eos   ) * [0]




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase = None   )				->List[int]:



         '''simple docstring'''
         __a					  =     self._add_eos_if_not_present(lowerCamelCase   )
         if token_ids_a is None:
            return token_ids_a
         else:
            __a					  =     self._add_eos_if_not_present(lowerCamelCase   )
            return token_ids_a + token_ids_a




      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->List[str]:



         '''simple docstring'''
         __a					  =     [chr(lowerCamelCase   ) for i in text.encode('utf-8'   )]
         return tokens




      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->Optional[Any]:



         '''simple docstring'''
         if token in self.special_tokens_encoder:
            __a					  =     self.special_tokens_encoder[token]
         elif token in self.added_tokens_encoder:
            __a					  =     self.added_tokens_encoder[token]
         elif len(lowerCamelCase   ) != 1:
            __a					  =     self.unk_token_id
         else:
            __a					  =     ord(lowerCamelCase   ) + self._num_special_tokens
         return token_id




      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->Tuple:



         '''simple docstring'''
         if index in self.special_tokens_decoder:
            __a					  =     self.special_tokens_decoder[index]
         else:
            __a					  =     chr(index - self._num_special_tokens   )
         return token




      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->Optional[Any]:



         '''simple docstring'''
         __a					  =     B''
         for token in tokens:
            if token in self.special_tokens_decoder:
               __a					  =     self.special_tokens_decoder[token].encode('utf-8'   )
            elif token in self.added_tokens_decoder:
               __a					  =     self.special_tokens_decoder[token].encode('utf-8'   )
            elif token in self.special_tokens_encoder:
               __a					  =     token.encode('utf-8'   )
            elif token in self.added_tokens_encoder:
               __a					  =     token.encode('utf-8'   )
            else:
               __a					  =     bytes([ord(lowerCamelCase   )]   )
            bstring += tok_string
         __a					  =     bstring.decode('utf-8'			, errors='ignore'   )
         return string






      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase = None   )				->Tuple[str]:



         '''simple docstring'''
         return ()