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


__snake_case:  Optional[Any]												=			logging.getLogger(__name__)


@dataclass(frozen=lowerCAmelCase__    )
class  _UpperCAmelCase :

     """simple docstring"""

     a_    	=					42
     a_    	=					42
     a_    	=					None
     a_    	=					None
     a_    	=					None







@dataclass(frozen=lowerCAmelCase__    )
class  _UpperCAmelCase :

     """simple docstring"""

     a_    	=					42
     a_    	=					None
     a_    	=					None
     a_    	=					None
     a_    	=					None


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


    class  _UpperCAmelCase ( lowerCAmelCase__    ):

         """simple docstring"""

         a_    	=					42


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

           '''simple docstring'''

           a_      :	Optional[int]      =							hans_processors[task]()

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

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

               a_      :	List[str]      =							(
                   processor.get_dev_examples(lowerCAmelCase_ ) if evaluate else processor.get_train_examples(lowerCAmelCase_ )
               )

               logger.info("""Training examples: %s"""							,		len(lowerCAmelCase_ ) )
               a_      :	Optional[int]      =							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  _UpperCAmelCase :

         """simple docstring"""

         a_    	=					42


         def __init__(      self							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_ = 1_28							,		lowerCAmelCase_=False							,		lowerCAmelCase_ = False							,		):

           '''simple docstring'''

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

           a_      :	Union[str, Any]      =							processor.get_dev_examples(lowerCAmelCase_ ) if evaluate else processor.get_train_examples(lowerCAmelCase_ )
           a_      :	Union[str, Any]      =							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_00_00 == 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,
               )

           a_      :	str      =							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  _UpperCAmelCase ( 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'''

       a_      :	Any      =							[]
       for i, line in enumerate(lowerCAmelCase_ ):
         if i == 0:
           continue
         a_      :	Optional[Any]      =							"""%s-%s""" % (set_type, line[0])
         a_      :	Optional[Any]      =							line[5]
         a_      :	Union[str, Any]      =							line[6]
         a_      :	Optional[Any]      =							line[7][2:] if line[7].startswith("""ex""" ) else line[7]
         a_      :	Any      =							line[0]
         examples.append(InputExample(guid=lowerCAmelCase_							,		text_a=lowerCAmelCase_							,		text_b=lowerCAmelCase_							,		label=lowerCAmelCase_							,		pairID=lowerCAmelCase_ ) )
       return examples







def 	_snake_case						(      A_       :    List[InputExample]     ,		A_       :    List[str]     ,		A_       :    int     ,		A_       :    PreTrainedTokenizer     ,		):
  """simple docstring"""







  a_      :	Any      =							{label: i for i, label in enumerate(A_   )}

  a_      :	Optional[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)   )

    a_      :	Dict      =							tokenizer(
        example.text_a     ,		example.text_b     ,		add_special_tokens=A_     ,		max_length=A_     ,		padding="""max_length"""     ,		truncation=A_     ,		return_overflowing_tokens=A_     ,		)

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

    a_      :	int      =							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


__snake_case:  Tuple												=			{
    "hans": 3,
}




__snake_case:  Dict												=			{
    "hans": HansProcessor,
}




'''simple docstring'''

from __future__ import annotations






def 	_snake_case						(      A_       :    int   ):
  """simple docstring"""







  a_      :	Optional[Any]      =							2
  a_      :	int      =							[]
  while i * i <= n:
    if n % i:
      i += 1
    else:
      n //= i
      factors.append(A_   )
  if n > 1:
    factors.append(A_   )
  return factors


if __name__ == "__main__":
    import doctest

    doctest.testmod()


