
"""simple docstring"""


# flake8: noqa
# Lint as: python3

from typing import Dict, List, Optional, Type

from .. import config
from ..utils import logging
from .formatting import (
    ArrowFormatter,
    CustomFormatter,
    Formatter,
    PandasFormatter,
    PythonFormatter,
    TensorFormatter,
    format_table,
    query_table,
)
from .np_formatter import NumpyFormatter


SCREAMING_SNAKE_CASE      =     logging.get_logger(__name__)

SCREAMING_SNAKE_CASE      =     {}
SCREAMING_SNAKE_CASE      =     {}
SCREAMING_SNAKE_CASE      =     {}



def 	lowerCamelCase__			(							UpperCAmelCase_		,      UpperCAmelCase_		,      UpperCAmelCase_ = None		,      )->							Tuple:







       """simple docstring"""
       UpperCamelCase					 =		aliases if aliases is not None else []
       if format_type in _FORMAT_TYPES:
              logger.warning(
                  F"Overwriting format type '{format_type}' ({_FORMAT_TYPES[format_type].__name__} -> {formatter_cls.__name__})"				)
       UpperCamelCase					 =		formatter_cls
       for alias in set(aliases + [format_type]				):
              if alias in _FORMAT_TYPES_ALIASES:
                     logger.warning(
                         F"Overwriting format type alias '{alias}' ({_FORMAT_TYPES_ALIASES[alias]} -> {format_type})"				)
              UpperCamelCase					 =		format_type



def 	lowerCamelCase__			(							UpperCAmelCase_		,      UpperCAmelCase_		,      UpperCAmelCase_ = None				)->							Any:







       """simple docstring"""
       UpperCamelCase					 =		aliases if aliases is not None else []
       for alias in set(aliases + [format_type]				):
              UpperCamelCase					 =		unavailable_error


# Here we define all the available formatting functions that can be used by `Dataset.set_format`
_register_formatter(PythonFormatter, None, aliases=["""python"""])
_register_formatter(ArrowFormatter, """arrow""", aliases=["""pa""", """pyarrow"""])
_register_formatter(NumpyFormatter, """numpy""", aliases=["""np"""])
_register_formatter(PandasFormatter, """pandas""", aliases=["""pd"""])
_register_formatter(CustomFormatter, """custom""")

if config.TORCH_AVAILABLE:
      from .torch_formatter import TorchFormatter

      _register_formatter(TorchFormatter, """torch""", aliases=["""pt""", """pytorch"""])
else:
      SCREAMING_SNAKE_CASE      =     ValueError("""PyTorch needs to be installed to be able to return PyTorch tensors.""")
      _register_unavailable_formatter(_torch_error, """torch""", aliases=["""pt""", """pytorch"""])

if config.TF_AVAILABLE:
      from .tf_formatter import TFFormatter

      _register_formatter(TFFormatter, """tensorflow""", aliases=["""tf"""])
else:
      SCREAMING_SNAKE_CASE      =     ValueError("""Tensorflow needs to be installed to be able to return Tensorflow tensors.""")
      _register_unavailable_formatter(_tf_error, """tensorflow""", aliases=["""tf"""])

if config.JAX_AVAILABLE:
      from .jax_formatter import JaxFormatter

      _register_formatter(JaxFormatter, """jax""", aliases=[])
else:
      SCREAMING_SNAKE_CASE      =     ValueError("""JAX needs to be installed to be able to return JAX arrays.""")
      _register_unavailable_formatter(_jax_error, """jax""", aliases=[])



def 	lowerCamelCase__			(							UpperCAmelCase_				)->							Optional[str]:







       """simple docstring"""
       if format_type in _FORMAT_TYPES_ALIASES:
              return _FORMAT_TYPES_ALIASES[format_type]
       else:
              return format_type



def 	lowerCamelCase__			(							UpperCAmelCase_		,      **UpperCAmelCase_				)->							Formatter:







       """simple docstring"""
       UpperCamelCase					 =		get_format_type_from_alias(UpperCAmelCase_				)
       if format_type in _FORMAT_TYPES:
              return _FORMAT_TYPES[format_type](**UpperCAmelCase_				)
       if format_type in _FORMAT_TYPES_ALIASES_UNAVAILABLE:
              raise _FORMAT_TYPES_ALIASES_UNAVAILABLE[format_type]
       else:
              raise ValueError(
                  F"Return type should be None or selected in {list(type for type in _FORMAT_TYPES.keys() if type != None				)}, but got '{format_type}'"				)







"""simple docstring"""


import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


SCREAMING_SNAKE_CASE      =     logging.getLogger(__name__)



@dataclass(frozen=_lowerCAmelCase	)
class   __a  :
       UpperCamelCase_		:					str
       UpperCamelCase_		:					str
       UpperCamelCase_		:					Optional[str]   =			None
       UpperCamelCase_		:					Optional[str]   =			None
       UpperCamelCase_		:					Optional[str]   =			None





