


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,
)


UpperCAmelCase_      : List[str]  					=       logging.getLogger(__name__)
@dataclass(frozen=lowercase__			)
class  SCREAMING_SNAKE_CASE__  :
      snake_case__				:     str
      snake_case__				:     str
      snake_case__				:     Optional[str]        =						None
      snake_case__				:     Optional[str]        =						None
      snake_case__				:     Optional[str]        =						None
@dataclass(frozen=lowercase__			)
class  SCREAMING_SNAKE_CASE__  :
      snake_case__				:     List[int]
      snake_case__				:     Optional[List[int]]        =						None
      snake_case__				:     Optional[List[int]]        =						None
      snake_case__				:     Optional[Union[int, float]]        =						None
      snake_case__				:     Optional[int]        =						None


if is_torch_available():
   import torch
   from torch.utils.data import Dataset
   class  SCREAMING_SNAKE_CASE__  (				lowercase__			):
         snake_case__				:     List[InputFeatures]
         def __init__(       self				:       str     ,							SCREAMING_SNAKE_CASE__				:       str     ,							SCREAMING_SNAKE_CASE__				:       PreTrainedTokenizer     ,							SCREAMING_SNAKE_CASE__				:       str     ,							SCREAMING_SNAKE_CASE__				:       Optional[int] = None     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=False     ,							SCREAMING_SNAKE_CASE__				:       bool = False     ,							)		->			List[Any]:
                a_					:		Dict							=  hans_processors[task]()

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

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

                              a_					:		Optional[Any]							=  (
                                  processor.get_dev_examples(SCREAMING_SNAKE_CASE__     ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE__     )
                              )

                              logger.info('Training examples: %s'     ,							len(SCREAMING_SNAKE_CASE__     )     )
                              a_					:		Union[str, Any]							=  hans_convert_examples_to_features(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )
                              logger.info('Saving features into cached file %s'     ,							SCREAMING_SNAKE_CASE__     )
                              torch.save(self.features     ,							SCREAMING_SNAKE_CASE__     )
         def __len__(       self				:       List[Any]     )		->			Optional[Any]:
                return len(self.features     )
         def __getitem__(       self				:       Any     ,							SCREAMING_SNAKE_CASE__				:       List[Any]     )		->			InputFeatures:
                return self.features[i]

         def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     )		->			List[Any]:
                return self.label_list


if is_tf_available():
   import tensorflow as tf
   class  SCREAMING_SNAKE_CASE__  :
         snake_case__				:     List[InputFeatures]
         def __init__(       self				:       Union[str, Any]     ,							SCREAMING_SNAKE_CASE__				:       str     ,							SCREAMING_SNAKE_CASE__				:       PreTrainedTokenizer     ,							SCREAMING_SNAKE_CASE__				:       str     ,							SCREAMING_SNAKE_CASE__				:       Optional[int] = 1_2_8     ,							SCREAMING_SNAKE_CASE__				:       Tuple=False     ,							SCREAMING_SNAKE_CASE__				:       bool = False     ,							)		->			str:
                a_					:		Any							=  hans_processors[task]()
                a_					:		Union[str, Any]							=  processor.get_labels()
                if tokenizer.__class__ in (
                    RobertaTokenizer,
                    RobertaTokenizerFast,
                    XLMRobertaTokenizer,
                    BartTokenizer,
                    BartTokenizerFast,
                ):
                       # HACK(label indices are swapped in RoBERTa pretrained model)
                       a_							,		a_					:		List[Any]							=  label_list[2], label_list[1]
                a_					:		Union[str, Any]							=  label_list

                a_					:		Tuple							=  processor.get_dev_examples(SCREAMING_SNAKE_CASE__     ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE__     )
                a_					:		Tuple							=  hans_convert_examples_to_features(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )

                def gen():
                       for ex_index, ex in tqdm.tqdm(enumerate(self.features     )     ,							desc='convert examples to features'     ):
                              if ex_index % 1_0_0_0_0 == 0:
                                     logger.info('Writing example %d of %d' % (ex_index, len(SCREAMING_SNAKE_CASE__     ))     )

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

                a_					:		Tuple							=  tf.data.Dataset.from_generator(
                    SCREAMING_SNAKE_CASE__     ,							(
                        {
                            '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				:       List[Any]     )		->			Tuple:
                return self.dataset
         def __len__(       self				:       Dict     )		->			Tuple:
                return len(self.features     )
         def __getitem__(       self				:       Optional[Any]     ,							SCREAMING_SNAKE_CASE__				:       str     )		->			InputFeatures:
                return self.features[i]

         def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     )		->			Union[str, Any]:
                return self.label_list
class  SCREAMING_SNAKE_CASE__  (				lowercase__			):
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Tuple     )		->			Tuple:
             return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE__     ,							'heuristics_train_set.txt'     )     )     ,							'train'     )
      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]     )		->			List[str]:
             return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE__     ,							'heuristics_evaluation_set.txt'     )     )     ,							'dev'     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     )		->			List[Any]:
             return ["contradiction", "entailment", "neutral"]

      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       Tuple     )		->			str:
             a_					:		List[Any]							=  []
             for i, line in enumerate(SCREAMING_SNAKE_CASE__     ):
                    if i == 0:
                           continue
                    a_					:		List[Any]							=  '%s-%s' % (set_type, line[0])
                    a_					:		Tuple							=  line[5]
                    a_					:		Optional[int]							=  line[6]
                    a_					:		List[Any]							=  line[7][2:] if line[7].startswith('ex'     ) else line[7]
                    a_					:		Union[str, Any]							=  line[0]
                    examples.append(InputExample(guid=SCREAMING_SNAKE_CASE__     ,							text_a=SCREAMING_SNAKE_CASE__     ,							text_b=SCREAMING_SNAKE_CASE__     ,							label=SCREAMING_SNAKE_CASE__     ,							pairID=SCREAMING_SNAKE_CASE__     )     )
             return examples







