






'''simple docstring'''



import os
import re
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

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


__UpperCamelCase   :							Optional[Any]         =   logging.get_logger(__name__)

__UpperCamelCase   :							Union[str, Any]         =   {"""vocab_file""": """spiece.model"""}

__UpperCamelCase   :							Dict         =   {
    """vocab_file""": {
        """google/bigbird-roberta-base""": """https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model""",
        """google/bigbird-roberta-large""": (
            """https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model"""
        ),
        """google/bigbird-base-trivia-itc""": (
            """https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model"""
        ),
    }
}

__UpperCamelCase   :							List[Any]         =   {
    """google/bigbird-roberta-base""": 4096,
    """google/bigbird-roberta-large""": 4096,
    """google/bigbird-base-trivia-itc""": 4096,
}
class 				__SCREAMING_SNAKE_CASE							(				_lowerCAmelCase  ):
      __a	       =VOCAB_FILES_NAMES
      __a	       =PRETRAINED_VOCAB_FILES_MAP
      __a	       =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      __a	       =["input_ids", "attention_mask"]
      __a	       =[]




      def __init__(	self			, lowerCamelCase			, lowerCamelCase="<unk>"			, lowerCamelCase="<s>"			, lowerCamelCase="</s>"			, lowerCamelCase="<pad>"			, lowerCamelCase="[SEP]"			, lowerCamelCase="[MASK]"			, lowerCamelCase="[CLS]"			, lowerCamelCase = None			, **lowerCamelCase			, )				->None:



         '''simple docstring'''
         __a					  =     AddedToken(lowerCamelCase			, lstrip=lowerCamelCase			, rstrip=lowerCamelCase   ) if isinstance(lowerCamelCase			, lowerCamelCase   ) else bos_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
         __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 cls_token
         __a					  =     AddedToken(lowerCamelCase			, lstrip=lowerCamelCase			, rstrip=lowerCamelCase   ) if isinstance(lowerCamelCase			, lowerCamelCase   ) else sep_token

         # Mask token behave like a normal word, i.e. include the space before it
         __a					  =     AddedToken(lowerCamelCase			, lstrip=lowerCamelCase			, rstrip=lowerCamelCase   ) if isinstance(lowerCamelCase			, lowerCamelCase   ) else mask_token

         __a					  =     {} if sp_model_kwargs is None else sp_model_kwargs

         super().__init__(
             bos_token=lowerCamelCase			, eos_token=lowerCamelCase			, unk_token=lowerCamelCase			, pad_token=lowerCamelCase			, sep_token=lowerCamelCase			, mask_token=lowerCamelCase			, cls_token=lowerCamelCase			, sp_model_kwargs=self.sp_model_kwargs			, **lowerCamelCase			, )

         __a					  =     vocab_file

         __a					  =     spm.SentencePieceProcessor(**self.sp_model_kwargs   )
         self.sp_model.Load(lowerCamelCase   )




      @property
      def 			__UpperCamelCase				(	self   )				->List[str]:



         '''simple docstring'''
         return self.sp_model.get_piece_size()




      def 			__UpperCamelCase				(	self   )				->Tuple:



         '''simple docstring'''
         __a					  =     {self.convert_ids_to_tokens(lowerCamelCase   ): i for i in range(self.vocab_size   )}
         vocab.update(self.added_tokens_encoder   )
         return vocab




      def __getstate__(	self   )				->List[Any]:



         '''simple docstring'''
         __a					  =     self.__dict__.copy()
         __a					  =     None
         return state




      def __setstate__(	self			, lowerCamelCase   )				->Dict:



         '''simple docstring'''
         __a					  =     d

         # for backward compatibility
         if not hasattr(self			, 'sp_model_kwargs'   ):
            __a					  =     {}

         __a					  =     spm.SentencePieceProcessor(**self.sp_model_kwargs   )
         self.sp_model.Load(self.vocab_file   )




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



         '''simple docstring'''
         return self.sp_model.encode(lowerCamelCase			, out_type=lowerCamelCase   )




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



         '''simple docstring'''
         return self.sp_model.piece_to_id(lowerCamelCase   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->int:



         '''simple docstring'''
         __a					  =     self.sp_model.IdToPiece(lowerCamelCase   )
         return token




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



         '''simple docstring'''
         __a					  =     []
         __a					  =     ''
         __a					  =     False
         for token in tokens:
            # make sure that special tokens are not decoded using sentencepiece model
            if token in self.all_special_tokens:
               if not prev_is_special:
                  out_string += " "
               out_string += self.sp_model.decode(lowerCamelCase   ) + token
               __a					  =     True
               __a					  =     []
            else:
               current_sub_tokens.append(lowerCamelCase   )
               __a					  =     False
         out_string += self.sp_model.decode(lowerCamelCase   )
         return out_string.strip()




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase = False			, lowerCamelCase = None			, lowerCamelCase = True			, **lowerCamelCase			, )				->str:



         '''simple docstring'''
         __a					  =     kwargs.pop('use_source_tokenizer'			, lowerCamelCase   )

         __a					  =     self.convert_ids_to_tokens(lowerCamelCase			, skip_special_tokens=lowerCamelCase   )

         # To avoid mixing byte-level and unicode for byte-level BPT
         # we need to build string separately for added tokens and byte-level tokens
         # cf. https://github.com/huggingface/transformers/issues/1133
         __a					  =     []
         __a					  =     []
         for token in filtered_tokens:
            if skip_special_tokens and token in self.all_special_ids:
               continue
            if token in self.added_tokens_encoder:
               if current_sub_text:
                  sub_texts.append(self.convert_tokens_to_string(lowerCamelCase   )   )
                  __a					  =     []
               sub_texts.append(lowerCamelCase   )
            else:
               current_sub_text.append(lowerCamelCase   )
         if current_sub_text:
            sub_texts.append(self.convert_tokens_to_string(lowerCamelCase   )   )

         # Mimic the behavior of the Rust tokenizer:
         # No space before [MASK] and [SEP]
         if spaces_between_special_tokens:
            __a					  =     re.sub(r' (\[(MASK|SEP)\])'			, r'\1'			, ' '.join(lowerCamelCase   )   )
         else:
            __a					  =     ''.join(lowerCamelCase   )

         __a					  =     (
             clean_up_tokenization_spaces
             if clean_up_tokenization_spaces is not None
             else self.clean_up_tokenization_spaces
         )
         if clean_up_tokenization_spaces:
            __a					  =     self.clean_up_tokenization(lowerCamelCase   )
            return clean_text
         else:
            return text




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



         '''simple docstring'''
         if not os.path.isdir(lowerCamelCase   ):
            logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""   )
            return
         __a					  =     os.path.join(
             lowerCamelCase			, (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']   )

         if os.path.abspath(self.vocab_file   ) != os.path.abspath(lowerCamelCase   ) and os.path.isfile(self.vocab_file   ):
            copyfile(self.vocab_file			, lowerCamelCase   )
         elif not os.path.isfile(self.vocab_file   ):
            with open(lowerCamelCase			, 'wb'   ) as fi:
               __a					  =     self.sp_model.serialized_model_proto()
               fi.write(lowerCamelCase   )

         return (out_vocab_file,)




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



         '''simple docstring'''
         if token_ids_a is None:
            return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
         __a					  =     [self.cls_token_id]
         __a					  =     [self.sep_token_id]
         return cls + token_ids_a + sep + token_ids_a + sep




      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   )

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






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



         '''simple docstring'''
         __a					  =     [self.sep_token_id]
         __a					  =     [self.cls_token_id]
         if token_ids_a is None:
            return len(cls + token_ids_a + sep   ) * [0]
         return len(cls + token_ids_a + sep   ) * [0] + len(token_ids_a + sep   ) * [1]







'''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"]'   )