


"""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             =   logging.getLogger(__name__)







@dataclass(frozen=snake_case_						)
class _lowerCAmelCase    :
   __UpperCAmelCase			:	str
   __UpperCAmelCase			:	str
   __UpperCAmelCase			:	Optional[str]							  =  None
   __UpperCAmelCase			:	Optional[str]							  =  None
   __UpperCAmelCase			:	Optional[str]							  =  None







@dataclass(frozen=snake_case_						)
class _lowerCAmelCase    :
   __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 _lowerCAmelCase    (				snake_case_						):
     __UpperCAmelCase			:	List[InputFeatures]







     def __init__(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__ = None						,     UpperCamelCase__=False						,     UpperCamelCase__ = False						,     )     ->		Dict:

         '''simple docstring'''




         snake_case   :				Tuple				 =   hans_processors[task]()

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

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

                 snake_case   :				Optional[Any]				 =   (
                     processor.get_dev_examples(UpperCamelCase__						) if evaluate else processor.get_train_examples(UpperCamelCase__						)
                 )

                 logger.info("Training examples: %s"						,     len(UpperCamelCase__						)						)
                 snake_case   :				Optional[Any]				 =   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						)     ->		List[Any]:

         '''simple docstring'''




         return len(self.features						)







     def __getitem__(     self						,     UpperCamelCase__						)     ->		InputFeatures:

         '''simple docstring'''




         return self.features[i]







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

         '''simple docstring'''




         return self.label_list


if is_tf_available():
  import tensorflow as tf







  class _lowerCAmelCase    :
     __UpperCAmelCase			:	List[InputFeatures]







     def __init__(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__ = 128						,     UpperCamelCase__=False						,     UpperCamelCase__ = False						,     )     ->		Tuple:

         '''simple docstring'''




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

         snake_case   :				Union[str, Any]				 =   processor.get_dev_examples(UpperCamelCase__						) if evaluate else processor.get_train_examples(UpperCamelCase__						)
         snake_case   :				List[Any]				 =   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 % 1_0000 == 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,
                 )

         snake_case   :				Tuple				 =   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 			lowerCamelCase		(     self						)     ->		Optional[int]:

         '''simple docstring'''




         return self.dataset







     def __len__(     self						)     ->		int:

         '''simple docstring'''




         return len(self.features						)







     def __getitem__(     self						,     UpperCamelCase__						)     ->		InputFeatures:

         '''simple docstring'''




         return self.features[i]







     def 			lowerCamelCase		(     self						)     ->		str:

         '''simple docstring'''




         return self.label_list







class _lowerCAmelCase    (				snake_case_						):







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						)     ->		Tuple:

       '''simple docstring'''




       return self._create_examples(self._read_tsv(os.path.join(UpperCamelCase__						,     "heuristics_train_set.txt"						)						)						,     "train"						)







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						)     ->		str:

       '''simple docstring'''




       return self._create_examples(self._read_tsv(os.path.join(UpperCamelCase__						,     "heuristics_evaluation_set.txt"						)						)						,     "dev"						)







   def 			lowerCamelCase		(     self						)     ->		Optional[int]:

       '''simple docstring'''




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







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		Optional[Any]:

       '''simple docstring'''




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







def   __lowerCAmelCase						(						lowercase  :  List[InputExample]						,							lowercase  :  List[str]						,							lowercase  :  int						,							lowercase  :  PreTrainedTokenizer						,							)     ->						List[Any]:


    """simple docstring"""





    snake_case   :				str				 =   {label: i for i, label in enumerate(lowercase			)}

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

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

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

        snake_case   :				Optional[Any]				 =   int(example.pairID			)

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

    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             =   {
    """hans""": 3,
}


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









"""simple docstring"""



import math
import sys







def   __lowerCAmelCase						(						lowercase  :  int			)     ->						int:


    """simple docstring"""





    if number != int(lowercase			):
        raise ValueError("the value of input must be a natural number"			)
    if number < 0:
        raise ValueError("the value of input must not be a negative number"			)
    if number == 0:
        return 1
    snake_case   :				Optional[Any]				 =   [-1] * (number + 1)
    snake_case   :				str				 =   0
    for i in range(1						,							number + 1			):
        snake_case   :				List[Any]				 =   sys.maxsize
        snake_case   :				Union[str, Any]				 =   int(math.sqrt(lowercase			)			)
        for j in range(1						,							root + 1			):
            snake_case   :				List[str]				 =   1 + answers[i - (j**2)]
            snake_case   :				Optional[Any]				 =   min(lowercase						,							lowercase			)
        snake_case   :				Any				 =   answer
    return answers[number]


if __name__ == "__main__":
  import doctest

  doctest.testmod()





