





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


_a 						=      logging.getLogger(__name__)






@dataclass(frozen=lowerCAmelCase )
class 					__A    :

			'''simple docstring'''







			lowerCAmelCase_							     =						42
			lowerCAmelCase_							     =						42
			lowerCAmelCase_							     =						None
			lowerCAmelCase_							     =						None
			lowerCAmelCase_							     =						None






@dataclass(frozen=lowerCAmelCase )
class 					__A    :

			'''simple docstring'''







			lowerCAmelCase_							     =						42
			lowerCAmelCase_							     =						None
			lowerCAmelCase_							     =						None
			lowerCAmelCase_							     =						None
			lowerCAmelCase_							     =						None


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






			class 					__A    (    lowerCAmelCase ):

						'''simple docstring'''







						lowerCAmelCase_							     =						42





						def __init__(    self       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase = None       ,  __lowerCAmelCase=False       ,  __lowerCAmelCase = False       ,  ):



							'''simple docstring'''







							lowerCamelCase__								=		hans_processors[task]()

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

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

									lowerCamelCase__								=		(
									    processor.get_dev_examples(__lowerCAmelCase							) if evaluate else processor.get_train_examples(__lowerCAmelCase							)
									)

									logger.info('''Training examples: %s'''       ,  len(__lowerCAmelCase							)							)
									lowerCamelCase__								=		hans_convert_examples_to_features(__lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase							)
									logger.info('''Saving features into cached file %s'''       ,  __lowerCAmelCase							)
									torch.save(self.features       ,  __lowerCAmelCase							)





						def __len__(    self							):



							'''simple docstring'''







							return len(self.features							)





						def __getitem__(    self       ,  __lowerCAmelCase							):



							'''simple docstring'''







							return self.features[i]





						def 		__lowerCamelCase				(    self							):



							'''simple docstring'''







							return self.label_list


if is_tf_available():
			import tensorflow as tf






			class 					__A    :

						'''simple docstring'''







						lowerCAmelCase_							     =						42





						def __init__(    self       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase = 1_2_8       ,  __lowerCAmelCase=False       ,  __lowerCAmelCase = False       ,  ):



							'''simple docstring'''







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

							lowerCamelCase__								=		processor.get_dev_examples(__lowerCAmelCase							) if evaluate else processor.get_train_examples(__lowerCAmelCase							)
							lowerCamelCase__								=		hans_convert_examples_to_features(__lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase							)

							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(__lowerCAmelCase							))							)

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

							lowerCamelCase__								=		tf.data.Dataset.from_generator(
							    __lowerCAmelCase       ,  (
							        {
							            '''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 		__lowerCamelCase				(    self							):



							'''simple docstring'''







							return self.dataset





						def __len__(    self							):



							'''simple docstring'''







							return len(self.features							)





						def __getitem__(    self       ,  __lowerCAmelCase							):



							'''simple docstring'''







							return self.features[i]





						def 		__lowerCamelCase				(    self							):



							'''simple docstring'''







							return self.label_list






class 					__A    (    lowerCAmelCase ):

			'''simple docstring'''







			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase							):



				'''simple docstring'''







				return self._create_examples(self._read_tsv(os.path.join(__lowerCAmelCase       ,  '''heuristics_train_set.txt'''							)							)       ,  '''train'''							)





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase							):



				'''simple docstring'''







				return self._create_examples(self._read_tsv(os.path.join(__lowerCAmelCase       ,  '''heuristics_evaluation_set.txt'''							)							)       ,  '''dev'''							)





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







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





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase       ,  __lowerCAmelCase							):



				'''simple docstring'''







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





def lowerCAmelCase__(__snake_case       ,__snake_case       ,__snake_case       ,__snake_case       ,)				->      str:







	'''simple docstring'''







	lowerCamelCase__								=		{label: i for i, label in enumerate(__snake_case							)}

	lowerCamelCase__								=		[]
	for ex_index, example in tqdm.tqdm(enumerate(__snake_case							)       ,desc='''convert examples to features'''							):
		if ex_index % 10000 == 0:
			logger.info('''Writing example %d''' % (ex_index)							)

		lowerCamelCase__								=		tokenizer(
		    example.text_a       ,example.text_b       ,add_special_tokens=__snake_case       ,max_length=__snake_case       ,padding='''max_length'''       ,truncation=__snake_case       ,return_overflowing_tokens=__snake_case       ,)

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

		lowerCamelCase__								=		int(example.pairID							)

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

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

	return features


_a 						=      {
    "hans": 3,
}



_a 						=      {
    "hans": HansProcessor,
}










def lowerCAmelCase__(__snake_case       ,__snake_case							)				->      float:







	'''simple docstring'''







	if mass < 0:
		raise ValueError('''The mass of a body cannot be negative'''							)
	return 0.5 * mass * abs(__snake_case							) * abs(__snake_case							)


if __name__ == "__main__":
			import doctest

			doctest.testmod(verbose=True)