@dataclass(frozen=_lowerCAmelCase	)
class   __a  :
       UpperCamelCase_		:					List[int]
       UpperCamelCase_		:					Optional[List[int]]   =			None
       UpperCamelCase_		:					Optional[List[int]]   =			None
       UpperCamelCase_		:					Optional[Union[int, float]]   =			None
       UpperCamelCase_		:					Optional[int]   =			None


if is_torch_available():
      import torch
      from torch.utils.data import Dataset



      class   __a  (      _lowerCAmelCase	):
             UpperCamelCase_		:					List[InputFeatures]






             def __init__(  self :							Union[str, Any]       ,      UpperCAmelCase_ :							str       ,      UpperCAmelCase_ :							PreTrainedTokenizer       ,      UpperCAmelCase_ :							str       ,      UpperCAmelCase_ :							Optional[int] = None       ,      UpperCAmelCase_ :							Optional[Any]=False       ,      UpperCAmelCase_ :							bool = False       ,      )->  Dict:







                    """simple docstring"""
                    UpperCamelCase					 =		hans_processors[task]()

                    UpperCamelCase					 =		os.path.join(
                        UpperCAmelCase_       ,      "cached_{}_{}_{}_{}".format(
                            "dev" if evaluate else "train"       ,      tokenizer.__class__.__name__       ,      str(UpperCAmelCase_			)       ,      UpperCAmelCase_       ,      )       ,      )
                    UpperCamelCase					 =		processor.get_labels()
                    if tokenizer.__class__ in (
                        RobertaTokenizer,
                        RobertaTokenizerFast,
                        XLMRobertaTokenizer,
                        BartTokenizer,
                        BartTokenizerFast,
                    ):
                           # HACK(label indices are swapped in RoBERTa pretrained model)
                           UpperCamelCase ,       UpperCamelCase					 =		label_list[2], label_list[1]
                    UpperCamelCase					 =		label_list

                    # Make sure only the first process in distributed training processes the dataset,
                    # and the others will use the cache.
                    UpperCamelCase					 =		cached_features_file + ".lock"
                    with FileLock(UpperCAmelCase_			):
                           if os.path.exists(UpperCAmelCase_			) and not overwrite_cache:
                                  logger.info(f"Loading features from cached file {cached_features_file}"			)
                                  UpperCamelCase					 =		torch.load(UpperCAmelCase_			)
                           else:
                                  logger.info(f"Creating features from dataset file at {data_dir}"			)

                                  UpperCamelCase					 =		(
                                      processor.get_dev_examples(UpperCAmelCase_			) if evaluate else processor.get_train_examples(UpperCAmelCase_			)
                                  )

                                  logger.info("Training examples: %s"       ,      len(UpperCAmelCase_			)			)
                                  UpperCamelCase					 =		hans_convert_examples_to_features(UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_			)
                                  logger.info("Saving features into cached file %s"       ,      UpperCAmelCase_			)
                                  torch.save(self.features       ,      UpperCAmelCase_			)






             def __len__(  self :							Optional[Any]			)->  List[Any]:







                    """simple docstring"""
                    return len(self.features			)






             def __getitem__(  self :							Optional[Any]       ,      UpperCAmelCase_ :							Any			)->  InputFeatures:







                    """simple docstring"""
                    return self.features[i]






             def 	_SCREAMING_SNAKE_CASE       (  self :							str			)->  List[Any]:







                    """simple docstring"""
                    return self.label_list


