

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


__lowercase     : Any       =						logging.getLogger(__name__)



@dataclass(frozen=snake_case				)
class 						_A     :





  '''simple docstring'''



  __lowerCamelCase			:      str
  __lowerCamelCase			:      str
  __lowerCamelCase			:      Optional[str]					=   None
  __lowerCamelCase			:      Optional[str]					=   None
  __lowerCamelCase			:      Optional[str]					=   None







@dataclass(frozen=snake_case				)
class 						_A     :





  '''simple docstring'''



  __lowerCamelCase			:      List[int]
  __lowerCamelCase			:      Optional[List[int]]					=   None
  __lowerCamelCase			:      Optional[List[int]]					=   None
  __lowerCamelCase			:      Optional[Union[int, float]]					=   None
  __lowerCamelCase			:      Optional[int]					=   None


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



     class 						_A     (				snake_case				):





       '''simple docstring'''



       __lowerCamelCase			:      List[InputFeatures]





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

           '''simple docstring'''
           snake_case							:		str        =		hans_processors[task]()

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

           # Make sure only the first process in distributed training processes the dataset,
           # and the others will use the cache.
           snake_case							:		Any        =		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}"""    )
                   snake_case							:		int        =		torch.load(SCREAMING_SNAKE_CASE_    )
               else:
                   logger.info(F"""Creating features from dataset file at {data_dir}"""    )

                   snake_case							:		Union[str, 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_    )    )
                   snake_case							:		Dict        =		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    ):

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





       def __getitem__(  self   ,SCREAMING_SNAKE_CASE_    ):

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





       def    snake_case_       (  self    ):

           '''simple docstring'''
           return self.label_list


if is_tf_available():
     import tensorflow as tf



     class 						_A     :





       '''simple docstring'''



       __lowerCamelCase			:      List[InputFeatures]





       def __init__(  self   ,SCREAMING_SNAKE_CASE_   ,SCREAMING_SNAKE_CASE_   ,SCREAMING_SNAKE_CASE_   ,SCREAMING_SNAKE_CASE_ = 128   ,SCREAMING_SNAKE_CASE_=False   ,SCREAMING_SNAKE_CASE_ = False   ,):

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

           snake_case							:		int        =		processor.get_dev_examples(SCREAMING_SNAKE_CASE_    ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_    )
           snake_case							:		Any        =		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 % 10000 == 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,
                   )

           snake_case							:		Any        =		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    snake_case_       (  self    ):

           '''simple docstring'''
           return self.dataset





       def __len__(  self    ):

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





       def __getitem__(  self   ,SCREAMING_SNAKE_CASE_    ):

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





       def    snake_case_       (  self    ):

           '''simple docstring'''
           return self.label_list



class 						_A     (				snake_case				):





  '''simple docstring'''



  def    snake_case_       (  self   ,SCREAMING_SNAKE_CASE_    ):

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





  def    snake_case_       (  self   ,SCREAMING_SNAKE_CASE_    ):

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





  def    snake_case_       (  self    ):

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





  def    snake_case_       (  self   ,SCREAMING_SNAKE_CASE_   ,SCREAMING_SNAKE_CASE_    ):

      '''simple docstring'''
      snake_case							:		List[str]        =		[]
      for i, line in enumerate(SCREAMING_SNAKE_CASE_    ):
          if i == 0:
              continue
          snake_case							:		Any        =		"""%s-%s""" % (set_type, line[0])
          snake_case							:		Optional[int]        =		line[5]
          snake_case							:		Union[str, Any]        =		line[6]
          snake_case							:		Optional[Any]        =		line[7][2:] if line[7].startswith("""ex"""    ) else line[7]
          snake_case							:		Dict        =		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       lowercase					(	__A :					List[InputExample] ,    __A :					List[str] ,    __A :					int ,    __A :					PreTrainedTokenizer ,    )				->			Tuple:





    '''simple docstring'''




    snake_case							:		List[Any]        =		{label: i for i, label in enumerate(__A		)}

    snake_case							:		Union[str, Any]        =		[]
    for ex_index, example in tqdm.tqdm(enumerate(__A		) ,    desc="""convert examples to features"""		):
        if ex_index % 1_0000 == 0:
            logger.info("""Writing example %d""" % (ex_index)		)

        snake_case							:		Union[str, Any]        =		tokenizer(
            example.text_a ,    example.text_b ,    add_special_tokens=__A ,    max_length=__A ,    padding="""max_length""" ,    truncation=__A ,    return_overflowing_tokens=__A ,    )

        snake_case							:		Tuple        =		label_map[example.label] if example.label in label_map else 0

        snake_case							:		Tuple        =		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


__lowercase     : Dict       =						{
    '''hans''': 3,
}


__lowercase     : Union[str, Any]       =						{
    '''hans''': HansProcessor,
}










class 		snake_case_   :







	'''simple docstring'''




	def __init__(						self							:     Dict					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Optional[Any]=None					,      __lowerCamelCase							:     Optional[Any]=None   )       -> Dict:

			'''simple docstring'''




			__lowercase											=     data
			__lowercase											=     previous
			__lowercase											=     next_node




	def __str__(						self							:     int   )       -> str:

			'''simple docstring'''




			return F"{self.data}"




	def    UpperCAmelCase			(						self							:     int   )       -> int:

			'''simple docstring'''




			return self.data




	def    UpperCAmelCase			(						self							:     Tuple   )       -> Optional[int]:

			'''simple docstring'''




			return self.next






	def    UpperCAmelCase			(						self							:     Optional[int]   )       -> Any:

			'''simple docstring'''




			return self.previous





class 		snake_case_   :







	'''simple docstring'''




	def __init__(						self							:     Tuple					,      __lowerCamelCase							:     Optional[Any]   )       -> str:

			'''simple docstring'''




			__lowercase											=     head




	def __iter__(						self							:     Union[str, Any]   )       -> List[Any]:

			'''simple docstring'''




			return self






	def    UpperCAmelCase			(						self							:     str   )       -> str:

			'''simple docstring'''




			if not self.current:
					raise StopIteration
			else:
					__lowercase											=     self.current.get_data()
					__lowercase											=     self.current.get_next()
					return value





class 		snake_case_   :







	'''simple docstring'''




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

			'''simple docstring'''




			__lowercase											=     None  # First node in list
			__lowercase											=     None  # Last node in list




	def __str__(						self							:     int   )       -> Any:

			'''simple docstring'''




			__lowercase											=     self.head
			__lowercase											=     []
			while current is not None:
					nodes.append(current.get_data()   )
					__lowercase											=     current.get_next()
			return " ".join(str(__lowerCamelCase   ) for node in nodes   )




	def __contains__(						self							:     Tuple					,      __lowerCamelCase							:     int   )       -> List[Any]:

			'''simple docstring'''




			__lowercase											=     self.head
			while current:
					if current.get_data() == value:
							return True
					__lowercase											=     current.get_next()
			return False




	def __iter__(						self							:     Optional[int]   )       -> List[str]:

			'''simple docstring'''




			return LinkedListIterator(self.head   )




	def    UpperCAmelCase			(						self							:     Any   )       -> str:

			'''simple docstring'''




			if self.head:
					return self.head.get_data()
			return None




	def    UpperCAmelCase			(						self							:     str   )       -> Union[str, Any]:

			'''simple docstring'''




			if self.tail:
					return self.tail.get_data()
			return None




	def    UpperCAmelCase			(						self							:     str					,      __lowerCamelCase							:     Node   )       -> None:

			'''simple docstring'''




			if self.head is None:
					__lowercase											=     node
					__lowercase											=     node
			else:
					self.insert_before_node(self.head					,      __lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Dict					,      __lowerCamelCase							:     Node   )       -> None:

			'''simple docstring'''




			if self.head is None:
					self.set_head(__lowerCamelCase   )
			else:
					self.insert_after_node(self.tail					,      __lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Optional[Any]					,      __lowerCamelCase							:     int   )       -> None:

			'''simple docstring'''




			__lowercase											=     Node(__lowerCamelCase   )
			if self.head is None:
					self.set_head(__lowerCamelCase   )
			else:
					self.set_tail(__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     int					,      __lowerCamelCase							:     Node					,      __lowerCamelCase							:     Node   )       -> None:

			'''simple docstring'''




			__lowercase											=     node
			__lowercase											=     node.previous

			if node.get_previous() is None:
					__lowercase											=     node_to_insert
			else:
					__lowercase											=     node_to_insert

			__lowercase											=     node_to_insert




	def    UpperCAmelCase			(						self							:     Optional[Any]					,      __lowerCamelCase							:     Node					,      __lowerCamelCase							:     Node   )       -> None:

			'''simple docstring'''




			__lowercase											=     node
			__lowercase											=     node.next

			if node.get_next() is None:
					__lowercase											=     node_to_insert
			else:
					__lowercase											=     node_to_insert

			__lowercase											=     node_to_insert




	def    UpperCAmelCase			(						self							:     List[str]					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     int   )       -> None:

			'''simple docstring'''




			__lowercase											=     1
			__lowercase											=     Node(__lowerCamelCase   )
			__lowercase											=     self.head
			while node:
					if current_position == position:
							self.insert_before_node(__lowerCamelCase					,      __lowerCamelCase   )
							return
					current_position += 1
					__lowercase											=     node.next
			self.insert_after_node(self.tail					,      __lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Optional[Any]					,      __lowerCamelCase							:     int   )       -> Node:

			'''simple docstring'''




			__lowercase											=     self.head
			while node:
					if node.get_data() == item:
							return node
					__lowercase											=     node.get_next()
			raise Exception('Node not found'   )




	def    UpperCAmelCase			(						self							:     Optional[Any]					,      __lowerCamelCase							:     List[str]   )       -> Dict:

			'''simple docstring'''




			if (node := self.get_node(__lowerCamelCase   )) is not None:
					if node == self.head:
							__lowercase											=     self.head.get_next()

					if node == self.tail:
							__lowercase											=     self.tail.get_previous()

					self.remove_node_pointers(__lowerCamelCase   )




	@staticmethod
	def    UpperCAmelCase			(						__lowerCamelCase							:     Node   )       -> None:

			'''simple docstring'''




			if node.get_next():
					__lowercase											=     node.previous

			if node.get_previous():
					__lowercase											=     node.next

			__lowercase											=     None
			__lowercase											=     None






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

			'''simple docstring'''




			return self.head is None





def  SCREAMING_SNAKE_CASE				(      )		->    None:
		pass


if __name__ == "__main__":
		import doctest

		doctest.testmod()