def        SCREAMING_SNAKE_CASE_						(			__A :			List[InputExample]  ,    __A :			List[str]  ,    __A :			int  ,    __A :			PreTrainedTokenizer  ,    )	->		Union[str, Any]:

       """simple docstring"""

       a_					:		Tuple							=  {label: i for i, label in enumerate(__A				)}

       a_					:		List[str]							=  []
       for ex_index, example in tqdm.tqdm(enumerate(__A				)  ,    desc='convert examples to features'				):
              if ex_index % 1_00_00 == 0:
                     logger.info('Writing example %d' % (ex_index)				)

              a_					:		Optional[int]							=  tokenizer(
                  example.text_a  ,    example.text_b  ,    add_special_tokens=__A  ,    max_length=__A  ,    padding='max_length'  ,    truncation=__A  ,    return_overflowing_tokens=__A  ,    )

              a_					:		Union[str, Any]							=  label_map[example.label] if example.label in label_map else 0

              a_					:		Union[str, Any]							=  int(example.pairID				)

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

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

       return features


UpperCAmelCase_      : List[str]  					=       {
    'hans': 3,
}
UpperCAmelCase_      : Tuple  					=       {
    'hans': HansProcessor,
}






UpperCAmelCase_      : Optional[int]  					=       [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
UpperCAmelCase_      : str  					=       [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
UpperCAmelCase_      : str  					=       {
    0: 'Sunday',
    1: 'Monday',
    2: 'Tuesday',
    3: 'Wednesday',
    4: 'Thursday',
    5: 'Friday',
    6: 'Saturday',
}







def        SCREAMING_SNAKE_CASE_						(			__A :			int  ,    __A :			int  ,    __A :			int				)	->		str:

       """simple docstring"""

       assert len(str(__A				)				) > 2, "year should be in YYYY format"
       assert 1 <= month <= 12, "month should be between 1 to 12"
       assert 1 <= day <= 31, "day should be between 1 to 31"

       # Doomsday algorithm:
       a_					:		List[str]							=  year // 1_00
       a_					:		Optional[int]							=  (5 * (century % 4) + 2) % 7
       a_					:		List[str]							=  year % 1_00
       a_					:		str							=  centurian % 12
       a_					:		List[str]							=  (
           (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor
       ) % 7
       a_					:		Any							=  (
           DOOMSDAY_NOT_LEAP[month - 1]
           if (year % 4 != 0) or (centurian == 0 and (year % 4_00) == 0)
           else DOOMSDAY_LEAP[month - 1]
       )
       a_					:		Any							=  (dooms_day + day - day_anchor) % 7
       return WEEK_DAY_NAMES[week_day]


if __name__ == "__main__":
   import doctest

   doctest.testmod()