if is_tf_available():
      import tensorflow as tf



      class   __a  :
             UpperCamelCase_		:					List[InputFeatures]






             def __init__(  self :							int       ,      UpperCAmelCase_ :							str       ,      UpperCAmelCase_ :							PreTrainedTokenizer       ,      UpperCAmelCase_ :							str       ,      UpperCAmelCase_ :							Optional[int] = 128       ,      UpperCAmelCase_ :							Optional[Any]=False       ,      UpperCAmelCase_ :							bool = False       ,      )->  Union[str, Any]:







                    """simple docstring"""
                    UpperCamelCase					 =		hans_processors[task]()
                    UpperCamelCase					 =		processor.get_labels()
                    if tokenizer.__class__ in (
                        RobertaTokenizer,
                        RobertaTokenizerFast,
                        XLMRobertaTokenizer,
                        BartTokenizer,
                        BartTokenizerFast,
                    ):
                           # HACK(label indices are swapped in RoBERTa pretrained model)
                           UpperCamelCase ,       UpperCamelCase					 =		label_list[2], label_list[1]
                    UpperCamelCase					 =		label_list

                    UpperCamelCase					 =		processor.get_dev_examples(UpperCAmelCase_			) if evaluate else processor.get_train_examples(UpperCAmelCase_			)
                    UpperCamelCase					 =		hans_convert_examples_to_features(UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_			)

                    def gen():
                           for ex_index, ex in tqdm.tqdm(enumerate(self.features			)       ,      desc="convert examples to features"			):
                                  if ex_index % 10_000 == 0:
                                         logger.info("Writing example %d of %d" % (ex_index, len(UpperCAmelCase_			))			)

                                  yield (
                                      {
                                          "example_id": 0,
                                          "input_ids": ex.input_ids,
                                          "attention_mask": ex.attention_mask,
                                          "token_type_ids": ex.token_type_ids,
                                      },
                                      ex.label,
                                  )

                    UpperCamelCase					 =		tf.data.Dataset.from_generator(
                        UpperCAmelCase_       ,      (
                            {
                                "example_id": tf.intaa,
                                "input_ids": tf.intaa,
                                "attention_mask": tf.intaa,
                                "token_type_ids": tf.intaa,
                            },
                            tf.intaa,
                        )       ,      (
                            {
                                "example_id": tf.TensorShape([]			),
                                "input_ids": tf.TensorShape([None, None]			),
                                "attention_mask": tf.TensorShape([None, None]			),
                                "token_type_ids": tf.TensorShape([None, None]			),
                            },
                            tf.TensorShape([]			),
                        )       ,      )






             def 	_SCREAMING_SNAKE_CASE       (  self :							Tuple			)->  Tuple:







                    """simple docstring"""
                    return self.dataset






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







                    """simple docstring"""
                    return len(self.features			)






             def __getitem__(  self :							Tuple       ,      UpperCAmelCase_ :							List[str]			)->  InputFeatures:







                    """simple docstring"""
                    return self.features[i]






             def 	_SCREAMING_SNAKE_CASE       (  self :							List[str]			)->  List[str]:







                    """simple docstring"""
                    return self.label_list



class   __a  (      _lowerCAmelCase	):






       def 	_SCREAMING_SNAKE_CASE       (  self :							str       ,      UpperCAmelCase_ :							Tuple			)->  Tuple:







              """simple docstring"""
              return self._create_examples(self._read_tsv(os.path.join(UpperCAmelCase_       ,      "heuristics_train_set.txt"			)			)       ,      "train"			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							int       ,      UpperCAmelCase_ :							List[str]			)->  Dict:







              """simple docstring"""
              return self._create_examples(self._read_tsv(os.path.join(UpperCAmelCase_       ,      "heuristics_evaluation_set.txt"			)			)       ,      "dev"			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]			)->  Optional[Any]:







              """simple docstring"""
              return ["contradiction", "entailment", "neutral"]






       def 	_SCREAMING_SNAKE_CASE       (  self :							Dict       ,      UpperCAmelCase_ :							Dict       ,      UpperCAmelCase_ :							str			)->  str:







              """simple docstring"""
              UpperCamelCase					 =		[]
              for i, line in enumerate(UpperCAmelCase_			):
                     if i == 0:
                            continue
                     UpperCamelCase					 =		"%s-%s" % (set_type, line[0])
                     UpperCamelCase					 =		line[5]
                     UpperCamelCase					 =		line[6]
                     UpperCamelCase					 =		line[7][2:] if line[7].startswith("ex"			) else line[7]
                     UpperCamelCase					 =		line[0]
                     examples.append(InputExample(guid=UpperCAmelCase_       ,      text_a=UpperCAmelCase_       ,      text_b=UpperCAmelCase_       ,      label=UpperCAmelCase_       ,      pairID=UpperCAmelCase_			)			)
              return examples





def 	lowerCamelCase__			(							UpperCAmelCase_		,      UpperCAmelCase_		,      UpperCAmelCase_		,      UpperCAmelCase_		,      )->							Union[str, Any]:







       """simple docstring"""
       UpperCamelCase					 =		{label: i for i, label in enumerate(UpperCAmelCase_				)}

       UpperCamelCase					 =		[]
       for ex_index, example in tqdm.tqdm(enumerate(UpperCAmelCase_				)		,      desc="convert examples to features"				):
              if ex_index % 1_00_00 == 0:
                     logger.info("Writing example %d" % (ex_index)				)

              UpperCamelCase					 =		tokenizer(
                  example.text_a		,      example.text_b		,      add_special_tokens=UpperCAmelCase_		,      max_length=UpperCAmelCase_		,      padding="max_length"		,      truncation=UpperCAmelCase_		,      return_overflowing_tokens=UpperCAmelCase_		,      )

              UpperCamelCase					 =		label_map[example.label] if example.label in label_map else 0

              UpperCamelCase					 =		int(example.pairID				)

              features.append(InputFeatures(**UpperCAmelCase_		,      label=UpperCAmelCase_		,      pairID=UpperCAmelCase_				)				)

       for i, example in enumerate(examples[:5]				):
              logger.info("*** Example ***"				)
              logger.info(F"guid: {example}"				)
              logger.info(F"features: {features[i]}"				)

       return features


SCREAMING_SNAKE_CASE      =     {
    """hans""": 3,
}

SCREAMING_SNAKE_CASE      =     {
    """hans""": HansProcessor,
}





